How do I change permissions recursively on Linux?

Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

Folders and files need different permissions, so give them different numbers. Use 755 for folders and 644 for files, set with two find commands, and then set the owner and group with chown -R. A single chmod -R with one number is wrong for one of the two kinds. chmod -R 777 is wrong for both.

What the numbers mean

Every file has three sets of permissions: for the owner, for the group and for everyone else. Each set is a sum: 4 for read, 2 for write, 1 for execute. So 7 is read, write and execute, 6 is read and write, 5 is read and execute, and 4 is read only.

On a file, execute means “run it as a program”. On a folder it means “enter it and reach what is inside”. Read on a folder lets you list the names, and write lets you create and remove entries. A folder without execute is a locked door, whatever its contents are.

A tree that someone set to 777

The web root /var/www/site holds a few files. A previous admin ran chmod -R 777 on it to stop an error. This lists the mode and the name of every item:

$ cd /var/www
$ find site -printf '%m %p\n' | sort -k2
777 site
777 site/.env
777 site/css
777 site/css/app.css
777 site/deploy.sh
777 site/index.html
777 site/js
777 site/js/app.js

Everyone may write everywhere. The account www-data stands for the web server, or for any program that runs as it. Any bug or break-in in that program can now rewrite your files:

$ sudo -u www-data sh -c 'echo defaced >> site/index.html'
$ cat site/index.html
<h1>Hello</h1>
defaced

Set folders and files separately

find -type d picks folders and -type f picks plain files. Each gets its own number:

$ find site -type d -exec chmod 755 {} +
$ find site -type f -exec chmod 644 {} +
$ chmod u+x site/deploy.sh
$ find site -printf '%m %p\n' | sort -k2
755 site
644 site/.env
755 site/css
644 site/css/app.css
744 site/deploy.sh
644 site/index.html
755 site/js
644 site/js/app.js

The + at the end of -exec hands many names to one chmod, which is faster than starting it for each. The script is the one file that needs execute, so chmod u+x adds that bit for the owner alone. The letters u, g and o mean owner, group and others, and + and - add and remove bits.

The same write now fails for www-data:

$ sudo -u www-data sh -c 'echo defaced >> site/index.html'
sh: 1: cannot create site/index.html: Permission denied

One more point: the list shows 644 site/.env. A file with a password in it should be 600, or 640 if its group has to read it. On a machine with several users, 644 lets all of them read it. The chown section below sets it right.

Why chmod -R 644 breaks things

The next idea is chmod -R 644 site, one number for everything. It removes execute from the folders, and chmod itself then cannot go in:

$ chmod -R 644 site
chmod: cannot access 'site/index.html': Permission denied
chmod: cannot access 'site/js': Permission denied
chmod: cannot access 'site/.env': Permission denied
chmod: cannot access 'site/css': Permission denied
chmod: cannot access 'site/deploy.sh': Permission denied

The command changed site first, and then it could not reach anything below it. Now not even the owner can read a file in the tree:

$ cat site/css/app.css
cat: site/css/app.css: Permission denied

The file itself is fine. namei -l walks the path and shows the mode of every part:

$ namei -l site/css/app.css
f: site/css/app.css
drw-r--r-- dev dev site
                    css - Permission denied

site is drw-r--r--, with no x for anyone. That is the whole problem. When you see a permission denied for a file that looks right, run namei -l on its path and read the folders.

The short form with a capital X

You can repair the tree with one command. In chmod, a capital X means “execute only if it is a folder, or if someone already has execute”:

$ chmod -R u=rwX,go=rX site
$ cat site/css/app.css
body { margin: 0 }

u=rwX gives the owner read and write, and execute on folders. go=rX gives the group and others read, and execute on folders. Files that were not executable stay without it. Take care with files that already have an execute bit: from 777 they would keep it, and that is why the find pair above is the safer way to clean up a tree.

chown -R: the owner and the group

chown -R changes who owns a tree, and it needs root. A common setup is the deploy account as owner, so it can write, and the web server’s group, so the server can read. The earlier commands left .env readable by everyone, and the X repair did it again. With the group set, 640 is enough for the server, so the last command closes it:

$ sudo chown -R dev:www-data site
$ chmod 640 site/.env
$ stat -c '%U:%G %a %n' site site/index.html site/.env
dev:www-data 755 site
dev:www-data 644 site/index.html
dev:www-data 640 site/.env

By default chown -R does not follow symbolic links to folders (-P). The option -L makes it follow every one it meets, so leave it off. A link inside the tree is not followed, but without -h the owner of the file it points to still changes. Add -h to change the link itself.

Be careful with the pattern .*, which looks like “all hidden files”. In sh on this system it also matches . and .., the current and the parent folder:

$ sh -c 'cd site && echo .*'
. .. .env

chown -R user .* would then change the parent as well, and everything below it. Name the files you mean, or work from the parent with the folder’s name.

Mistakes people make

You run chmod -R 777 to clear a permission denied. It hides the cause, opens the files to every account, and on a key folder it makes sshd ignore your keys, as the SSH keys article shows.

You run a recursive command on the wrong directory. Check pwd, and give the full path. A chown -R on /var or /etc breaks services and logins.

You copy files with a tool that does not keep the modes. Copying files over SSH keeps times and mode bits with scp -p, and rsync with -a keeps permissions and times, and the owner when it runs as root.

You change the mode of a file when the folder is the problem. Look at the folders first.

Valakes stops chmod -R 777 / on a High risk card before it runs, and shows a line under the composer for chown -R. The check reads the command you type, so it does not see inside a script. Feature: Command check

Questions

What do the three digits of 755 mean?

The first is the owner, the second the group, the third everyone else. Each digit adds 4 for read, 2 for write and 1 for execute. So 7 is read, write and execute, and 5 is read and execute.

Is 755 and 644 always right?

It is a good default for a web root or a project folder that several accounts read. Private files such as keys and .env files want 600, and their folders 700. A folder that a group writes together wants 775 and files 664.

What is the difference between chmod and chown?

chmod changes who may read, write or run a file. chown changes who owns it, and which group it belongs to. Only root can give a file to another user.

Why does ssh complain about the permissions of my key?

ssh refuses a private key that other users can read, and sshd ignores an authorized_keys file that other users can write. The SSH keys article lists the right modes.

References

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.