Occasionally you may need to change the ownership or permissions of a file or provide access to files for a specific user or a group of users. Ownership of a file is important in determining how permissions are given. Every file and folder is owned by a user and a group. The primary owner of a file does not neccessarily need to be in the group which "owns" the file or folder.

This guide is to help change ownership and permissions in Linux using some of the most commonly used options.

Change Ownership (chown)

Changing ownership of files and folders is fairly straightforward and contains very few commonly used options.

The basic format for changing ownership is:

chown username:groupname fileorfoldername

If you only want to change the primary owner:

chown username fileorforldername

If you only want to change the group owner:

chown :groupname fileorfoldername

 Recursive

The previous command only changes the file or folder you specify. Frequently it is necessary to change all files and folders inside of a specific folder. In order to do that we need to tell chown to set ownership recursively. We can do this by using the -R option.

chown -R username:groupname foldername

The only major caviat when using the recursive option is symbolic links. If you are unsure of which option to use, check chmod --help.

 

Change Mode (chmod)

Changing file permissions can be confusing and tricky if you don't so it regularly. Permissions are set to the user (u), group (g), other (o), and all (a).

The types of permissions (modes) that a user, group, or other can be given are usually calculated using these sets of numbers.

4 (r) = Read

2 (w) = Write

1 (x) = Execute

Therefore if a user needs read (r) and execute (x) you will add 4 + 1 = 5. The resultant numerical value for the user will be 5. 

The possible calculated permissions are:

0 = none

1 = x = 1

2 = w = 2

3 = wx = 1 + 2

4 = r

5 = rx = 4 + 1

6 = rw = 4 + 2

7 = rwx = 4 + 2 + 1

The format for chmod can be difficult to navigate at first. It's important to understand that you can set permissions by defining the numeric value or specifying the user, group, or other and add or remove permissions using the letters associated with each mode.

chmod [options] mode fileorfoldername (options are optional)

 

Numeric value

chmod ugo fileorfoldername

chmod 777 fileorfoldername (this will give read, write, and execute to all users)

chmod 755 fileorfoldername (this will give read, write, and execute to the primary owner, but read and execute for all other users)

chmod 754 fileorfoldername (this will give read, write, and execute to the primary owner, but read and execute for all users in the primary group, and read permissions for all other users)

 

Letters

The operators in this instance would be =, +, and -. Plus (+) adds the mode, minus (-) removes the mode, and equal (=) defines the exact mode to use.

chmod (letter)(operator)(rwx) fileorfoldername (this will give read, write, and execute to all users)

chmod u=rwx,g=rwx,o=rwx fileorfoldername (this will give read, write, and execute to all users)

chmod u=rwx,g=rx,o=rx fileorfoldername (this will give read, write, and execute to the primary owner, but read and execute for all other users)

chmod u=rwx,g=rx,o=r fileorfoldername (this will give read, write, and execute to the primary owner, but read and execute for all users in the primary group, and read permissions for all other users)

chmod a=rwx fileorfoldername (this will give read, write, and execute to all users)

chmod +x fileorfoldername (this gives the file executable permissions to all users)

chmod +rx fileorfoldername (this gives the file read and executable permissions to all users)

chmod -wx fileorfoldername (this removes the write and executable permissions to all users)

chmod u=rwx,g-w,o-wx fileorfoldername (this will give read, write, and execute to the primary owner, but removes write for all users in the primary group, and removes write and execute for all other users)

chmod = fileorfoldername (this will remove all permissions from the file or folder)

Recursive

The previous command only changes the file or folder you specify. Frequently it is necessary to change all files and folders inside of a specific folder. In order to do that we need to tell chmod to set the mode recursively. We can do this by using the -R option.

chmod -R 755 foldername

Examples:

chmod -R 644 /home/user/public_html

chmod -R 744 /home/user/myfiles/*

It is recommended to check the command chmod --help for more settings and possible formats.

 

It is likely that these commands will take quite a bit of practice to master.