How to Change File Permissions in Linux

Every file and directory on your Linux system has permissions assigned to it. These permissions are what determines which users are allowed to read, write to, and/or execute the file. In this tutorial, you will learn about how to change Linux file permissions with the chmod command.

Changing File Permissions – Symbolic Mode

There are three types of permissions, read, write, and execute. The most user-friendly way of adding or removing permissions from a file or directory is with the chmod command and the +rwx or -rwx syntax (this is called symbolic mode) as shown in the examples below.

Example 1. Add read, write, and execute permissions to a file:

$ chmod +rwx script.sh

Example 2. Add execute permissions to a file:

$ chmod +x script.sh

Example 3. Remove all permissions from a file:

$ chmod -rwx script.sh

Example 4. Remove write permissions from a file:

$ chmod -w script.sh

Example 5. Use the -R option to change the permissions for all files recursively.

$ chmod +rw -R /path/to/dir

Example 6. Add execute permissions for the owner of a file (u is for user):

$ chmod u+x script.sh

Example 7. Add read and write permissions to the group owner of the file (g is for group):

$ chmod g+rw script.sh

Example 8. Remove all permissions for any user that is not owner or in the group (o is for other):

$ chmod o-rwx script.sh

Example 9. Give read permissions to all users (a is for all):

$ chmod a+r script.sh

Example 10. Rather than adding or taking away permissions, we can also use = to set the exact permissions we want. To give the group read and write permissions:

$ chmod g=rw script.sh

Viewing File Permissions

Running the ls -l command on a file or directory will reveal everything you need to know about its current permissions.

$ ls -l script.sh

The output you see will look something like this:

-rwxrw-r-- 1 linuxnightly admins 42 Jan 20 14:19 script.sh

The first character of the output tells us what type of file it is.

  • - = regular file
  • d = directory
  • l = symbolic link

You may see other file types throughout your system, but directories and regular files will comprise the vast majority of what you find.

The next set of characters we see are rwxrw-r--. These nine characters can be split into three different blocks. Each block of characters represents the permissions for a different user or group. In our example:

  • rwx = user permissions
  • rw- = group permissions
  • r-- = other permissions

What each permission means:

  • r = read permissions
  • w = write permissions
  • x = execute permissions
  • - = no permissions

The next data we see is the user account who owns the file and the user group that has permissions on the file. In our example:

  • linuxnightly = owner
  • admins = group

In many cases, the owner and group of a file are the same value. Let’s look again at the permissions of our file:

-rwxrw-r-- 1 linuxnightly admins 42 Jan 20 14:19 script.sh

From this data, we can determine that user ‘linuxnightly has rwx (read, write, and execute) permissions on the file, the ‘admins’ group has rw- (read and write) permissions, and other users (those that are neither the owner nor in the group) have only r (read) permissions.

Changing File Permissions – Absolute Mode

Absolute mode uses numbers to represent permissions. It’s a little less user-friendly, but can be more succinct. It accomplishes the exact same thing as symbolic mode shown above.

In order to understand absolute mode, you’ll need to think of each permission (read, write, execute) as a number. Check out the table below.

Number Permission Symbol
0 No Permissions ---
1 Execute --w
2 Write -w-
3 Write + Execute -wx
4 Read r--
5 Read + Execute r-x
6 Read + Write rw-
7 Read + Write + Execute rwx

If Execute=1 and Write=2, then 3 (the sum of those numbers) would give Execute + Write permissions. With this in mind, all you really need to know is that Execute=1, Write=2, and Read=4. To combine different permissions, just add those numbers to each other.

Example 1. This command would give a file rwxr-xr-- permissions:

$ chmod 754 script.sh

Explanation:

  • 7 = 4+2+1 = rwx
  • 5 = 4+1 = r-x
  • 4 = 4 = r--

Example 2. If you need to change the permissions of all files in a directory, you can use the -R option for recursive.

$ chmod -R 750 /path/to/dir

How to Change Owner and Group

Changing the permissions for the file owner and group is one thing, but what if we want to change the owner or group of the file? This goes hand-in-hand with changing the file permissions and can be accomplished with the chown and chgrp commands, respectively.

Example 1. Change the owner of the file to user ‘linuxnightly’:

$ chown linuxnightly script.sh

Example 2. Change the group of the file to ‘admins’:

$ chgrp admins script.sh

Example 3. The chown command can also change the owner and group of a file simultaneously. Separate your user and group with a : character.

$ chown linuxnightly:admins script.sh

Example 4. If you need to change the owner and group of all files in a directory, you can use the -R option for recursive.

$ chown -R linuxnightly:admins /path/to/dir

Conclusion

File permissions in Linux allow us to restrict or grant access to files and directories in a very straightforward way. Depending on your preference, you can opt for symbolic mode or absolute mode when changing file permissions with chmod, though we find the symbolic mode a little easier to work with. The chown and chgrp commands are also essential when working with file permissions in order to change the owner and group of files or directories.

Leave a Comment

Your email address will not be published. Required fields are marked *