Home > Articles

File and directory permissions

  • Print
  • + Share This

In this sample chapter from CompTIA Linux+ Portable Command Guide: All the commands for the CompTIA XK0-004 exam in one compact, portable resource, 2nd Edition, explore security commands and, given a scenario, apply or acquire the appropriate user and/or group permissions and ownership.

This chapter is from the book

This chapter provides information and commands concerning the following topics:

File and directory permissions

  • Read, write, execute

  • User, group, other

  • SUID

  • Octal notation

  • umask

  • Sticky bit

  • GUID

  • Inheritance

  • Utilities (chmod, chown, chgrp, getfacl, setfacl, ls, ulimit, chage)

Context-based permissions

  • SELinux configurations (disabled, permissive, enforcing)

  • SELinux policy (targeted)

  • SELinux tools (setenforce, getenforce, sestatus, setsebool, getsebool, chcon, restorecon, ls -Z, ps -Z)

  • AppArmor (aa-disable, aa-complain, aa-unconfined, /etc/apparmor.d/, /etc/apparmor.d/tunables)

Privilege escalation

  • su

  • sudo

  • wheel

  • visudo

  • sudoedit

User types

  • Root

  • Standard

  • Service

File and Directory Permissions

The user who owns a file or directory has the ability to allow or deny access to the file or directory using permissions. Additionally, the root user has the ability to change the permissions of any file or directory on the system. This section focuses on these permissions and how to apply them.

Read, Write, Execute

Every file and directory has standard permissions (also called “read, write, and execute” permissions) that either allow or disallow a user access. Using these standard permissions is something that every Linux user should understand how to do, as this is the primary way a user will protect his files from other users.

To view the permissions of a file or directory, use the ls -l command:

[student@OCS ~]$ ls -l /etc/chrony.keys
-rw-r-----. 1 root chrony 62 May  9  2018 /etc/chrony.keys

The first ten characters of the output denote the file type (recall that a hyphen [] as the character in the first position denotes a plain file, whereas a d denotes a directory) and the permissions for the file. Permissions are broken into three sets: the user owner of the file (root in the previous example), the group owner (chrony), and all other users (referred to as “others”).

Each set has three possible permissions: read (symbolized by r), write (w), and execute (x). If the permission is set, the character that symbolizes the permission is displayed. Otherwise, a hyphen () character is displayed to indicate that permission is not set. Thus, r-x means “read and execute are set, but write is not set.”

What read, write, and execute permissions really mean depends on whether the object is a file or directory. For files, these permissions mean the following:

  • Read: Can view or copy file contents.

  • Write: Can modify file contents.

  • Execute: Can run the file like a program. After you create a program, you must make it executable before you can run it.

For directories, they mean the following:

  • Read: Can list files in the directory.

  • Write: Can add and delete files in the directory (requires execute permission).

  • Execute: Can “cd” into the directory or use it in a pathname.

User, Group, Other

See the “Read, Write, Execute” subsection in this chapter.

SUID

The following table describes the special permission sets of suid, sgid, and the sticky bit:

 

suid

sgid

Sticky Bit

Description

When set on executable files, suid allows a program to access files using the permissions of the user owner of the executable file.

When set on executable files, sgid allows a program to access files using the permissions of the group owner of the executable file.

When it’s set on directories, all new files in the directory inherit the group ownership of the directory.

When the sticky bit is set on directories, files in the directory can only be removed by the user owner of the file, the owner of the directory, or the root user.

Set

chmod u+s file

or

chmod 4xxx file

(xxx refers to regular read, write, and execute permissions.)

chmod g+s file

or

chmod 2xxx file

(xxx refers to regular read, write, and execute permissions.)

chmod o+t file

or

chmod 1xxx file

(xxx refers to regular read, write, and execute permissions).

Note: Sticky bit permissions are almost always set to the octal value of 1777.

Remove

chmod u-s file

or

chmod 0xxx file

chmod g-s file

or

chmod 0xxx file

chmod o-t file

or

chmod 0xxx file

See the “chmod” subsection in this chapter for details about the chmod command.

Octal Notation

See the “chmod” subsection in this chapter for details about octal notation.

umask

The umask command sets default permissions for files and directories. These default permissions are applied only when the file or directory is initially created.

The umask command accepts one argument: the mask value. The mask value is an octal value that is applied against the maximum possible permissions for new files or new directories, as shown in the following table:

Type

Maximum Possible Permission for New Item

