file ownerships and file permisions on Linux and unix like systems
This command will list files in the current directory with file info.
[code language=”shell”]
ls -l linux_script.sh
-rwxr-xr– 1 root staff 1801 Jul 21 21:30 linux_script.sh
[/code]
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’
[code language=”shell”]
adduser ben
passwd ben
[/code]
Delete the user “ben”.
[code language=”shell”]
userdel ben
[/code]
Create a new group developers and check if it was created successfully.
[code language=”shell”]
groupadd developers
grep developers /etc/group
[/code]
Delete the group developers.
[code language=”shell”]
groupdel developers
[/code]
Add the user “ben” to the group staff and check if the staff was added successfully.
[code language=”shell”]
usermod -a -G staff ben
groups ben
[/code]
Remove the user “ben” from the group staff and check if it was removed successfully.
[code language=”shell”]
gpasswd -d ben staff
groups ben
[/code]
Change the group of /u to “staff”.
[code language=”shell”]
chgrp staff /u
[/code]
Change the group of /u and subfiles to “staff”
[code language=”shell”]
chgrp -hR staff /u
[/code]
Change the owner of /u to “root”.
[code language=”shell”]
chown root /u
[/code]
Change the owner of /u to “root” and also change its group to “staff”.
[code language=”shell”]
chown root:staff /u
[/code]
Change the owner of /u and subfiles to “root”.
[code language=”shell”]
chown -hR root /u
[/code]
Give read permission for all users
[code language=”shell”]
chmod +r linux_script.sh
[/code]
Give write permission for all users
[code language=”shell”]
chmod +w linux_script.sh
[/code]
Give execute permission for all users
[code language=”shell”]
chmod +x linux_script.sh
[/code]
Give read, write and execute permission for all users
[code language=”shell”]
chmod +rwx linux_script.sh
[/code]
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
[code language=”shell”]
chmod u+rwx,g+rw,o+r linux_script.sh
[/code]
To remove the write permission for all users
[code language=”shell”]
chmod -w linux_script.sh
[/code]
Give read permission to everyone for the directory /share and it’s subfiles
[code language=”shell”]
chmod -R +r /share
[/code]
We can also use numbers to grant permissions for files and directories.
[code language=”text”]
4 means read (r)
2 means write (w)
1 means execute (x)
[/code]
For example:
[code language=”shell”]
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
[/code]
We can add up the numbers to get other types of permissions.
[code language=”text”]
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)
[/code]
Give read, write, execute permissions to the owner, everyone else no access
[code language=”shell”]
chmod 700 linux_script.sh
[/code]
Give read, write, execute permissions to the group, everyone else no access
[code language=”shell”]
chmod 070 linux_script.sh
[/code]
Give read and execute permissions to other users, everyone else has same access since other users means anyone in the world.
[code language=”shell”]
chmod 005 linux_script.sh
[/code]
Give read, write, execute permissions to the owner; read, and execute permissions to the group; read permission to others.
[code language=”shell”]
chmod 754 linux_script.sh
[/code]
Search within Codexpedia

Search the entire web
