Ubuntu Sever – How to set file owner for all files and folders

Problem

If you find you can’t edit WordPress theme and plugin files from the wp-admin – it’s likely the file owner on the server is not set correctly.

Solution

This often happens when the files have been created outside of WordPress, for example by copying over using FTP.

Step 1: Confirm the problem is the file owner

To confirm this problem, open the directory using your preferred FTP application – such as Filezilla – and look at the ‘Owner/Group’ column.

If you see ‘root root’ – the problem is file permissions. The web server will not be able to edit files owned by root.

Step 2: Determine the correct file owner

In my case, I can see the correct owner is ‘www-data’.

This will be used in step 4.

Step 3: Note the WordPress directory path

For example, ‘var/www’

This will be used in step 4.

Step 4: Update the file permissions using the server terminal

Log into the server terminal – I do this using the Putty application.

If you do not know how to do this, talk with your server host.

Run the following command, which recursively updates file owner permissions.

Note – you will need to replace the file owner and WordPress directory path for your server.

sudo chown -R www-data:www-data /var/www

How does this work?

This Ubuntu Server terminal command uses the sudo chown command to change the owner and group of the /var/www directory and all of its subdirectories.

Here is how the command works in more detail:

  1. The sudo command is used to run the following command with superuser (i.e., administrator) privileges. This is necessary because the /var/www directory and its subdirectories are owned by the system and cannot be modified by a regular user without superuser privileges.
  2. The chown command is used to change the owner and group of a file or directory. This command takes the following syntax:
  3. chown [options] [owner][:group] [file or directory]
  4. In this command, the -R option is used to specify that the owner and group will be changed recursively for the /var/www directory and all of its subdirectories.
  5. The www-data:www-data part of the command specifies the new owner and group for the /var/www directory and its subdirectories. In this case, the owner and group are both set to www-data, which is the user and group that are typically used for web server files and directories in Ubuntu.
  6. The /var/www part of the command specifies the path to the directory whose owner and group will be changed.