How to Set permissions on files and directories on ubuntu server 20.04

Viewing permissions

Step 1 : Use the ls command to list the access permissions of files and directories.

Step 2 : In each line, we see several fields of information.

1 : The permission strings

2 : The link count for the object

3 : The user that owns the file

4 : The group that owns the file

5 : The size in bytes

6 : The file was modified

7 : The name of the file

The permission strings

Step 3 : Each permission string can be broken down into four groups, as I'll show you in the following table

Object type User Group All users
- rw- r-- r--

Object type : - (file), d (directory), l (link)

User : This refers to the permissions that apply to the user that owns the file.

Group : This refers to the permissions that apply to the group that owns the file.

All users : This refers to the permissions that apply to all other users that owns the file.

Files Directories
r The file can be read The contents of the directory can be viewed
w The file can be written to Contents of the directory can be altered
x The file can be executed as a program The user or group can use cd to go inside the directory

To change permissions

Step 4 : To change permissions on an object, we will use the chmod command. The chmod command using this method consists of at least three parts from the following lists:

Access class Operator Access Type
u (user),g (group),o (other),a (all) +,-,= r, w, x

Ex : we're removing the r bit from other

Step 5 : You can also use octal point values to manage and modify permissions. This is actually the most common method of altering permissions. Each of the permission bits (r, w, and x) have their own octal equivalent, as follows:

Read : 4

Write : 2

Execute : 1

Step 6 : For example, if we add Read and Write, we get 6. If we add Read and Execute, we get 5. If we add all three, we get 7. If we add no permissions, we get 0. We repeat this for each column (User, Group, and Other) to come up with a string of three numbers. Here are some examples

600 : User has read and write (4+2). This is the same as -rw-------

770 : Both user and group have read, write, and execute. This is the same as -rwxrwx---

777 : This is the same as -rwxrwxrwx

Step 7 : If you wanted to change the permissions of a directory, the -R option changes recursive, meaning that you'll not only make the changes to the directory

Step 8 : To change all the directories to 755

Step 9 : To change all the files to 644

To change the owner

Step 10 : if we wanted to change the owner of a file, we could do the following:

Step 11 : We can also use the -R flag to change ownership of the directory itself, as well as all the files and directories it may contain:

Step 12 : If we would like to change the group assignment to the object, we would follow the following syntax: