file ownerships and file permisions on Linux and unix like systems

This command will list files in the current directory with file info.

ls -l linux_script.sh
-rwxr-xr--  1  root  staff  1801  Jul 21  21:30 linux_script.sh

It indicates
File name: linux_script.sh
Last modified: July 21 at 9:30PM
File size: 1801 bytes
The group of the file: staff
The owner of the file: root
root access level: read, write and execute
staff access level: read and execute
others access level: read only
The first symbol – means this is a file, D means it is a directory. The rest are 9 symbols divided into 3 groups:
Firt three symbols for the owner of the file
Second three symbols for the group of the file
Last three symbols for other users who have access to the computer

Create a new user “ben” and set a password’

adduser ben
passwd ben

Delete the user “ben”.

userdel ben

Create a new group developers and check if it was created successfully.

groupadd developers
grep developers /etc/group

Delete the group developers.

groupdel developers

Add the user “ben” to the group staff and check if the staff was added successfully.

usermod -a -G staff ben
groups ben

Remove the user “ben” from the group staff and check if it was removed successfully.

gpasswd -d ben staff
groups ben

Change the group of /u to “staff”.

chgrp staff /u

Change the group of /u and subfiles to “staff”

chgrp -hR staff /u

Change the owner of /u to “root”.

chown root /u

Change the owner of /u to “root” and also change its group to “staff”.

chown root:staff /u

Change the owner of /u and subfiles to “root”.

chown -hR root /u

Give read permission for all users

chmod +r linux_script.sh

Give write permission for all users

chmod +w linux_script.sh

Give execute permission for all users

chmod +x linux_script.sh

Give read, write and execute permission for all users

chmod +rwx linux_script.sh

Give read, write and execute permissions to the file owner; give read and write permissions to the users who are members of the file’s group; give read permisson to other users

chmod u+rwx,g+rw,o+r linux_script.sh

To remove the write permission for all users

chmod -w linux_script.sh

Give read permission to everyone for the directory /share and it’s subfiles

chmod -R +r /share

We can also use numbers to grant permissions for files and directories.

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

For example:

chmod 400 linux_script.sh #read by owner
chmod 040 linux_script.sh #read by group
chmod 004 linux_script.sh #read by anybody (other)
chmod 200 linux_script.sh #write by owner
chmod 020 linux_script.sh #write by group
chmod 002 linux_script.sh #write by anybody
chmod 100 linux_script.sh #execute by owner
chmod 010 linux_script.sh #execute by group
chmod 001 linux_script.sh #execute by anybody

We can add up the numbers to get other types of permissions.

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)

Give read, write, execute permissions to the owner, everyone else no access

chmod 700 linux_script.sh

Give read, write, execute permissions to the group, everyone else no access

chmod 070 linux_script.sh

Give read and execute permissions to other users, everyone else has same access since other users means anyone in the world.

chmod 005 linux_script.sh

Give read, write, execute permissions to the owner; read, and execute permissions to the group; read permission to others.

chmod 754 linux_script.sh

Search within Codexpedia

Custom Search

Search the entire web

Custom Search