Tuesday, March 19, 2024
HomeA-Z CommandsEverything You Need To Know About Linux Chmod Command

Everything You Need To Know About Linux Chmod Command

If you are familiar with the Linux filesystem, you know that all the system files are stored inside the root (/) directory. Linux reserves some specific file system permission only for the root user. The main difference between the filesystem of the Linux-based operating system and other operating systems is that Linux reserves some specific file system permission. If you are not a root user, you can’t delete, move, or modify root files from your system. For example, if you have a localhost server running on your Linux system, you can’t move the server configuration files without the root permission. Even you can’t change the configuration settings without the root permission. To change the filesystem status, you need to know the chmod commands. The chmod command allows you to read, write, and execute files on a Linux system.

Chmod Command on Linux


Linux is not only used for reliability and security; it is also used for multifunctional purposes. You can run servers, multi-user activities, and so more. The problem arrives when you assign a user to your system but doesn’t give the root access. Sometimes, that user might need to do some superuser work to configure and make the environment functional.

There comes the notion of the chmod command for Linux. As I said earlier, Linux is not only used for multitasking or multifunctional work; it is also used for multi-user. The chmod command can modify the read, write, and execute tasks with just a root password on a Linux system.

In the entire post, we will discuss the differences between the chmod 777, chmod 755, or chmod 600, and more other chmod commands on the Linux system.

If you are a Linux enthusiast user, you can follow this link to get more detailed formations about the chmod command on Linux. In the following link, you would find a table as showing in the picture below. You need to put the value of the specific permission. The table will automatically show you which type of permissions are defined by the specific commands.

website of chmod ss64

Know the Current Permission Status of a File/Directory on Linux


Now, there are methods to check the permission details of a file or a directory in Linux. You can either use the GUI method or the CLI method to check the permission status of a file. Here, we will learn both of the methods of how to know the current permission status of a file on our system.

Method 1: Use the GUI method to check the permission Status


Using the graphical user interface method is the easiest method to check any details of a file on Linux. You just need to select the file and right-click on the file. From the drop-down menu, you will find the Properties option. Select the Properties option that will open a new dialogue box.

From the dialogue box, click on the Permission menu. There you will find the detailed permission status information in the file. You can see the owner info, access records, group info, and the security content of that file.

chmod in linux permission check

Method 2: CLI method to know the Permission Status of a File


Linux power users always want to use the CLI method to complete any task. Well, here it is, you can check the read, write and execute status of any file or directory from your Linux terminal shell. In this method, we will use the long list (ls -l) commands to get the permission status.

You can have an idea by watching the following terminal command-lines given below about getting the permission status of any file using the terminal command-line interface.

cd Documents
ls
ls -l sampledata.zip

Here, you can see that the output result starts with a dash(-), which means the sample is a file, not a directory. The directory symbol starts with d. Then, the rw signifies the read and write permission of that file.

CLI permission check Linux Chmod

To run a demonstration, you can run the following command-lines given below to create a new file and see that file’s permissions.

touch newfile.txt
chmod g+w newfile.txt
ls -og newfile.txt

touch chmod on linux

In the above picture, you can see that the permission is written as -rw-rw-r-- 1; here the rw symbol means that the file has both read and write permission. And the numeric sign 1 means that the user has permission to execute the file. The basic numeric permission syntaxes are explained below.

  • 0 = The user has permission to read, write, and execute the file.
  • 1 = The current user has permission to execute the file.
  • 2 =  User the permission to write the file.
  • 3 = The user has permission to write and execute the file.
  • 4 = User has the read-only permission.

Understanding the Syntaxes and the chmod Command on Linux


The chmod command has a few syntaxes that you need to know to understand the output of the command. Here, I’m enlisting explanations of the very basic syntaxes of the Chmod command on Linux. In this stage, we will also learn a few primary chmod commands that you might need to use daily.

  • u The u syntax mentions the user who owns the file or the directory.
  • g The g syntax mentions the group where the file belongs.
  • o The o syntax mentions that the file is owned by all the users.
  • a The a syntax mentions that the file is owned by all users and groups.
  • r The r syntax mention that the file has read-only permission.
  • w The w syntax mentions that it has permission to write the file.
  • x The x syntax mentions that the current user has the authority to execute the file.
  • The syntax mentions that the user has permission to drop the file.

