Home > Linux > Chmod Recursive Guide: How to Change Permissions in Linux

Chmod Recursive Guide: How to Change Permissions in Linux

chmod recursive guide text

ServerMania offers a variety of HybridCloud, and Dedicated Linux servers which all make use of the chmod command. It’s a frequently used command, so it’s important that any system admin knows how to use it.

In this article, we’re going to cover; what is chmod, how is it used, and what things to avoid.

What is chmod? Chmod, also known as change mode, changes the file mode bits of a certain file. You can change it with an actual number representation or just symbolically.

Except for changing permissions for files and directories, you are also able to modify special mode flags, like SGID, SUID, and sticky bit mode. It is present in Unix-like and Linux operating systems since the 70s.

One of the most essential parts of any Linux system administrator is setting up correct permissions. Setting up wrong permissions can allow hackers to execute malicious code to your server and harm it. Unless needed, it is highly advised that other users are restricted to read and execute files on your system.

If you’re looking for the best Linux distributions or Linux dedicated servers, make sure you visit our website and more about it!

In this article, we will cover the meaning of chmod, how it is used, what are the things you should avoid doing, and some general tips when it comes to chmod.

File Permissions

If you’re managing Linux servers or you’re using Linux as your main operating system, you will most likely come across the “Permission deny” error on your screen when you try to create or edit a file. These errors that occur due to insufficient permission can be handled by setting the right file permissions. Linux is controlled through ownership, attributes, and file permissions. These file permissions ensure that only authorized users can access files and directories.

ServerMania offers a variety of Hybrid, Cloud, and Dedicated Linux servers which all make use of the chmod command. It’s a frequently used command, so it’s important that any system admin knows how to use it.

Files and Directories

You can change all of the files and directories with the Linux chmod command. Sudo is the preferred way of editing system files. This is because sudo allows you to keep the system context.

To change directory permissions in Linux, use the following commands: chmod +rwx filename to add permissions; chmod -rwx directoryname to remove permissions; chmod +x filename to allow executable permissions; and chmod -wx filename to take out write and executable permissions.

Make sure you note that “x” is for execute, “r” is for read, and “w” is for write.

Chmod R

When you’re working on a folder directory, you probably don’t need to change the permission of one single file. Often, people need to change the permission on that file, all of its subdirectories, and all the files in the current folder.

You can change permissions of files using numeric or symbolic mode with the chmod command. Use the chmod command with the R (recursive) option to work on all directories and files under a given directory.

To change the file permissions, the general syntax is:

chmod -R MODE DIRECTORY

For instance, you can the symbolic method by typing:

chmod -R u=rwx,go=rx /var/www/html

Or, if you want to change the permissions of all files and subdirectories under the /var/www/html directory to 755 you would use:

chmod -R 755 /var/www/html

The permissions of a file can be changed only with the user with sudo priviledges, or the file owner. When you chmod recursively change the files’ permissions, you need to be really careful.

Type F Exec Chmod

Directories and files shouldn’t have the same permissions. In order to change into directories, you must set execute permissions. Recursively changing the website file’s permissions to 644 and the directory’s permissions to 755 is the most common scenario.

Using Different Types of Methods

This can be done by using the numeric method, with f exec chmod 644 or exec chmod 755, more precisely:

find /var/www/html -type d -exec chmod 755 {} \;

find /var/www/html -type f -exec chmod 644 {} \;

It can also be done by using the symbolic method:

find /var/www/html -type d -exec chmod u=rwx,go=rx {} \;

find /var/www/html -type f -exec chmod u=rw,go=r {} \;

Then, files and directories are searched by the file command under /var/www/html, and found files and directories are passed to the chmod command to set the permissions.

The chmod command is run for each found entry when you use find with -exec. This operation can be sped up by using the xargs command, which passes multiple entries at the same time:

find /var/www/html -type d -print0 | xargs -0 chmod 755

find /var/www/html -type f -print0 | xargs -0 chmod 644

We want to offer you some extra advice and tips on chmod. Firstly, in order to list directories and subdirectories, make sure that you remember that you need read permissions. Secondly, all files in a directory or folder can be set to writeable with chmod -R 775 [directory]. And lastly, with the find command, you can apply permissions independently to files and directories.

One chmod operation that is advised to be generally avoided since it can cause some pretty big security issues on your server is the chmod 777 operation.

This operation gives full permission to anyone who accesses your server, and it is the reason why this operation should be avoided.

It is better to just use the chmod 775 command since it does not have a significant security risk.

When it comes to your permission, you should also avoid using 664. In order to access it, you will need to execute permissions on a directory, and removing that permission will break whatever you apply it to.

Conclusion

Use the chmod command in combination with the find command in order to recursively set permissions on directories. Chmod recursive lets you recursively change the permission of a certain file.

As you can see from the article, there are several options you can choose from when it comes to permissions. The usability among users is dictated by you. Even though giving permission to everyone sounds like an easier choice, it isn’t the wisest one. So make sure you choose wisely when it comes to permissions.

Chmod helps out a lot with the sticky situations that file permissions can sometimes cause. You can truly take ownership of your files and directories once you know the procedure for changing permissions.

Complete Digital Server Solutions For All

Bare Metal

Dedicated Servers

A single tenant, physical server allowing you full access to its resources.

Professional-grade

Hybrid Servers

Virtualized server platform hosted on enterprise-grade physical servers.

Affordable & Secure

Cloud Servers

High-performance and highly-available infrastructure, delivering a reliable, secure, and scalable solution.

  • You forgot to mention ‘a’ in ‘Who’, which is short for ‘all’ – meaning it’s a combination of user, group, and other.