File and Directory Permission 2
File and Directory Permission 2
File and Directory Permission 2
PERSSIONS
DO ALAKE
2023
• Linux stores files and directory permission within the INODE in a structure called the mode.
The mode contains three sections that describe permission assigned to these three entities:
You will see these three sets of permission referred to UGO for user, group and other.
For each of those entities you may assign yes/no indicator for these permissions
Read (r) Display the content of file List the contents of the directory,
Write (w) Modify or delete the contents of file Add or delete files from directory
Execute (x) Execute a program or shell scripts Change (cd) to a directory but not
on each line. The first character in the mode describes the file type that
is (‘-r’ for a normal file, ‘d’ for directory, ‘I’ for a link and so forth).
Elsewhere, a dash indicates that the entity does not have a permission.
1 At the terminal prompt,e ls -l To view the file listing with the mode included
What permission do other users have, who are in the group that
owns the file?
What permission do users have who are not in the group that
owns this file?
• You can change the permissions granted to UGO entities by using the chmod (change
• To make a change you must specify the new permission to alter. You can do so using two
methods
I. Ugo syntax
• You can use UGO syntax with the chmod command. with this style
you specify one or more entities using the letter in the Entity column
in the following table. You specify the operation, such as adding or
removing a permission, using the operators. And you specify the
permission to change.
ENTITY OPERATOR PERMISSION
U(User) + (add) R(read)
G(group) -(remove) W(write)
O(owner) =(set permission explicitly) X(execute)
A(all)
• For example, use the following command to add the write permission for your
primary group to a file named myfile
• You can assign multiple permission in a single statement. For example, use the
following command to add the write permission for yourself, add the read
permission for your primary, group, and others and remove the write permission
for your group and others.
• Write=21 =2
• Execute=20 =1
rw- rw- r—
(4+2) (4+2) (4)
6 6 4
• You add up the values of the permissions to assign. For example to assign
read and write permissions, you would use the number 6(4+2). You
assign permissions to each of the users, group and other permissions with
such numbers. For example, to assign read/write permissions to the
owner and read-only to both the group and others you use this command
• Such permissions enable the user to create, edit and delete files and
traverse directories and get directory listings.
• The group and other entities can read files, list directory contents and
traverse the directory tree.
DEFAULT PERMISSION
• When you create a new file or directory, your system must assign default permissions
to it.
• You can control what permissions your system applies by configuring the user mask.
• To start, new files are assigned the 666 permission(all have read/write permission)
and new directories have 777(all have read/write/execute permission). A unmask is a
three-digit number such as 022, which is subtracted from the starting permissions set.
• For example, lets say you create a new file and your systems unmask is
set to be 022, your file ends up with the three permissions
User=read/write
Group=read
Other=read
• If you create a new file, the permissions are
User=read/write/execute
Group=read/execute
Other=read/execute
The umask with new files and new directories
• Read, write and execute are regular Linux permissions that you assign
to files and directories. In addition, you can assign 3 additional types
of permissions collectively called the special permissions. They are:
3. Sticky bit
SUID(Set User ID)
• SUID (Set owner User ID is a special type of file permissions given to a file. Normally
in Linux when a program runs, it inherits access permissions from the logged in user.
• In simple words, users will get file owners permissions as well as owner UID and GID
when executing a file/program/command.
• This bit is present for files which have executable permissions. The setuid bit simply
indicates that when running the executable, it will set its permissions to that of the user
who created it (owner), instead of setting it to the user who launched it.
• ls -l /etc/passwd
• As we can observe, the ‘x’ is replaced by an ‘s’ in the user section of the file
permissions.
• When you run a program, it runs under your user ID and has the same
permissions that you have on the system. If the SUID bit is set for a
program when you run it, it runs under the context of the owner of the
program, not you.
• For example, to change your password you run the passwd command. It
must update the /etc/shadow file to save your password, but non-root user
don’t have permission to write that file.
• In this case the passwd command is owned by root which does have
permission to write the shadow file. The SUID bit is set. When you
run passwd command the program runs as if the root user were
running it. Therefore, the program has necessary permissions to update
the file.
SGID(Set Group ID)
• The SGID bit offers functionality similar to that of SUID bit when used
with files. A file with SGID bit set runs in the context of the owner's
group rather than in the group context of the user that ran the program.
• The setgid affects both files as well as directories. When used on a file, it
executes with the privileges of the group of the user who owns it instead
of executing with those of the group of the user who executed it.
• When the bit is set for a directory, the set of files in that directory will
have the same group as the group of the parent directory, and not that
of the user who created those files. This is used for file sharing since
they can be now modified by all the users who are part of the group of
the parent directory.
• To locate the setgid bit, look for an ‘s’ in the group section of the file
permissions, as shown in the example below.
• chmod g+s
• chmod g-s
commands
1 Enter touch specialfile We will create an executable file and set special permission on it
2 Enter chmod 777 specialfile To use the normal chmod command to assign read, write and
execute permissions for everyone
5 Enter chmod g+s specialfile To use the chmod syntax to set the SGID bit on the file
To remove the setgid bit, use the following chmod g-s sharedfolder
command.
chmod g-s
Security Risks
• When a directory has the sticky bit set, its files can be deleted or renamed only by the
file owner, directory owner and the root user. The command below shows how the
sticky bit can be set.
• chmod +t
• Simply look for a ‘t’ character in the file permissions to locate the
sticky bit. The snippet below shows how we can set the sticky bit for
some directory “power’, and how it prevents the new user from
deleting a file in the directory.
• To remove the sticky bit, simply use the following command.
• chmod -t
• Since deleting a file is controlled by the write permission of the file, practical
uses of the sticky bit involve world-writable directories such as ‘/tmp’ so that
the delete permissions are reserved only for the owners of the file.
HOW STICKY BIT WORKS
Press ls -l
Make a new directory using mkdir command To do this, create a directory on your system using
mkdir command. Eg mkdir power
ls –l | grep power (directory name)
The command shows how the sticky bit can be set. chmod +t | grep power (directory name)
chmod +t
chmod -t | grep power (directory name)
To remove the sticky bit, simply use the following
command.
chmod -t
NOTE