chmod command in Linux / Ubuntu

chmod is mainly used to modify file permissions in Linux. chmod can be used to control who can access files, search directories. In this tutorial, we will learn about chmod command in Linux / Ubuntu.

Viewing The File Permissions

To view the file permission of files and directories you can use ls -l in the terminal.

ls -l

Above command will display file permission of all files and directories available in a directory. If you want to display file permission of specific file or directory available in a directory use ls -l filename in terminal.

ls is for list the file and -l is long format option.

  • The first character represents, is it file or directory? “-” is for files and letter “d”  is for the directory.
  • The next nine characters represent three sets of permissions.
  1. The first three characters represent the permissions for the owner of the file (user permissions).
  2. The middle three characters represent the permissions for members of the file (group permissions).
  3. The last three characters represent the permissions for anyone.
  • r ( Read Permission ). The file can be opened.
  • w ( Write permissions ). The file can be edited and deleted.
  • x ( Execute permissions ). The file can be run or executed, in the case of a directory, search it.
  • --- (No Permission ). No permissions have been granted.
  • rwx means full permissions have been granted.

 

  • u : User ( Owner of the file )
  • g : Group ( Who are members of the file’s group )
  • o : Other ( User who are neither owner of the file nor members of file’s group )
  • a : Anyone (All user, all of the above. ie. user, group, other)

 

  • + : Grants the permission.
  • – : Remove the permission.
  • = : Grant a permission and remove other permission.

Syntax: chmod [option] [permissions] [filename]

Syntax (digits/Numbers): chmod [option] [number_file]

** If you want to have this permission and only this permission set, use the = option

Setting And Modifying file/directory Permissions

 

 

DigitsPermissions
000No permission
001Execute
010Write
011Write and Execute
100Read
101Read and Execute
110Read and Write
111Read, Write, and Execute
  • chmod +x filename   –
  • chmod +r filename  –
  • chmod +w filename  –
  • chmod +rwx filename  
  • chmod -wx filename   

** “r” is for read, “w” is for write, and “x” is for execute.