Home > Articles

File and directory permissions

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.”

Pearson IT Certification Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Pearson IT Certification and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Pearson IT Certification products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.pearsonitcertification.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020