This article will walk you through setting up a Linux user with read and write permissions for your web document root, usually the /var/www/ directory. Connecting with this user via SFTP will let you upload your website content directly to the /var/www/your/site/folder.
For the purposes of this example we'll use an account named "demo". Be sure to replace "demo" in the examples with your preferred name.
These commands require superuser privileges so they assume you are running them from an account with sudo privileges.
We'll need to know the group the web server process is running under as well as the location of your web server's document root. This information can usually be found in the web server's config file (like httpd.conf or apache2.conf for apache).
We've listed the default values for the apache web server running on some Linux distributions below.
On Red Hat-based systems apache runs under the group "apache" with a document root of "/var/www/html".
On Debian-based systems apache runs under the group "www-data" with a document root of "/var/www".
Now we can either create a new user or modify an existing user for our purposes.
If we're creating a new user, we'll want it to be in the same group as the web server with its home directory set to your document root.
Remember to change the values to match your web server's settings and the username you're using.
On CentOS, Fedora, or RHEL, the command to create the user would look like this:
sudo useradd -d /var/www/html -G apache demo
On Ubuntu or Debian systems you would use different values:
sudo useradd -d /var/www -G www-data demo
Once the user is created you'll need to set its password as well.
sudo passwd demo
You can now skip to the section on changing the document root to be group-writable.
If you want to modify an existing user you'll need to add it to the group used by your web server.
On CentOS, Fedora, or RHEL, the command would look like this:
sudo usermod -a -G apache demo
And on Ubuntu or Debian it might look like:
sudo usermod -a -G www-data demo
If you want to change the account to use the document root as its home directory you can do that too.
On CentOS, Fedora, or RHEL you would run:
sudo usermod -d /var/www/html demo
And on Ubuntu or Debian:
sudo usermod -d /var/www demo
Now we'll change the document root so it and its contents are in the same group as the web server.
On CentOS, Fedora, or RHEL run:
sudo chgrp -R apache /var/www/html
And on Ubuntu or Debian:
sudo chgrp -R www-data /var/www
Next we make the document root group-writable, but we'll also want to set the "setgid" permission on the document root directory itself. The setgid permission will ensure that new files created in the document root will inherit the group ID from their parent directory.
On CentOS, Fedora, or RHEL you can set the right permissions with the commands:
sudo chmod -R g+w /var/www/html
sudo chmod g+s /var/www/html
The Ubuntu and Debian versions of the commands would be:
sudo chmod -R g+w /var/www
sudo chmod g+s /var/www
Now you can connect to your server via sftp with the user account you created or modified. Try uploading a file to make sure the permissions were set correctly. If you get a permission denied error run an "ls -la" in the document root to check the directory permissions.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

8 Comments
hello
on centos 6, logged in as root in putty, when i type in:
usermod -a -G apache demo
it returns:
usermod: no changes
however i continue through the steps and the user still can not upload or delete files from var/www/html (after restarting apache).
i am connecting via ftp on port 21 (i need to be able to do this to run a restore script temporarily) and in vsftpd.conf have chroot_local_user=NO and in iptables i have -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT so the user can browse around folders at the moment, but cannot make any changes it seems.
i want to be able to give this user privileges to add, edit, delete files and folders in var/www/html.
thanks!
Re: permissions
You might try running "ls -l /var/www" and check the results. Make sure the group for the html directory is "demo". Also check the permissions on the directory - they should look something like "rwxrwxr-x" if the permissions are properly set.
It won't hurt to run both the "chgrp" and the "chmod" commands again. Make sure to either run them as root or use the "sudo" command with them.
Helpful - Formatting
Because all of the information is the same text weight and font size, it is hard to pull the good info out.
re: formatting
This approach will not lock
re: locked directory
For more details on Linux file permissions, see this artice series:
http://www.rackspace.com/knowledge_center/article/linux-file-permission-concepts
Updates in the doc root
Any suggestions for how to solve for this?
re: Updates in the doc root
Add new comment