15

I've studied this topic https://help.ubuntu.com/community/FilePermissions 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 0

Then 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.html

From 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 owner

no effect.

10
  • You are missing the -r option for recursive. And 777 is -never- the correct change to make (unless you also set the t option (sticky bit). Question: why is the dir empty at the 2nd command and filled with index.html at the 3rd command? Those commands are NOT issued with the same situations. sudo chown mark owner what is that suppose to do? How come you introduce a file or dir called "owner" there?
    – Rinzwind
    Apr 14 '17 at 19:33
  • To take ownership of a file or directory, you have to specify who the new owner is, the group (optional) and the file or directory. For example sudo chown user:user mydocs where the new owner and group is user and the directory you are taking ownership of is mydocs
    – Gansheim
    Apr 14 '17 at 19:35
  • 1
    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
    – Gansheim
    Apr 14 '17 at 19:37
  • @Rinzwind looks like he issued the ls -l command from their home directory. @Mark Unless you are in the directory that you are trying to view/manipulate, you need to specify the path, in this case you should have issued the command as ls -l /var/www/html
    – Gansheim
    Apr 14 '17 at 19:40
  • 1
    Like the guys have pointed out, chmod changes permissions, chown changes owner. 755 first digit (7) means that 'owner' has all permissions, while second and last digits (5) mean that 'group' and 'other' both have read and execute permissions. You could add your user to the group and give full permissions to group too so that you don't have to make that many changes.
    – Samuel
    Apr 14 '17 at 19:45
14

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.

17

You can make yourself the owner of that directory

sudo chown pi /var/www/html

But you definitely want to set the permissions

chmod 755 -R /var/www/html

Using 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.cgi

Note 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 666

Add the following SSI directive tag to your Web page(s):

<!--#exec cgi="/cgi-bin/bdlogger/bdlogger.pl" -->
6

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/www

Hope it Works!!

3

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

  1. Add the user to the Usergroup www-data which has default ownership of the folder. Why? Cause if you intend to make any changes on the web app dynamically, even if its in wordpress, you'll need the users in this group to have write permissions. To do this just write: sudo adduser username grouptoadd In our case it will be sudo adduser username www-data.
  2. Next you need to change the read/write permissions using the chmod command. Remember now you are a part of the usergroup and do not need to change ownership using chown. To do this just run: sudo chmod -R 766 directory, in this case it will be sudo 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.

2

In your case I see root owns the file so first we ;

  1. sudo chown -R www-data:www-data /var/www/html to allow the group www-data to take ownership of folder.

2.Then sudo adduser username www-data this 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)

  1. 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.

  2. 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.