File

rw-rw-rw-

Directory

rwxrwxrwx

Figure 15.1 describes how a umask value of 027 would apply to new files versus how it would apply to new directories.

FIGURE 15.1

FIGURE 15.1 How umask Is Applied

Sticky Bit

See the “SUID” subsection in this chapter for details about the sticky bit.

GUID

See the “SUID” subsection in this chapter for details about GUID.

Inheritance

Unlike in some operating systems, basic and advanced Linux permissions don’t utilize inheritance. The idea of inheritance is when a new file or directory inherits the permissions from the directory that the item is created in.

There is a way to have permissions inherit from the parent directory: by using ACLs (access control lists). See the “setfacl” subsection for details.

Utilities

Several utilities or commands allow you to manage permissions. The utilities that are testable for the Linux+ exam are covered in this section.

chmod

The chmod command is used to change permissions on files. It can be used in two ways: symbolic method and octal method. With the octal method, the permissions are assigned numeric values:

  • Read = 4

  • Write = 2

  • Execute = 1

With these numeric values, one number can be used to describe an entire permission set:

  • 7 = rwx

  • 6 = rw-

  • 5 = r-x

  • 4 = r--

  • 3 = -wx

  • 2 = -w-

  • 1 = --x

  • 0 = ---

So, to change the permissions of a file to rwxr-xr--, you would execute the following command:

chmod 754 filename

The following table demonstrates some examples using the octal method:

Example

Description

chmod 755 file

Sets the permissions of rwxr-xr-x.

chmod 511 file

Sets the permissions of r-x--x--x.

chmod 600 file

Sets the permissions of rw-------.

With octal permissions, you should always provide three numbers, which will change all the permissions. But, what if you only want to change a single permission of the set? For that, use the symbolic method by passing three values to the chmod command, as shown in the following table:

Who

What

Permission

u = user owner

+

r

g = group owner

-

w

o = other

=

x

a = all sets

The following demonstrates adding execute permission to all three sets (user owner, group owner, and others) using the symbolic method:

[student@OCS ~]$ ls -l display.sh
-rw-rw-r--. 1 student student 291 Apr 30 20:09 display.sh
[student@OCS ~]$ chmod a+x display.sh
[student@OCS ~]$ ls -l display.sh
-rwxrwxr-x. 1 student student 291 Apr 30 20:09 display.sh

Here are some important options for the chmod command:

Option

Description

-R

Recursively apply changes to an entire directory structure.

-v

Verbose. Produce output demonstrating the changes that are made.

chown

The chown command is used to change the user owner or group owner of a file or directory. The following table demonstrates different ways to use this command:

Example

Description

chown tim abc.txt

Changes the user owner of the abc.txt file to tim user.

chown tim:staff abc.txt

Changes the user owner of the abc.txt file to tim user and the group owner to the staff group.

chown :staff abc.txt

Changes the group owner of the abc.txt file to the staff group.

Here are some important options for the chown command:

Option

Description

-R

Recursively apply changes to an entire directory structure.

--reference=file

Change the user and group owner to the ownership of file.

-v

Verbose. Produce output demonstrating the changes that are made.

chgrp

The chgrp command is designed to change the group ownership of a file. The syntax of this command is chgrp [options] group_name file. In the following example, the group ownership of the abc.txt file is changed to the staff group:

[student@OCS ~]$ chgrp staff abc.txt

Here are some important options for the chgrp command:

Option

Description

-R

Recursively apply changes to an entire directory structure.

--reference=file

Change the user and group owner to the ownership of file.

-v

Verbose. Produce output demonstrating the changes that are made.

getfacl

See the “setfacl” subsection next.

setfacl

ACLs (access control lists) allow the owner of a file to give permissions for specific users and groups. The setfacl command is used to create an ACL on a file or directory:

sarah@OCS:~$ setfacl -m user:dane:r-- sales_report

The -m option is used to make a new ACL for the file. The format of the argument to the -m option is what:who:permission. The value for what can be one of the following:

  • user or u when applying an ACL for a specific user.

  • group or g when applying an ACL for a specific group.

  • others or o when applying an ACL for “others.”

  • mask or m when setting the mask for the ACL. (The mask will be explained later in this section.)

The value for who will be the user or group to which the permission will be applied. The permission can be provided as either a symbolic value (r--) or an octal value (4).

Once an ACL has been applied on a file or directory, a plus sign (+) character will appear after the permissions when the ls -l command is executed, as shown here:

