File Permissions
File Permissions
Permission Groups
Every file on your Linux system , including directories , is owned by a s pecific user and group.
Therefore, file permissiions are defined separately for users,groups,and others .
Each file and directory has three user based permission groups:
owner - The Owner permissions apply only the owner of the file or directory, they will not impact
the actions of other users.
group - The Group permissions apply only to the group that has been assigned to the file or
directory, they will not effect the actions of other users.
all users - The All Users permissions apply to all other users on the system, this is the
permission group that you want to watch the most.
Permission Types
Each file or directory has three basic permission types:
read - The Read permission refers to a user's capability to read the contents of the file.
write - The Write permissions refer to a user's capability to write or modify a file or directory.
execute - The Execute permission affects a user's capability to execute a file or view the
contents of a directory.
You can view the permissions by checking the file or directory permissions by ls -l
User rights/Permissions
The first character that I marked with an underscore is the special permission flag that
can vary.
The following set of three characters (rwx) is for the owner permissions.
The second set of three characters (rwx) is for the Group permissions.
The third set of three characters (rwx) is for the All Users permissions.
The Fourth set is integer/number displays the number of hardlinks to the file.
The last piece is the Owner and Group.
ls -l /home
total 32
drwxr-x---. 38 editor editor 12288 Nov 30 10:49 editor
drwxr-x---. 4 greg development 4096 Nov 30 12:44 greg
drwx------. 21 gretchen gretchen 4096 Nov 30 11:26 gretchen
drwxr-xr-x. 41 ian ian 4096 Nov 30 10:51 ian
-rwxrwxr-x 1 mana mana 248 Mar 18 15:32 jt.sh
-rw-rw-r-- 1 mana mana 441 Mar 18 14:25 ThreadTest.class
-rw-rw-r-- 1 mana mana 560 Mar 18 15:10 ThreadTest.java
The output from ls -l may contain filesystem objects other than files and directories as shown by
the first character in the listing.
The eleventh character in a long listing from the ls command is a recent enhancement, so some
distributions may still show only the first ten characters. In other cases, the eleventh character is
a space, so you may not notice it. This character specifies whether an alternate access method
applies to the file. When the character following the file mode bits is a space, there is no
alternate access method. When it is a printing character, then there is such a method. The
method might be an access control list for example. GNU ls uses a '.' (dot) character to signify a
file with only an SELinux security context. Files with any other ombination of alternate access
methods are marked with the '+' (plus) character (if file set with ACL permission we will see +
mark).
Changing permissions
You can set file permissions with the chmod command. Both the root user and the file's owner
can set file permissions.
Following are few examples on how to use the symbolic representation on chmod.
Wipe out all the permissions but add read permission for everybody:
6. Change execute permission only on the directories (files are not affected)
On a particular directory if you have multiple sub-directories and files, the following command
will assign execute permission only to all the sub-directories in the current directory (not the files
in the current directory).
$ chmod u+X *
Note: If the files has execute permission already for either the group or others, the above
command will assign the execute permission to the user
Special Permissions
The special permissions flag can be marked with any of the following:
_ - no special permissions
s - This indicated the setuid/setgid permissions.
t - This indicates the sticky bit permissions.
i - immutable
when we execute these permission, we find below signs in the permission fields
Permissions Descriptions
--S------ SUID is set, but user (owner) execute permission is not set.
--------T Sticky bit is set, bot other execute permission is not set.
--------t Sticky bit and other execute permission are both set.
Access modes
SUID Bit
User excuted as owner of the file.
When an executable file runs, it runs under the ownership of the user who has executed it. It
means that when student user runs "ls" command then the corresponding process will run under
the ownership of student. The SUID bit, also known as Set User ID bit, overwrites this behavior.
If SUID bit is set on a program, then that program will run as the owner of that file, irrespective
of who is executing it.
The passwd command in Linux has SUID bit set.
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 23420 Aug 3 2010 /usr/bin/passwd
This can be seen in the third field of permissions. The 's' in place of 'x' indicates that SUID bit is
set. With SUID bit set, when a normal user (say student) runs the passwd command, the
command runs with the ownership of 'root', and not as student, because root is the owner of this
file. This behavior is required because the passwords are stored in the /etc/shadow file, which
has no permission on group or other level.
$ ls -l /etc/shadow
-r-------- 1 root root 1027 Jul 13 21:56 /etc/shadow
You need to understand that all users cannot be given read or write permission on this file for
security reasons, otherwise they will read/change the passwords of other users. So this causes
a problem that if the users don't have permission on this file, then how will they change their
own passwords? So SUID bit solves the problem. The passwd command has SUID bit set, so
when normal users execute this command, they run it with the ownership of root, i.e. the owner
of passwd command.all users starting the passwd command execute it as root
The letters rwxXst select file mode bits for the affected users: read (r), write (w), execute (or
search for directories) (x), execute/search only if the file is a directory or already has execute
permission for some user (X), set user or group ID on execution (s), restricted deletion flag or
sticky bit (t).
This is to be noted that SUID bit works on files only. To set the SUID bit on a file, use the chmod
command as follows
$ ls -l
total 8
-rwxr--r-- 1 root root 104 Aug 19 01:26 hello.sh
$ ls -l
total 8
-rwsr--r-- 1 root root 104 Aug 19 01:26 hello.sh
The numeric method for changing permissions can also be used. If the normal permissions for a
file are 744 (suppose), then with SUID bit set, these will become 4744. SUID bit has value 4.
$ ls -l
total 8
-rwxr--r-- 1 root root 104 Aug 19 01:26 hello.sh
$ chmod 4744 hello.sh
$ ls -l
total 8
-rwsr--r-- 1 root root 104 Aug 19 01:26 hello.sh
SGID Bit
Unlike SUID bit, SGID bit works on both files and directories, but it has different meaning in both
cases.
The setuid bit applies to users. However, there is also an equivalent property for groups: the
setgid bit. A program for which this bit was set runs under the group ID under which it was
saved, no matter which user starts it. Therefore, in a directory with the setgid bit, all newly
created files and subdirectories are assigned to the group to which the directory belongs.
Consider the following example directory:
You can see the s that denotes that the setgid bit is set for the group permission. The owner of
the directory and members of the group archive may access this directory. Users that are not
members of this group are “mapped” to the respective group. The effective group ID of all
written files will be archive. For example, a backup program that runs with the group ID archive
is able to access this directory even without root privileges.
On files:
For file, it has similar meaning as the SUID bit, i.e. when any user executes a file with SGID bit
set on it, it will always be executed with the group ownership of that file, irrespective of who is
running it. For example, the file /sbin/netreport has SGID bit set, which can be seen in the 's'
instead of 'x' in group permissions.
$ ls -l /sbin/netreport
-rwxr-sr-x 1 root root 6020 Oct 13 2010 /sbin/netreport
This file has group ownership of root group. So when a user (say student) executes it, then the
corresponding process will not have group ownership of student, but that of root group.
On directories:
Now let’s talk about SGID on directories. SGID on directories is used for creating collaborative
directories. To understand SGID bit on directories, consider the following scenario:
Suppose three users jack, jones and jenny are working together on some project. All of them
belong to a group named javaproject. For the course of the project, they need to share all the
files related to the project. All of them must be able to see each other's file. This can be done
simply by providing read permission on group level. Further suppose that the directory used for
the project is "/javaproject".
Here, a problem arises that when a file is created, it belongs to the primary group of the user
who created the file. So, when different users create their files in this directory, those files will
not have group ownership of javaproject group.
What we do for our problem is that we set the group of /javaproject directory to be javaproject
group, and set the SGID bit set on it. When SGID bit is set on a directory, all the files and
directory created within it has the group ownership of the group associated with that directory. It
means that after setting SGID bit on /javaproject directory, all the files and directories being
created in this directory will have the group ownership of "javaproject" group. Moreover, this
behavior is recursive, i.e. the directories created in this directory will have SGID bit set as well.
The permissions for the new directory will also be same as that of /javaproject directory.
The SGID bit can be set with chmod command as follows:
$ ls -ld /javaproject
drwxrwxr-x 2 root javaproject 4096 Aug 19 02:33 /javaproject
$ chmod g+s /javaproject
$ ls -ld /javaproject
drwxrwsr-x 2 root javaproject 4096 Aug 19 02:33 /javaproject
Now when jones user creates a file in this directory, it is created under the group ownership of
javaproject group.
The numeric value corresponding to SGID bit is 2. So, to add SGID bit numerically, use the
following command:
$ ls -ld /shared/
drwxrwxr-x 2 root adm 4096 Aug 19 02:47 /shared/
$ chmod 2775 /shared/
$ ls -ld /shared/
drwxrwsr-x 2 root adm 4096 Aug 19 02:47 /shared/
Sticky Bit
There is also the sticky bit. It makes a difference whether it belongs to an executable program or
a directory. If it belongs to a program, a file marked in this way is loaded to RAM to avoid
needing to get it from the hard disk each time it is used. This attribute is used rarely, because
modern hard disks are fast enough. If this bit is assigned to a directory, it prevents users from
deleting each other's files. Typical examples include the /tmp and /var/tmp directories:
what if an user accidentally or deliberately deletes (or rename) a file created by some
other user in this directory?
Well, to avoid these kind of issues, the concept of sticky bit is used.A Sticky bit is a permission
bit that is set on a file or a directory that lets only the owner of the file/directory or the root user
to delete or rename the file. No other user is given privileges to delete the file created by some
other user.
The sticky bit works on directories only. If a user wants to create or delete a file/directory in
some directory, he needs write permission on that directory. The write permission on a directory
gives a user the privilege to create a file as well as the privilege to remove it.
The /tmp directory is the directory for temporary files/directories. This directory has all the rights
on all the three levels, because all the users need to create/delete their temporary files. But as
the users have write permission on this directory, they can delete any file in this directory. The
permissions of that file do not have any effect on deletion.
But with sticky bit set on a directory, anyone can create a file/directory in it, but can delete his
own files only. Files owned by other users cannot be deleted.
$ ls -ld /tmp/
drwxrwxrwt 4 root root 4096 Aug 19 02:29 /tmp/
The sticky bit can be set with chmod command with following methods:
Example:1
Example:2
Turn ON the sticky bit on the directory by using +t flag of chmod command.
# chmod +t allAccess/
Sticky bit can be removed from a directory permissions through the -t option of the chmod
command.Here is an example :
# chmod -t allAccess/
Alternatively
This is very useful to set attributes in system files like passwd and shadow files wherein user’s
info are contains.
Syntax of chattr
Operator
● + : Adds the attribute to the existing attribute of the files.
● - : Removes the attribute to the existing attribute of the files.
● = : Keep the existing attributes that the files have.
Note: The immutable bit +i can only be set by superuser (i.e root) user or a user with sudo
privileges can able to set.
After setting immutable bit, let’s verify the attribute with command ‘lsattr‘.
Now, tried to delete forcefully, rename or change the permissions, but it won’t allowed says
“Operation not permitted“.
In the above example, we’ve seen how to set attribute to secure and prevent files from a
accidental deletion, here in this example, we will see how to reset (unset attribute) permissions
and allows to make a files changeable or alterable using -i flag.
[root@tecmint tecmint]# chattr -i demo/ important_file.conf
After resetting permissions, verify the immutable status of files using ‘lsattr‘ command.
[root@tecmint tecmint]# lsattr
---------------- ./demo
---------------- ./important_file.conf
You see in the above results that the ‘-i‘ flag removed, that means you can safely remove all the
file and folder reside in tecmint folder.
Now try to create a new system user, you will get error message saying ‘cannot open
/etc/passwd‘.
[root@tecmint tecmint]# useradd tecmint
useradd: cannot open /etc/passwd
This way you can set immutable permissions on your important files or system configuration
files to prevent from deletion.
After setting append mode, the file can be opened for writing data in append mode only. You
can unset the append attribute as follows.
Now try to replace already existing content on a file example.txt, you will get error saying
‘Operation not permitted‘.
Now try to append new content on a existing file example.txt and verify it.
[root@tecmint tecmint]# echo "replace contain on file." >> example.txt
To secure entire directory and its files, we use ‘-R‘ (recursively) switch with ‘+i‘ flag along with
full path of the folder.
After setting recursively attribute, try to delete the folder and its files.
[root@tecmint tecmint]# rm -rf myfolder/
rm: cannot remove 'myfolder/': Operation not permitted
To unset permission, we use same ‘-R’ (recursively) switch with ‘-i’ flag along with full path of the
folder.
[root@tecmint tecmint]# chattr -R -i myfolder
chgrp has a -R option to allow changes to be applied recursively to all selected files and
subdirectories.
Default group
When you studied Access modes above, you learned how setting the sgid mode on a directory
causes new files created in that directory to belong to the group of the directory rather than to
the group of the user creating the file.
You may also use the newgrp command to temporarily change your primary group to another
group of which you are a member. A new shell will be created, and when you exit the shell, your
previous group will be reinstated, as shown in Listing.
UMASK
Unless set up by yourself or the system administrator, your default umask setting will be 0000,
which means that new files you create will have read and write permission for everyone (0666
or -rw-rw-rw-), and new directories that you create will have read, write and search permissions
for everyone (0777 or drwxrwxrwx). You will almost certainly want to change your umask setting
to a non-zero value, to make the default values to the newly created files and directories more
restrictive.
You can change the default permission by using the umask command.
The digits in the umask number are 'subtracted' from 777 for directories or 666 for files when
you are creating their initial permissions.
Then when you create new files their default permissions will be 644 (666 minus 022, i.e.
-rw-r--r--). When you create new directories their default permissions will be 755 (drwxr-xr-x).
For reference, the following table shows the mappings between umask values and default
permissions. BE VERY CAREFUL not to confuse umask and chmod permissions, as they are
entirely different (a binary inversion of each other) and are NOT INTERCHANGABLE!!
How Do I Calculate umasks?
The octal umasks are calculated via the bitwise AND of the unary complement of the argument
using bitwise NOT. The octal notations are as follows:
■ Octal value : Permission
■ 0 : read, write and execute
■ 1 : read and write
■ 2 : read and execute
■ 3 : read only
■ 4 : write and execute
■ 5 : write only
■ 6 : execute only
■ 7 : no permissions
$ umask
If you set umask at the shell prompt, it will only apply to the current login session. It will not
apply to future login sessions. To apply umask setting automatically at login, you would add the
umask command to your .login file (C Shell users) or .profile (Bourne and Korn Shell users).
# vi /etc/profile
OR
$ vi ~/.bashrc
umask 022
Save and close the file. Changes will take effect after next login. All UNIX users can override the
system umask defaults in their /etc/profile file, ~/.profile (Korn / Bourne shell) ~/.cshrc file (C
shells), ~/.bash_profile (Bash shell) or ~/.login file (defines the user's environment at log
caveat: you cannot setup a umask per directory as it's a process level thing.
You cannot set umask per directory, it's a process-level value. If you need to prevent others from
reading files in a directory, revoke the corresponding permissions bits.
For example, if you've a directory /home/user/directory with some files and directories which can
get permissions like 777 from a process, set the permission bits of /home/user/directory to
something like 700. That will make it impossible for other users (excluding the superuser root) to
descend in /home/user/directory.
$ umask u=rwx,g=,o=
$ mkdir dir2
$ touch file2
$ ls -ld dir2 file2
Prerequisites
Filesystem options
Before using ACL's we must first verify that our filesystem has the acl option enabled.
A common way to enable acl support on a filesystem is to add the acl option to a filesystems
mount options in /etc/fstab. We can check if that has been done on this system by using the
mount command.
root@testvm:~# mount | grep root
/dev/mapper/workstation-root on / type ext4 (rw,errors=remount-ro)
In this case the acl option has not been added but that doesn't mean our filesystem doesn't
have acl's enabled. On most distributions the default filesystems have the acl option as part of
the default mount options. You can check if your filesystems have acl as part of the defaults by
using the tune2fs command.
As you can see on my test system the default mount options contain acl, in this case my
filesystem will support acl's even if I don't specify it during the mount process. If your filesystem
does not have acl as a default mount option, than you can add it during the mount process
easily by editing the fstab file.
root@testvm:~# vi /etc/fstab
Simply add the term acl to the mount options as shown below.
Once your fstab file is edited you can remount your filesystem with the mount command.
# setfacl -m "u:username:permissions"
or
# setfacl -m "u:uid:permissions"
# setfacl -m "g:groupname:permissions"
or
# setfacl -m "g:gid:permissions"
Once you've set the acl with setfacl it is common sense to check if it took effect, in order to do
so you will need to use the getfacl command.
As you can see there is now a + at the end of the directories permissions. This + is the indicator
that this file or directory has acl's, from here you can use the getfacl command to see what they
are.
# setfacl -b abc
b - option that this will remove all of the acl rules on the specified file or directory.
Examples of Usage
Use Cases
1. Removing all acl entries from a file or directory
2. Set test users to have read access to all files in a directory
3. Set the same acl changes recursively
4. Set the same acls on all newly created files automatically
5. Set testuser1 to have read, write and execute access to the appuser1 directory
6. Set all users to have read, write and execute access to the shared directory
7. Remove the acl for testuser1 on appuser1 directory
2. Set testusers to have read access to all files in the appdir directory
we want the testusers group to have read access to all files in the appdir directory.
The appdir has 2 sub directories appdir/appuser1 and appdir/appuser2 in this case we want the
acl rules set above to apply to these directories as well. This can easily be accomplished by
adding the -R (recursive) option in setfacl. This works exactly like the recursive option for
chmod.
The -d (default) option in setfacl is extremely useful; this option will allow us to set an acl rule to
be the default rule. When this is set on a directory this makes all new files or directories created
within that directory inherit the same acl rules.
# setfacl -dm g:testusers:r appdir/
As you can see dir1 also have the default acl rules as appdir, yet appuser1 does not. This is
because we did not set the default recursively; this can be done by using the -R option.
5. Set testuser1 to have read, write and execute access to the appuser1 directory
While the user testuser1 is in the testusers group and has read access to the appuser1 directory
he does not have write access. In this case we want to give him write access without giving the
rest of the testusers write access. This can be done using acl's by specifying a specific user
rather than a group.
6. Set all users to have read, write and execute to the shared directory
We have now given users and groups permissions on directories and files, but what happens
when we want all users to have access to a directory? Adding every users name or group could
get tedious, in this case we can set the "other" or "world" permissions so that all users on a
system can access this directory.
You might be asking yourself right now "wait a minute, did setfacl just set the permissions to
757?"; why yes it did! ACL's are just an extension of the standard unix permissions, in this case
because we are not specifying a user or group; setfacl will simply just change the mode of the
file. Tricky, yes I know.
Unlike the -b option that removes all acl's on a directory or file the -x option will only remove the
specified rule. This is useful for when you maybe fat fingered an acl rule and don't want to
completely remove all of the acl rules attached to that file/directory.
8. How to share the directory among the marketing-g and sales-g groups and the user
named george. Sales, marketing and george need to have full access to the directory
including the creation of new files in that directory.
# file: projections
# owner: peter
# group: peter
user::rwx
user:george:rwx
group::r-x
group:sales-g:rwx
group:marketing-g:rwx
mask::rwx
other::r-x
Solution: