How to use chmod in Linux
Welcome to our blog. In this tutorial we will learn more about linux file permissions using chmod command. We will learn in assigning permissions to file using rwxrwxrwx representation like chmod u+x filename and digits representation for example chmod 765 filename
OK now we will about next 9 characters. We will explain whats meaning of these characters
First three characters are for user’s permissions , next three are for group’s permissions and last three are for other users permissions
what does rwx means? below is brief explanation of rwx
r= read permission
x= execute permissions
Linux directory permission
if there is r(read) permission on directory it means you can list contents in directory. To enter into the directory you will need x(execute) permission to use cd command. And then finally if you want to create a file or subdirectory you need to have w(write permissions) with write permissions you can remove content in diretory. The content can be file or directory.
In terms of regular file permissions
r(read) permission allows to display content of file e.g cat file1.txt and to change, delete , or add content in fle you will need w(write) permission on file. if the file is like shell script and needs to be executed then you will need x(execute) permissions on file.
If you are the user/owner of a file, then only user owner permissions apply to you.rest of permissions have no influence on your access to the file.
If you belong to the group that is the group owner of a file, then the group owner permissions will apply to you. The rest of the permissions have no influence on your access to the file.
If you are not the user/owner of a file and you do not belong to the group owner, then the others permissions apply to you. The rest of the permissions have no influence on youraccess to the file.
Now lets see below example values and their meanings which will help us understand file permissions value
(rwxrwxrwx) No restrictions on permissions. In other words full permissions and anybody can do anything on this file , but this is not desirable in real world. You should be careful
(rwxr-xr-x) The file’s owner(user) can read, write, and execute the file.In second part (we have r-x) which means group has only read and execute permissions. last three characters are (r-x) which means rest of all users have also read and execute permissions only . you will find this setting generally common in linux environment
(rwx——) Only user(owner) can read,write and execute no permissions for groups and other users. This is to secure file
(rw-rw-rw-) everyone can read and write the file.(users, group, others)
(rw-r–r–) The owner can read and write a file, while all others may only read the file. This is also common permission in real life. where only user can read and write and others can only read
(rw——-) The owner can only read and write a file. To keep file private secure no other users/groups have permisison to access file.
to change permissions use chmod
To change permission on file we use command chmod
syntax : chmod permissions filename
Let’ see below example. file1 onwer is rob and it has -rw (read and write) permissions only.
[root@shahid security]# ls -l file1 -rw-rw-r--. 1 rob shahid 0 Feb 20 18:53 file1
To give user rob execute permission on file1 we will use chmod u+x filename(file1 is in this case) u+x means give or add(+) x(execute) permission to u(user)
[root@shahid security]# chmod u+x file1 [root@shahid security]# ls -l file1 -rwxrw-r--. 1 rob shahid 0 Feb 20 18:53 file1 [root@shahid security]#
Now you can see file1 has three permissions for user rob rwx(read,write,execute) check characters 2 to 5
As we use u+x to add execute permission to user you can do u-x to remove execute permission for user. In below example we will remove write permission for group
[[root@shahid security]# chmod g-w file1 [root@shahid security]# ls -l file1 -rw-r--r--. 1 rob shahid 0 Feb 20 18:53 file1 [root@shahid security]#
to remove read permisisons for others see below example
[root@shahid security]# chmod o-r file1 [root@shahid security]# ls -l file1 -rw-r-----. 1 rob shahid 0 Feb 20 18:53 file1
to give execute permissions now to all (user/owner,group,others) we will use command chomd a+x file1
[root@shahid security]# chmod a+x file1 [root@shahid security]# ls -l file1 -rwxr-x--x. 1 rob shahid 0 Feb 20 18:53 file1
you can also set permission on file using octal digits
Some people prefer using this type of permission
Below are digits and their meanings
7 = 4+2+1 (read/write/execute)
6 = 4+2 (read/write)
5 = 4+1 (read/execute)
4 = 4 (read)
3 = 2+1 (write/execute)
2 = 2 (write)
1 = 1 (execute)
0==0 no permission
To use this system see below example to give full permission rwxrwxrwx we can use 777 first 7 represents (7 = 4+2+1 (read/write/execute) for user second for group and third for other
[root@shahid security]# ls -l file -rw-rw-r--. 1 rob rob 0 Feb 20 18:54 file [root@shahid security]# chmod 777 file [root@shahid security]# ls -l file -rwxrwxrwx. 1 rob rob 0 Feb 20 18:54 file [root@shahid security]#
In above exmaple we gave full permissions to file ‘file’
Similarly 654 means rw-r-xr–
See below examples I hope they will help in understanding permissions assignment using digits
[root@shahid security]# chmod 664 file [root@shahid security]# ls -l file -rw-rw-r--. 1 rob rob 0 Feb 20 18:54 file [root@shahid security]# chmod 755 file [root@shahid security]# ls -l file -rwxr-xr-x. 1 rob rob 0 Feb 20 18:54 file [root@shahid security]#
I hope you have enjoyed this tutorial. we have learnt how to assign permissions using chmod command. We have also learnt how to assign permission using rwx and its equivalent way in digits. Please subscribe to our newsletter and let us knonw your thoughts