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.

About ServerMania

ServerMania has over a decade of experience building high-performance infrastructure hosting platforms for businesses globally. We offer a wide range of fully customizable dedicated, hybrid, cloud, colocation and IP Transit services. Our mission is to empower clients by equipping them with fast, reliable, innovative infrastructure hosting while upholding a 100% network uptime SLA. This is assisted by a 24x7x365 rapid response team — one with some of the best response times in the industry.