sarah@OCS:~$ ls -l sales_report
-rw-rw-r--+ 1 sarah sales 98970 Dec 27 16:45 sales_report

To view the ACL, use the getfacl command:

sarah@OCS:~$ getfacl sales_report
# file: sales_report
# owner: sarah
# group: sarah
user::rw-
user:william:r--
group::rw-
mask::rw-
other::r--

The following example demonstrates setting an ACL for a group:

sarah@OCS:~$ setfacl -m g:games:6 sales_report
sarah@OCS:~$ getfacl sales_report
# file: sales_report
# owner: sarah
# group: sarah
user::rw-
user:william:r--
group::rw-
group:games:rw-
mask::rw-
other::r--

For regular permissions, the umask value is used to determine the default permissions applied for new files and directories. For ACLs, you can define a default ACL set for all new files and directories that are created within a shell by using the -m option with the setfacl command. In this case, the following syntax is used for the argument: default:what:who:permission.

The following example will create a default ACL for the reports directory:

sarah@OCS:~$ mkdir reports
sarah@OCS:~$ setfacl -m default:g:games:r-x reports
sarah@OCS:~$ setfacl -m default:u:bin:rwx reports
sarah@OCS:~$ getfacl reports
# file: reports
# owner: sarah
# group: sarah
user::rwx
group::rwx
other::r-x
default:user::rwx
default:user:bin:rwx
default:group::rwx
default:group:games:r-x
default:mask::rwx
default:other::r-x

The following example demonstrates how new files and directories will inherit the ACLs that were created in the commands executed in the previous example:

sarah@OCS:~$ mkdir reports/test
sarah@OCS:~$ getfacl reports/test
# file: reports/test
# owner: sarah
# group: sarah
user::rwx
user:bin:rwx
group::rwx
group:games:r-x
mask::rwx
other::r-x
default:user::rwx
default:user:bin:rwx
default:group::rwx
default:group:games:r-x
default:mask::rwx
default:other::r-x
sarah@OCS:~$ touch reports/sample1
sarah@OCS:~$ getfacl reports/sample1
# file: reports/sample1
# owner: sarah
# group: sarah
user::rw-
user:bin:rwx                    #effective:rw-
group::rwx                      #effective:rw-
group:games:r-x                 #effective:r--
mask::rw-
other::r--

Here are some important options for the setfacl command:

Option

Description

-b

Remove all ACLs.

-d

Set a default ACL on a directory; this will be inherited by any new file or directory created with this directory.

-R

Apply recursively.

ls

See the “Read, Write, Execute” subsection in this chapter to see how the ls command is important for displaying permissions.

ulimit

The ulimit command lists or sets a user’s account limits:

[root@OCS ~]# ulimit -a
core file size                 (blocks, -c)   0
data seg size                  (kbytes,   -d)   unlimited
scheduling priority            (-e)    0
file size                      (blocks, -f)   unlimited
pending signals                (-i)    15439
max locked memory              (kbytes, -l)   64
max memory size                (kbytes, -m)     unlimited
open files                     (-n)      1024
pipe size                      (512 bytes, -p)   8
POSIX message queues           (bytes, -q)   819200
real-time priority             (-r)   0
stack size                     (kbytes, -s)   8192
cpu time                       (seconds, -t)  unlimited
max user processes             (-u)  4096
virtual memory                 (kbytes, -v)  unlimited
file locks                     (-x)  unlimited

These limits are normally configured by the system administrator using a PAM (Pluggable Authentication Modules) configuration file:

[root@OCS ~]# tail -n 12 /etc/security/limits.conf
#<domain>          <type>            <item>            <value>
#

#*                 soft              core              0
#*                 hard              rss               10000
#@student          hard              nproc             20
#@faculty          soft              nproc             20
#@faculty          hard              nproc             50
#ftp               hard              nproc             0
# End of file

For example, you may want to limit how many concurrent logins an account can have:

student            -            maxlogins           4

Users rarely use the ulimit command to limit their own account, so the options for this command are not as important as understanding what the output displays. Additionally, some of the limits are very rarely used. The commonly used limits are described in the following table:

Limit

Description

fsize

Maximum file size allowed in memory

cpu

Maximum CPU time allowed

nproc

Maximum number of concurrently running processes

maxlogins

Maximum number of concurrent logins

chage

See the “chage” section in Chapter 8, “Given a scenario, manage users and groups.”

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.