‘chmod command’ -How to change file/folder permissions in linux ?

‘chmod command’ -How to change file/folder permissions in linux ?

How do you pronounce chmod? That is a question that will give you multiple answers, some answers are below Change mode C.H mode or see-aych-m

Linux Commands – Working with Directories

How do you pronounce chmod?

That is a question that will give you multiple answers, some answers are below

  • Change mode
  • C.H mode or see-aych-mod
  • Chu-mode
  • Other – Please let us know in comments

Jokes apart, let’s look at how to change the file permissions using chmod- whichever way you choose to pronounce it

 

How to change file /folder permissions in Linux?

You can use chmod to set permissions for files or folders. In Nix(Linux, Unix, and Mac) based systems, you can assign permissions for a file/folder to the owner of the file(User), a group of users or everyone else(Other)

chmod Syntax

chmod options permissions <filename>

How to check the chmod file permissions?

you can use the ls command with -l to see the permission set for files and folders. The permissions can be assigned to the owner of a file (user), a group of users(group) and everyone else(others). Please see the table below.

DescriptionTypeRepresented by
Owner of the fileuseru
Group of usersgroupg
neither owner nor a group memberothero
all of the 3 abovealla

Permissions can be either octal or symbolic. If you are using octal, you can represent permissions as numbers(777,440, 775, etc..). and in symbolic mode permissions(a+x,u+r, etc..), you can represent permissions as letters. The type of permission can be to read, write, execute and a combination of these.

File or folder access modes

TypeSymbolicNumbersFileFolder
READr4permission to read filepermission to list folder content
WRITEw2write to a filewrite to a directory
EXECUTEx1execute a filesee the list of files in the directory
NONE-o--

I myself is a visual learner, so I try to visualize to make sure that I understand better, please see the Venn Diagram below, so that you can understand the concept better. So you can see that each type of permission has an octal value.

Examples of using Octalmode:

Let’s Look at some examples based on the table above

chmod Files

  • Example 1: Chmod 777

chmod   777   filename.txt

Let’s disassemble the above command. The three 777 in order is for the user, group and other in order as in the table below.

chmod777
User(u)Group(g)Others(o)
4+2+14+2+14+2+1
Read+Write+Execute(rwx)Read+Write+Execute(rwx)Read+Write+Execute(rwx)

So by using the command chmod 777 file name, you are giving the user, group and other permission to read, write and execute the file. This is the least restriction you can set for any file or folder. Giving 777 permissions to the wrong file can lead to security issues.

  • Example 2: chmod 775

chmod775
User(u)Group(g)Others(o)
4+2+14+2+14+0+1
Read+Write+Execute(r w x)Read+Write+Execute(r w x)Read+Execute(r - x)

Here as per the table above both user and group have the permission to read, write and execute, but everyone else has only read and execute permission. You can see that 775 is a bit more restrictive than 777.

  • example 3: chmod 600

chmod600
User(u)Group(g)Others(o)
4+2+00+0+00+0+0
Read+Write( r w - )None( - - - )None( - - - )

Here as per the table user (Owner of the file) have read and write permission and everyone else(group & other) have no permission at all. So this is more restrictive than 775 or 777.

  • Example 4: chmod 444

chmod444
User(u)Group(g)Others(o)
4+0+04+0+04+0+0
Read( r - - )Read( r - - )Read( r - - )

As per the table above the owner(user), group and others have only read permission.

  • chmod 400

chmod444
User(u)Group(g)Others(o)
4+0+00+0+00+0+0
Read(r - - )Read( - - - )Read( - - - )

As per the table above only the owner of the file has read permission, everyone else has no permission at all. If you have ever tried to ssh to a server with a key file(.pem) file, you must have noticed this error below.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for 'ssh-key.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "ssh-key.pem": bad permissions
ubuntu@exmaple.com: Permission denied (publickey).


You can solve it by giving only the owner read-only permission, using the command ‘chmod 400 ssh-key.pem’ (chmod ssh key).

  • example 5: chmod 000

chmod000
User(u)Group(g)Others(o)
0+0+00+0+00+0+0
No permissions(---)No permissions(---)No permissions(---)

Here the user(owner), group and others have no permission at all. To change it back, login as root(sudo bash) and then give the appropriate permissions using the chmod command.

symbolic mode

DescriptionTypeRepresented by
Owner of the fileuseru
Group of usersgroupg
neither owner nor a group memberothero
all of the 3 abovealla

In the symbolic mode , instead of octal characters are used while implementing the permissions.  Permissions can be read(r) , write(w) or execute (x). And we use operator symbols like + , – and = to assign or remove permissions( Please see the table below)

OperatorUse
+add a ceratin pemission
-remove a certain pemission
=give exact mode to a certain usr or group
chmod examples using symbolic mode
  • chmod u+x filename.txt

Here ‘u’ represents the user and ‘x’ means permission to execute and ‘+’ is to add the permissions. So this command will give Owner(user(u)) permission to execute the file.

  • chmod g-r filename.txt

Here ‘g’ represent the group and ‘r’ means the permission to read and ‘-‘ removes the permission. So if you execute this command , the  group will loose the permission to read the file.

  • chmod +x   or  chmod +r

What if we don’t explicitly mention where to apply the permission like in the above example ? Here you need o remember that execute(x) and read (r) the default is ‘a’- everyone. So you will give everyone permission to read (chmod +x ) or write (+w). that means

chmod +x filename  ==> chmod a+x filename

and

chmod +r filename  ==> chmod a+r filename

  • chmod +w  : Here the default permission is applied to the user(owner) of the file if you don’t explicitly mention whom you want to apply the permission for . that means

chmod +w filename  ==>chmod u+w filename

chmod recursive

chmod can be used in recursive mode to add or remove permissions from a group of files or folders at once. you can use  ‘-R’ /  ‘–recursive’ to give permissions recursively.

Examples of chmod recursive

  • chmod -R 777  folder

Here all the files and folders under the directory named ‘folder ‘ will get 777 permission .

Chmod Linux manual

To see the full Linux manual of chmod command in Linux,use the man command as below

man chmod

 

How to check Linux file permissions in Linux?

You  use ‘ls’ command(list command) with ‘-l’ to list the file names along with the permissions.

COMMENTS

WORDPRESS: 0
DISQUS: