UNIX commands — Changing permissions

 

Overview

This article shows how to change file or directory permissions using SSH or FTP. See this article for an overview of how permissions are configured.

Changing permissions via SSH

If you're logged into your server via SSH, you can change permissions by running the chmod command. There are two ways to set permissions using chmod.

Option 1 — Numeric Mode

Numeric Mode is easier to understand as you just need to add up the values to assign numbers to each permission. For example:

4 = r (read)
2 = w (write)
1 = x (execute)

There are three sets of owners of a file or directory:

  • User
  • Group
  • Public

All three have their own permissions. Thus, all three must now be added together to get the full value. The following table illustrates the different permissions each owner could possibly have.

7 read, write, and execute ("rwx") 4 + 2 + 1 = 7
6 read and write ("rw-") 4 + 2 = 6
5 read and execute ("r-x") 4 + 0 + 1 = 5
4 read only ("r--") 4 + 0 + 0 = 4
3 write and execute (rare) ("-wx") 0 + 2 + 1 = 3
2 write only (rare) ("-w-") 0 + 2 + 0 = 2
1 execute only (rare) ("--x") 0 + 0 + 1 = 1
0 no permissions ("---") 0 + 0 + 0 = 0

Examples 

Command Permissions
[server]$ chmod 600 file.txt

-rw-------

  • Only the User has read and write permissions.
[server]$ chmod 700 dir

drwx------

  • Only the Owner has read, write and execute permissions
[server]$ chmod 755 file.txt

-rwxr-xr-x

  • The User has read, write and execute permissions.
  • The Group only has read and execute permissions.
  • All others have read and execute permissions.
[server]$ chmod 644 file.txt

-rw-r--r--

  • The User has read and write permissions.
  • The Group has read permissions.
  • Others have read permissions.
[server]$ chmod 664 file.txt

-rw-rw-r--

  • The User has read and write permissions.
  • The Group has read and write permissions.
  • Others have only read permissions.

Option 2 — Named Mode

There are two sets of permissions to assign when using 'Named Mode', which are who and what permissions. For example:

who permissions

u change the user bits
g change the group bits
o change the other bits
a change the bits for everybody

what permissions

r grant read access
w grant write access
x grant execute access
s set the sticky bit

Using ("+") and ("-") with the information above, you’d combine permissions from the who and what groups to assign the exact permissions you desire.

Format

The format to use chmod in the following commands is:

[server]$ chmod "groups"+"access" file.example

Examples

Allows everybody to read file.txt.

In the following example, a is the bit for 'everybody' and the r (read) permission is added:

[server]$ chmod a+r file.txt

Strips everybody of all permissions, except for the owner who retains any former permissions.

In the following example, g is group bit, o is the 'other users' bit and the - sign is removing all permissions (rwx):

[server]$ chmod go-rwx file.txt

The file named script.cgi is executable by the user and group.

In the following example, u is the user bit, g is the 'group' bit, and the x permission is added to both:

[server]$ chmod ug+x script.cgi

All files created in the directory somedir are owned by the group that owns somedir.

In the following example, g is the group bit and the s flag is added to it:

[server]$ chmod g+s somedir

 

Changing permissions in an FTP client

FTP clients such as Filezilla allow you to use the Numeric Mode to set permissions.

If you right-click on a file in an FTP client, you can view its permissions. For example:

You can then either check the boxes, or type in the Numeric value of the permissions you need to change.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?