What is chmod? How to use chmod for WordPress File Permissions

As an experienced webmaster managing WordPress sites for over 15 years, I‘ve seen how file permissions issues can quickly bring a site to its knees. But by understanding how the chmod command works, you can take control and resolve these problems yourself.

Let me explain what chmod is all about and how to use it to fix file access issues in WordPress.

Why File Permissions Matter for WordPress

WordPress is a powerful content management system, but that power comes with some risks. If the files that drive your WordPress site are readable and writable by the public, it opens the door for hackers to steal data or inject malicious code.

That‘s why the default file permissions on most web servers lock down access so only the owner (and sometimes group) users can modify files.

But WordPress still needs the ability to write to certain files and folders, like when updating plugins or adding images to your media library. If the permissions are too restrictive, you‘ll get frustrating errors like:

Unable to create directory wp-content/uploads/2023/01. Is its parent directory writable by the server?

So understanding chmod and file permissions allows you to fine-tune access for a secure yet functional WordPress site.

Permissions in Linux/UNIX Explained

In Linux and UNIX-based systems like macOS or Linux web hosting accounts, every file and folder has an assigned owner and group owner. Then there are three types of permissions:

Read: View and list file contents
Write: Modify, edit, delete the file
Execute: Run a file or access a directory

These permissions are assigned separately to the owner, group, and all other users (called "world" or "others").

When you run ls -l to view permissions, you see a set of rwx flags like:

-rwxrw-r--

Let‘s break this down:

  • The first rwx is for the file owner
  • The second rw- is for the group
  • The third r– is for all other users

Each r, w, or x means that permission is granted. A dash – means it‘s denied.

So in this example, the owner can read, write, and execute the file. The group can read and write but not execute. And others can only read.

This allows selectively granting access to only those who need it!

What Does the chmod Command Do?

chmod stands for "change mode" and it allows you to modify the permissions settings on files and folders.

The basic syntax is:

chmod [options] mode file

Where:

  • mode specifies the new permissions, defined in two ways: numeric or symbolic
  • file is the file or directory to modify

For example:

chmod 644 wp-config.php

chmod g+w uploads  

There are two formats you can use for the mode:

Numeric Mode

This uses an octal number (base 8) by combining permission values:

  • r = 4
  • w = 2
  • x = 1

To get the numeric mode, you add up the values for each user type:

  • Owner (rwx) = 4 + 2 + 1 = 7
  • Group (rw-) = 4 + 2 + 0 = 6
  • Group (r–) = 4 + 0 + 0 = 4

So a mode of 764 sets read/write/execute for owner, read/write for group, and read for others.

Symbolic Mode

This uses letters to explicitly give permissions:

  • u = user/owner
  • g = group
  • o = others
  • a = all

Some examples:

  • chmod g+w – Add write permission for group
  • chmod a-x – Remove execute permission for all
  • chmod o=rx – Set others permissions to read + execute

See man chmod for more details on both numeric and symbolic modes.

Using chmod to Fix File Permission Issues in WordPress

Now that you understand how file permissions work, let‘s look at some examples of using chmod to resolve access issues in WordPress.

A few common scenarios where permissions might need adjustment:

  • Can‘t edit wp-config.php: This file contains sensitive data and some hosts lock it down. To allow WordPress to modify it, do chmod 664 wp-config.php to add owner write permission.

  • Can‘t install plugins/themes: The wp-content folder may be restricted. Try chmod 755 wp-content to add execute access.

  • Failed uploads: The uploads folder needs write and execute permission. Do chmod 775 wp-content/uploads.

  • White screen of death: If files like .htaccess become unreadable, WordPress crashes. Check with chmod 644 .htaccess.

The goal is to grant the minimum permissions needed for WordPress to function. I recommend incremental changes, like adding write permission only for the owner. Too permissive (like 777) is risky.

Troubleshooting File Permission Problems

If you‘re seeing odd errors or WordPress failing to modify files, a few steps to diagnose:

  • Use ls -l to view permissions and ownership of files. Look for issues.

  • Try operations like editing wp-config.php directly via SFTP or SSH to test.

  • Check your web host‘s guidance on recommended file permissions.

  • Scan files with a tool like Wordfence to detect malicious code injections.

Adjusting permissions with chmod should resolve most problems. But if not, ensuring your file ownership matches the expected user and group may help.

I hope this guide gives you a solid grasp of how file permissions work and how to use chmod to fine-tune access for a smooth WordPress experience. Let me know if you have any other questions!

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.