How to change File and Folder Ownership in Drupal and Linux

knooq-howto.png

There are occasions when you'll need to change the file and folder ownership for your Drupal installation on your Linux web-server, VPS or home PC. Using the command line in Linux Terminal can be a little off-putting to begin with, however once you learn a few commands it'll become second nature to you.

Displaying Drupal Files and Directories in Linux

To start with, there are a few useful commands for displaying your Drupal files and folders on your Linux system. A lot of you will be familiar with them, however, for reference, we'll list here a few that we use frequently:

List Files and Folders

Navigate to the root of your Drupal (Drupal 7 in our example) installation and type in the 'ls' command. You will see a list of the files in your Drupal installation, as follows:

authorize.php  includes           install.php         MAINTAINERS.txt  README.txt  themes       xmlrpc.php
CHANGELOG.txt  index.php          INSTALL.sqlite.txt  misc             robots.txt  update.php
COPYRIGHT.txt  INSTALL.mysql.txt  INSTALL.txt         modules          scripts     UPGRADE.txt
cron.php       INSTALL.pgsql.txt  LICENSE.txt         profiles         sites       web.config

What we really want to see here is the assigned ownership of the files and folders in the Drupal directory. We can see these by typing 'ls -al'. This Terminal command produces the following:

drwxr-xr-x  9 owner group 4.0K 2011-10-26 22:25 .
drwxr-xr-x  4 owner group 4.0K 2011-11-27 16:43 ..
-rw-r--r--  1 owner group 6.5K 2011-10-26 21:53 authorize.php
-rw-r--r--  1 owner group  61K 2011-10-26 21:53 CHANGELOG.txt
-rw-r--r--  1 owner group  996 2011-10-26 21:53 COPYRIGHT.txt
-rw-r--r--  1 owner group  720 2011-10-26 21:53 cron.php
-rw-r--r--  1 owner group 174 2011-10-26 21:53 .gitignore
-rw-r--r--  1 owner group 5.1K 2011-10-26 21:53 .htaccess
drwxr-xr-x  4 owner group 4.0K 2011-10-26 21:53 includes
-rw-r--r--  1 owner group  529 2011-10-26 21:53 index.php
-rw-r--r--  1 owner group 1.5K 2011-10-26 21:53 INSTALL.mysql.txt
-rw-r--r--  1 owner group 1.9K 2011-10-26 21:53 INSTALL.pgsql.txt
-rw-r--r--  1 owner group  688 2011-10-26 21:53 install.php
-rw-r--r--  1 owner group 1.3K 2011-10-26 21:53 INSTALL.sqlite.txt
-rw-r--r--  1 owner group  18K 2011-10-26 21:53 INSTALL.txt
-rw-rw-r--  1 owner group  18K 2011-09-17 23:50 LICENSE.txt
-rw-r--r--  1 owner group 7.5K 2011-10-26 21:53 MAINTAINERS.txt
drwxr-xr-x  4 owner group 4.0K 2011-10-26 21:53 misc
drwxr-xr-x 42 owner group 4.0K 2011-10-26 21:53 modules
drwxr-xr-x  5 owner group 4.0K 2011-10-26 21:53 profiles
-rw-r--r--  1 owner group 3.5K 2011-10-26 21:53 README.txt
-rw-r--r--  1 owner group 1.6K 2011-10-26 21:53 robots.txt
drwxr-xr-x  2 owner group 4.0K 2011-10-26 21:53 scripts
drwxr-xr-x  4 owner group 4.0K 2011-10-26 21:53 sites
drwxr-xr-x  8 owner group 4.0K 2011-10-26 21:53 themes
-rw-r--r--  1 owner group  18K 2011-10-26 21:53 update.php
-rw-r--r--  1 owner group 9.3K 2011-10-26 21:53 UPGRADE.txt
-rw-r--r--  1 owner group 2.1K 2011-10-26 21:53 web.config
-rw-r--r--  1 owner group  417 2011-10-26 21:53 xmlrpc.php

From the Terminal output above we can see the permissions and ownership assigned to the Drupal files and folders. In this case, the 'owner' is the 'username' where the Drupal folder is installed, and the 'group' is the default group for that 'username'.

If you want to know how to change the permissions of Drupal and Linux files and folders we have written another blog article covering this.

As mentioned, the "Owner" and "Group" will normally be the default user and group of the user that installed the Drupal CMS. But, if you installed the folders while using the 'sudo' command this will often be root:root.

Giving the Apache Server access to the Drupal Files

The default group for your server is www-data, so in order to give the server access to the Drupal files and folders, navigate to the parent folder that contains the Drupal CMS and use the following command:

sudo chown -R user1:www-data mydrupalfolder

