7.1 Control Access To Files With Linux File System Permissions
7.1 Control Access To Files With Linux File System Permissions
Linux is a multi-user system and therefore directories and files inside a Linux computer need to
be protected from unauthorized use. Linux file access permissions are used to control who is
able to read, write and execute a certain file. Access permissions are implemented at a file level
with the appropriate permission set based on the file owner, the group owner of the file and
other access. In Linux, directories and device are also files and therefore the file permissions
apply on a directory and devices level as well, although some permissions are applied
differently depending upon whether the file is a regular file, directory or device. The access
permission design allows a good amount of flexibility in what permissions can be applied.
There are three categories of permissions which apply: read, write, and execute. These
permissions affect access to files and directories. The permissions can be assigned in octal
notation or in the more easily recognized character or symbolic format.
Abbreviation Description
r (Read) Permission to read a file.
Permission to read a directory (also requires "x")
w (Write) Permission to delete or modify a file.
Permission to delete or modify files in a directory
x (Execute) Permission to execute a file/script.
Permission to read a directory (also requires "r")
To View file and directory permissions and ownership use - l option of the ls command will
expand the file listing to include both the permissions of a file and the ownership.
# ls -l
Chmod Command:
The chmod command stands for "Change Mode", and allows changing permissions of files and
f1olders using symbolic or numeric format. Using this command, to set permissions (read,
write, execute) on a file and directory for the owner, group and other. Below table show
changing permission using Symbolic Method and Numerical Method.
Chgrp Command:
Chgrp (Change Group) is a command which is useful to change group associated to a file or
folder from one group to other in a Linux. This is sister command to chown which is used to
change owner of the file or folder as well as group name associated with that file.
Commands Description
# chgrp new-group filename Change the group name
# chgrp -R new-group folder Change the group name of all contents inside
Umask:
UMASK (User Mask or User file creation Mask) is the default permission given when a new file
or folder is created. The default umask 002 used for normal user. With this mask default
directory permissions are 775 and default file permissions are 664. The default umask for the
root user is 022 results into default directory permissions are 755 and default file permissions
are 644. The minimum and maximum UMASK value for a folder is 000 and 777. The minimum
and maximum UMASK value for a file is 000 and 666. The file has 666 because only scripts and
binaries should have executed permissions, normal and regular files should have just read and
write permissions. Directories require execute permissions for viewing the contents in it, so
they can have 777 permissions. Simply subtract the umask from the default permissions to
determine the final permission for file: 666 – 002 = 664: Simply subtract the umask from the
default permissions to determine the final permission for directory:777 – 002 = 775:
Commands Description
# umask Show the default UMASK in octal notation
# umask -S Show the default UMASK in symbolic notation
# umask 022 To change default Umask value numerical value
# umask -S u=r, g=r, o= To change default Umask value symbolic value
# vi /etc/profile Change default UMASK for all new users
$ vi ~/.bashrc Change default UMASK for existing users
$ umask u=rwx, g=,o= To change default Umask value symbolic value
Sticky Bit:
If Sticky bit is applied on a file or directory, then only root and owner of that file or directory
can delete it. Even if other users are having full permissions they cannot delete the file or
directory. chmod o+t folder. “T”, which means sticky bit has been applied. The sticky bit is
primarily used on shared directories.
Permissions Meaning
--S------ SUID is set, but user (owner) execute is not set.
--s------ SUID and user execute are both set.
-----S--- SGID is set, but group execute is not set.
-----s--- SGID and group execute are both set.
--------T Sticky bit is set, but other execute is not set.
--------t Sticky bit and other execute are both set.