I've studied this topic for a while and can't get it work for some reason.
Particularly, these lines interest me the most:
To change all the permissions of each file and folder under a specified directory at once, use sudo chmod with -R
$ sudo chmod 777 -R /path/to/someDirectory $ ls -l total 3 -rwxrwxrwx 1 user user 0 Nov 19 20:13 file1 drwxrwxrwx 2 user user 4096 Nov 19 20:13 folder -rwxrwxrwx 1 user user 0 Nov 19 20:13 file2
Here's what I typed:
mark@ubuntuserver:~$ sudo chmod 755 /var/www/html
mark@ubuntuserver:~$ ls -l
total 0Then I checked any changes in sftp:
sftp> cd /
sftp> cd var/www/html
sftp> pwd
Remote working directory: /var/www/html
sftp> ls -l
-rw-r--r-- 1 root root 11321 Apr 10 20:07 index.htmlFrom the output it's clear that the html directory is still modifiable only by root.
How can I change this in the way that me (non-root) can upload files to the html directory ?
I also tested:
A file's owner can be changed using the chown command. For example, to change the foobar file's owner to tux:
$ sudo chown tux foobar
I typed from the server:
sudo chown mark ownerno effect.
105 Answers
You can make yourself the owner of that directory
sudo chown pi /var/www/htmlBut you definitely want to set the permissions
chmod 755 -R /var/www/htmlUsing the -R (recursive) option will make sure that your script files and your .htaccess file are all set the same.
Whenever you add a new script, be sure to set it this way also.
chmod 755 /var/www/html/cgi-bin/newscript.cgiNote that some CGI programs have their own requirements, such as the
Bestdam Website Visitor Logger + Hit Counter
Excerpt:
Open the bdlogger.pl file in a text editor and if necessary, modify the shebang line.
On the server, create a bdlogger subdirectory under your cgi-bin directory and FTP the script and data files into it using ASCII mode
If you using a UNIX/Linux server, chmod the files:
bdlogger.pl to 755 all other files to 666Add the following SSI directive tag to your Web page(s):
<!--#exec cgi="/cgi-bin/bdlogger/bdlogger.pl" --> In this case I would leave the directory ownership alone. To modify the permissions for that specific directory so that you can write to it, set read/write permissions, the command being sudo chmod 766 -R /var/www/html. This will assign full permissions 7 for the owner, read/write 6 for the group, and read/write for everyone 6, recursively.
You need to set the www folder owner from 'root' to 'Me'.
From your terminal, run the command :
sudo chown -R your_system_username /var/wwwHope it Works!!
While the above answers will pretty much do the trick, this is a more wholesome approach. Chances are you want to modify files inside /var/www/html because you want to add a web app to serve in Apache2. To achieve this you need the following steps
- Add the user to the Usergroup
www-datawhich has default ownership of the folder. Why? Cause if you intend to make any changes on the web app dynamically, even if its inwordpress, you'll need the users in this group to have write permissions. To do this just write:sudo adduser username grouptoaddIn our case it will besudo adduser username www-data. - Next you need to change the
read/write permissionsusing thechmodcommand. Remember now you are a part of the usergroup and do not need to change ownership usingchown. To do this just run:sudo chmod -R 766 directory, in this case it will besudo chmod -R 766 html. If you feeling sporty, you can get full write permissions(777),depending on how safe your app is.
The -R is meant is a recursive function, allowing the command to apply to all subdirectories. I hope this helps someone in need.
In your case I see root owns the file so first we ;
sudo chown -R www-data:www-data /var/www/htmlto allow the group www-data to take ownership of folder.
2.Then sudo adduser username www-datathis is to add your user to the group www-data which now has ownership of the folder /var/www/hmtl/
(NB:Replace username with your computer user name if you don't know your computer username type whoami into your terminal you get the username of the current user logged in)
sudo chmod -R 775 /var/www/html/To now change file permissions to 775 so that, (U)ser / owner can read, can write and can execute. (G)roup can read, can write and can execute. (O)thers can read, can't write and can execute.If step 3 fails then
sudo chmod -R 777 /var/www/html/to free up all users to read, write and execute on that folder. But I highly don't recommend this on a live server, if it's local or you have no other option do that and don't forget to change permissions back to much safer settings or risk getting hacked if this is online.
I hope this is useful