Part of Datorkirurgen AB – IT experts across Sweden

File permissions in Linux control who can read and change the web files. This is how we set owners, 755 and 644 and protect wp-config.php on a web server.

Why permissions matter so much

Many problems with WordPress and other web applications concern file permissions in Linux. If the web server cannot write in the right directory, updates and uploads do not work. If, on the other hand, it can write everywhere, an attacker who gets in can change any file. In other words, the right permissions are a balance between function and security. Therefore, we go through them when we take over a server.

In addition, every file in Linux has an owner, a group and three sets of permissions. They state what the owner, the group and everyone else may do. For example, the number 755 on a directory means that the owner may do everything, while others may read and open the directory. The number 644 on a file means that the owner may read and write, and others may only read. It sounds technical, but the principle is clear.

Common values for file permissions in Linux on a web server and what they mean
Common values for file permissions in Linux on a web server and what they mean. This is how Linux Service, part of Datorkirurgen AB, works for businesses in Täby, Danderyd and Lidingö.

A real fault: the wrong user for the web server

The screenshot in the article comes from a server where we corrected the ownership. An older command from another environment tried to set the owner to webuser:webgroup, but that user did not exist. The command failed with the error invalid user, since the user was missing. In fact, on Amazon Linux and the Red Hat family, the web server’s user is called apache. On Debian and Ubuntu, it is instead called www-data.

Once we had the right user name, we set the owner on the whole web directory. After that, we set 755 on all directories and 644 on all files with find. It is a safe baseline for most sites. Finally, we gave the configuration file stricter permissions, since it contains the database password. After that, the website worked as it should.

File permissions in Linux: the wrong user gives invalid user, then the right owner and find with 755 and 644
File permissions in Linux: the wrong user gives invalid user, then the right owner and find with 755 and 644. A real screenshot from our work. Addresses and the client’s directory names are masked.

The right baseline for WordPress

For WordPress, we recommend that the web server’s user owns the files if the site should update itself from the admin panel. The alternative is that a separate user owns the files and that updates happen with WP-CLI. It is more secure, but at the same time it requires more work. We choose the model according to how you want to work. The important thing is that the choice is deliberate and documented.

The wp-config.php file gets permission 640 or 600. The upload directory gets write permission, but PHP files there must never run. This stops a common attack where someone uploads a malicious file and then calls it. We add the rule in Apache, so that it applies even if someone changes something in WordPress. Read more about WP-CLI for safe updates.

A safe baseline for owners and permissions on a WordPress site
A safe baseline for owners and permissions on a WordPress site. An example from our work for clients in Stockholm.

Troubleshooting when something does not work

When WordPress asks for FTP details during an update, it often means that the web server cannot write in the directory. The solution is then the right owner, not opening the permissions for everyone. Unfortunately, we sometimes see sites where everything has 777. This means that anyone on the server can change the files, and therefore it is very dangerous. We fix such things immediately.

Another common problem arises when the wrong user creates files. If an administrator runs a script as root, new files get root as their owner. Then the web server can no longer change them. Therefore, we always run WP-CLI and similar tools as the web server’s user with sudo -u apache. In this way, the ownership is right from the start.

Checklist for troubleshooting permissions on a web server
Checklist for troubleshooting permissions on a web server. Linux Service, part of Datorkirurgen AB, helps businesses across Sweden with servers, scripts and databases.

SELinux: the extra level

On the Red Hat family and Amazon Linux, SELinux can also play a part. Even if the permissions are right, SELinux can deny the web server writing if the file has the wrong label. For example, this often shows after someone has moved files from another directory. We check the label with ls -Z and correct it with restorecon. We never switch off SELinux to solve such a problem.

For directories where the web server should write, we set a special label. As a result, it may write exactly there, but nowhere else. This gives strong protection even if an attacker were to get in via a vulnerable site. Read more about the protection in the article on hardening Linux.

Contact Linux Service about file permissions in Linux

Linux Service is part of Datorkirurgen AB and operates web servers with Apache, PHP and WordPress across Sweden. We correct ownership and permissions, and in addition we make sure that updates work securely. WordPress describes its recommendations in the documentation on file permissions.

Therefore, contact us if WordPress asks for FTP details or if uploads do not work. We carry out a review and fix the faults remotely. You always receive a short summary of what we have changed.

Frequently asked questions about file permissions in Linux

Questions about file permissions in Linux

File permissions in Linux for WordPress

More about SELinux and support

Website developed by Datorkirurgen AB · Privacy policy