1. chmod -R 755: Modify Authority on an Entire Directory


The chmod 755 is often used as the -R 755 in the Linux shell to modify the filesystem’s permission. You can run the chmod 775 commands on your Linux terminal shell if you cant write or remove files from any directory. The chmod -R 775 command has the power to change the permission for an entire directory instead of a single file.

chmod -R 755 directory
sudo chmod -R 755 /var/www/html/

In the picture below, you can see that the log-list (ls -l) output has already changed the read, write, and execute permission for the directory.

chmod on Linux 755

2. chmod 777: Allow Permissions for All Users


Here we will see the use of the chmod 777 commands on the Linux system. Basically, all the chmod commands are associated with the Linux filesystem. To better understand the chmod commands, I will recommend you also know the Linux filesystem hierarchy.

However, the chmod 777 commands are used on Linux to write and execute the file. The following terminal commands can help you get a basic idea of how the chmod 777 command works on Linux.

chmod 777 filename
sudo chmod 777 /var/www/
sudo chmod -R 777 /var/www/

chmod command 777 on Linux

In the picture above, you can see that the output starts with the dr syntax, and it has the wxr syntaxes along with it, which means that the target path is a directory and it has the write, execute, and read permission. At the end of the output, the numeric value 3 indicates that the current has the permission to write and execute the directory.

3. chmod +x : Permission to execution File/Directory


Here comes the riskiest chmod command for Linux. If you are a newbie on the Linux system, I would not recommend using the chmod +x command on your system. Basically, the chmod+x command is used to execute the file or to kill the process. With superuser power, you can run the chmod+x command to destroy your entire system.

Here are a few chmod+x commands for the Linux systems that you can follow for better understanding. In the output value, the numeric sign 1 symbolizes that the current user has permission to execute the file.

sudo chmod +x /path/to/file
sudo chmod a+rx /usr/local/bin/composer

execute chmod command on linux

4. chmod 755: Allow the Root User to Read and Write Files on Linux


Previously, we have seen the use of the chmod -R 755 command for Linux systems; but now we will see the usages of chmod 755 on a Linux system. The main difference between the chmod -R 755 and the chmod 755 is, the -R 775 allows all users to modify the entire directory, where the 775 command only allows the root user to read and write the filesystem.

You can follow the following methods to run the chmod 755 commands on your Linux terminal shell.

chmod 755 /path/to/file
chmod 755 /usr/local/bin/certbot-auto
chmod 755 /home/UbuntuPIT/New_TES/code/SG_V1.0/Voice-data.pl

5. chmod 700: Allow Read, Write, and Execution Permission for the Owner


If you are the owner of your Linux system and still can’t write or execute a specific file from your filesystem, that might be occurred due to not having the right authorization to the system. Why would I need to get permission to execute files from my own Pc while I am the root user?

Well, you might already know that the Linux filesystem doesn’t work like Windows or other operating systems. Linux wants to make sure that you know what you’re doing. That’s why despite being the owner, you might need to get the chmod 700 permission to execute a specific file from your Linux system.

You can see the following terminal commands below to understand how the chmod 700 command works on Linux.

chmod 700 /path/to/file
sudo chmod 700 /etc/ssl/private

change mode 700

In the picture above, you can see that the chmod is run as a root user, and you can’t see the long-listed output (ls-l) result without the root privilege.

6. chmod 666: Disable Execution for all User on Linux


As a Linux sysadmin, I personally found the chmod 666 commands very interesting. This command is very helpful for those who have to work remotely from one system to another. Sometimes clients get mismatches with the filesystem. You can add the chmod 666 rule for all the directories so that the naive users cant mismatch with the filesystem.