Or from inside the Drupal folder you can use the following command:

sudo chown -R user1:www-data *

However, be very careful with the second command because the "*" asterisk in the command means 'everything' and will change all of the files in the directory. Coupled with the "R" recursive, this command will apply the ownership change to all files, folders and their sub-files and sub-folders/sub-directories.

We cannot stress enough, how important it is to be aware of the potential problems you could cause by using the "R" recursive command, because it drills down through all of the sub-files and sub-folders. To begin with, you should try changing ownership of files and folders on an individual basis, using the following simpler command:

sudo chown user1:www-data sites

In the command above, we're giving ownership of only the '/sites' folder to 'user1' and are changing the group to 'www-data' for the server.

After you run the relevant commands, the ownership of your folders and files should look like this:

drwxr-xr-x  9 user1 www-data 4.0K 2011-10-26 22:25 .
drwxr-xr-x  4 user1 www-data 4.0K 2011-11-27 16:43 ..
-rw-r--r--  1 user1 www-data 6.5K 2011-10-26 21:53 authorize.php
-rw-r--r--  1 user1 www-data  61K 2011-10-26 21:53 CHANGELOG.txt
-rw-r--r--  1 user1 www-data  996 2011-10-26 21:53 COPYRIGHT.txt
-rw-r--r--  1 user1 www-data  720 2011-10-26 21:53 cron.php
-rw-r--r--  1 user1 www-data 174 2011-10-26 21:53 .gitignore
-rw-r--r--  1 user1 www-data 5.1K 2011-10-26 21:53 .htaccess
drwxr-xr-x  4 user1 www-data 4.0K 2011-10-26 21:53 includes
-rw-r--r--  1 user1 www-data  529 2011-10-26 21:53 index.php
-rw-r--r--  1 user1 www-data 1.5K 2011-10-26 21:53 INSTALL.mysql.txt
-rw-r--r--  1 user1 www-data 1.9K 2011-10-26 21:53 INSTALL.pgsql.txt
-rw-r--r--  1 user1 www-data 688 2011-10-26 21:53 install.php
-rw-r--r--  1 user1 www-data 1.3K 2011-10-26 21:53 INSTALL.sqlite.txt
-rw-r--r--  1 user1 www-data  18K 2011-10-26 21:53 INSTALL.txt
-rw-rw-r--  1 user1 www-data  18K 2011-09-17 23:50 LICENSE.txt
-rw-r--r--  1 user1 www-data 7.5K 2011-10-26 21:53 MAINTAINERS.txt
drwxr-xr-x  4 user1 www-data 4.0K 2011-10-26 21:53 misc
drwxr-xr-x 42 user1 www-data 4.0K 2011-10-26 21:53 modules
drwxr-xr-x  5 user1 www-data 4.0K 2011-10-26 21:53 profiles
-rw-r--r--  1 user1 www-data 3.5K 2011-10-26 21:53 README.txt
-rw-r--r--  1 user1 www-data 1.6K 2011-10-26 21:53 robots.txt
drwxr-xr-x  2 user1 www-data 4.0K 2011-10-26 21:53 scripts
drwxr-xr-x  4 user1 www-data 4.0K 2011-10-26 21:53 sites
drwxr-xr-x  8 user1 www-data 4.0K 2011-10-26 21:53 themes
-rw-r--r--  1 user1 www-data  18K 2011-10-26 21:53 update.php
-rw-r--r--  1 user1 www-data 9.3K 2011-10-26 21:53 UPGRADE.txt
-rw-r--r--  1 user1 www-data 2.1K 2011-10-26 21:53 web.config
-rw-r--r--  1 user1 www-data  417 2011-10-26 21:53 xmlrpc.php

You can repeat this command to give ownership to the other files and folders in your Drupal installation. For example, you will often need to create a '/files' folder within the 'default' folder; mydrupalfolder/sites/default/files

Firstly navigate to 'mydrupalfolder/sites/default/' and then create the '/files' folder as follows:

sudo mkdir files

..then assign ownership

sudo chown user1:www-data files


Change Only the Owner or the Group

The commands above showed you how to change the owner and group simultaneously. The following examples detail how to change only the owner or only the group separately:

Change the 'owner' of the 'sites' folder to 'user1'
sudo chown user1 sites
Change the 'group' of the 'sites' folder to 'www-data'
sudo chgrp www-data sites

These commands should help you to set up your Drupal installation and as mentioned above if you want to assign permissions, see our blog on the numerical permissions commands for Drupal and Linux.

We hope this brief tutorial is of some help to those setting up websites and the Drupal CMS on Linux servers. Feel free to leave a comment and let us have your questions or feedback.

Home Blogs knooq.com's blog