Let’s back to the point; in Linux, the chmod 666 disables the execution permission of files or directories for all users. The chmod 666 commands only allow users to read and write files. You can see the terminal command-lines given below to get a better idea of how the chmod 666 commands work on a Linux system.

sudo chmod -c 666 /path/to/file

7. chmod 644: Access Read-only Permission for All Users


If you are a sysadmin or the owner of a local FTP server, this command will help you. You can set rules for the user or the visitors to only view and download the file. In this way, your files will be kept safe, and you can share them among many users.

In Linux, the chmod 644 command works for both files and directories. You can set the chmod 644 commands in any Linux filesystem, server, or media player server like Plex or Emby. Here is an example of how you can run the chmod 644 commands on a Linux system.

sudo chmod 644 /path/to/file

8. chmod 600: Allow Read-Write, But No Execution


Suppose you work for a multinational company to work in a local area network server to share documents or files with your fellow worker. In that case, you might not want to give them permission to delete any files from your personal directory.

To solve this issue, Linux users can use the chmod 600 commands. The chmod 600 command allows users or clients to read and write the file and directories. But it doesn’t allow them to delete or execute the directories. No one but only you can execute files from your system in a chmod 600 protected system.

sudo chmod 600 /path/to/file/

change mode 600 command

Extra Tips – 1: Use Chmod Command to Install Packages on Linux


Till now, we have seen very few basic chmod commands for Linux distributions to access or deny permissions to the filesystem. But did you know there is also a use of the chmod command on the Linux system? Yes, most often, you might need to run the chmod commands to install a package on your Linux system.

After downloading the binary package file of an application, you directly install it on your Linux system by the chmod commands. Here is an example of how the chmod commands look like when installing packages via the chmod command on your Linux system.

$ chmod +x install.sh
$ sudo ./install.sh

Extra Tips – 2: Use Chmod Command to Handle the Rookie Mistakes on Linux


As the chmod command is very powerful in Linux, you should handle this command very carefully. Just because you have the root privileges, you just can’t run the chmod anyway to any directory. Running the chmod command without understanding can cost you a lot. Now, we will see a few issues that might occur if you run the chmod commands carelessly on your Linux system.

Case 1: Coincidentally Run the chmod 655 Command and Can’t Do Superuser Works


If you run the chmod 655 to your root(/) directory, there is a chance that you might no longer have root access over your Linux filesystem. If you have already made that mistake, there are no worries; there is a method to recover your system’s root permission.

You need to get a live USB disk or a CD of the Linux operating system, then insert it and go to the live session mode. Then run the following chmod command on your terminal shell to get your root privileges back.

sudo chmod /path/to/root/ 755

Case 2: Add Permission After Dropping the Permission


Every so often, there could be a mistake of disallowing permission to one directory instead of another directory. In that case, you might temporarily lose access to the directory. To solve problems like this, first, you need to fix the directory that you accidentally executed. You can obey the command line given below to understand how the chmod command works on Linux.

sudo chmod a-x /directory_that_you_accidentally_dropped

Now, run the following chmod command on your Linux terminal to fix the directory.

sudo chmod a+X /directory_that_you_accidentally_dropped

Case 3: Permission Deny to Log in to a Linux server


If you a Linux server admin, and you run the chmod 444 commands to your server admin path, there is a chance that you might not be able to access your server anymore. In that case, you need to run the following chmod 555 commands in the terminal shell to resolve the issue.

sudo chmod 555 <directory_path>

Here is one more tip, if you run the chmod 000 commands in any directory, none but the root user can read and write that directory.

Final Words


In Linux, the chmod commands are very helpful when you get stuck with the filesystem’s permission. As a Linux sysadmin, you need to know all the primary chmod commands on Linux. In the entire post, I have described the most used Linux chmod command. I have also shown how to save your system from the rookie mistakes of the chmod command. If you need more help with the chmod command, you can always open your Linux terminal and chmod --help.

If you like this post and find this post helpful, please share it with your friends and the Linux community. You can also mention if I have missed any essential chmod commands. We also encourage you to write down your opinions regarding this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now