Home > Articles > Open Source > Linux/UNIX/Open Source Operating Systems

From the Rough Cut

Working with Permissions

Under Linux (and UNIX), everything in the file system, including directories and devices, is a file. And every file on your system has an accompanying set of permissions based on ownership. These permissions provide data security by giving specific permission settings to every single item denoting who may read, write, and/or execute the file. These permissions are set individually for the file’s owner, for members of the group the file belongs to, and for all others on the system.

You can examine the default permissions for a file you create by using the umask command, which lists default permissions using the number system explained next, or by using the touch command and then the ls command’s long-format listing like this:

matthew@seymour:~$ touch file
matthew@seymour:~$ ls -l file
-rw-r--r-- 1 matthew matthew 0 2010-06-30 13:06 file

In this example, the touch command is used to quickly create a file. The ls command then reports on the file, displaying the following (from left to right):

  • The type of file created—Common indicators of the type of file are in the leading letter in the output. A blank (which is represented by a dash, as in the preceding example) designates a plain file, d designates a directory, c designates a character device (such as /dev/ttyS0), and b is used for a block device (such as /dev/sda).
  • Permissions—Read, write, and execute permissions for the owner, group, and all others on the system. (You learn more about these permissions later in this section.)
  • Number of links to the file—The number one (1) designates that there is only one file, whereas any other number indicates that there might be one or more hard-linked files. Links are created with the ln command. A hard-linked file is an exact copy of the file, but it might be located elsewhere on the system. Symbolic links of directories can also be created, but only the root operator can create a hard link of a directory.
  • The owner—The account that owns the file; this is originally the file creator, but you can change this designation using the chown command.
  • The group—The group of users allowed to access the file; this is originally the file creator’s main group, but you can change this designation using the chgrp command.
  • File size and creation/modification date—The last two elements indicate the size of the file in bytes and the date the file was created or last modified.

Assigning Permissions

Under Linux, permissions are grouped by owner, group, and others, with read, write, and execute permission assigned to each, like so:

Owner    Group    Others
rwx      rwx      rxw

Permissions can be indicated by mnemonic or octal characters. Mnemonic characters are

  • r indicates permission for an owner, member of the owner’s group, or others to open and read the file.
  • w indicates permission for an owner, member of the owner’s group, or others to open and write to the file.
  • x indicates permission for an owner, member of the owner’s group, or others to execute the file (or read a directory).

In the previous example for the file named file, the owner, matthew, has read and write permission. Any member of the group named matthew may only read the file. All other users may only read the file. Also note that default permissions for files created by the root operator (while using sudo or a root account) will be different because of umask settings assigned by the shell.

Many users prefer to use numeric codes, based on octal (base 8) values, to represent permissions. Here’s what these values mean:

  • 4 indicates read permission.
  • 2 indicates write permission.
  • 1 indicates execute permission.

In octal notation, the previous example file has a permission setting of 664 (read + write or 4 + 2, read + write or 4 + 2, read-only or 4). Although you can use either form of permissions notation, octal is easy to use quickly after you visualize and understand how permissions are numbered.

Directory Permissions

Directories are also files under Linux. For example, again use the ls command to show permissions like this:

matthew@seymour:~$ mkdir directory
matthew@seymour:~$ ls -ld directory
drwxr-xr-x  2 matthew  matthew  4096 2010-06-30 13:23 directory

In this example, the mkdir command is used to create a directory. The ls command and its -ld option is used to show the permissions and other information about the directory (not its contents). Here you can see that the directory has permission values of 755 (read + write + execute or 4 + 2 + 1, read + execute or 4 + 1, and read + execute or 4 + 1).

This shows that the owner can read and write to the directory and, because of execute permission, also list the directory’s contents. Group members and all other users can list only the directory contents. Note that directories require execute permission for anyone to be able to view their contents.

You should also notice that the ls command’s output shows a leading d in the permissions field. This letter specifies that this file is a directory; normal files have a blank field in its place. Other files, such as those specifying a block or character device, have a different letter.

For example, if you examine the device file for a Linux serial port, you will see:

matthew@seymour:~$ ls -l /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 2010-06-30 08:13 /dev/ttyS0

Here, /dev/ttyS0 is a character device (such as a serial communications port and designated by a c) owned by root and available to anyone in the dialout group. The device has permissions of 660 (read + write, read + write, no permission).

On the other hand, if you examine the device file for an IDE hard drive, you see:

matthew@seymour:~$ ls -l /dev/sda
brw-rw-- -- 1 root disk 8, 0 2010-06-30 08:13 /dev/sda

In this example, b designates a block device (a device that transfers and caches data in blocks) with similar permissions. Other device entries you will run across on your Linux system include symbolic links, designated by s.

Altering File Permissions with chmod

You can use the chmod command to alter a file’s permissions. This command uses various forms of command syntax, including octal or a mnemonic form (such as u, g, o, or a and rwx, and so on) to specify a desired change. The chmod command can be used to add, remove, or modify file or directory permissions to protect, hide, or open up access to a file by other users (except for the root account or a user with super-user permission and using sudo, either of which can access any file or directory on a Linux system).

The mnemonic forms of chmod’s options are (when used with a plus character, +, to add, or a minus sign, -, to remove):

  • u—Adds or removes user (owner) read, write, or execute permission
  • g—Adds or removes group read, write, or execute permission
  • o—Adds or removes read, write, or execute permission for others not in a file’s group
  • a—Adds or removes read, write, or execute permission for all users
  • r—Adds or removes read permission
  • w—Adds or removes write permission
  • x—Adds or removes execution permission

For example, if you create a file, such as a readme.txt, the file will have default permissions (set by the umask setting in /etc/bashrc) of

-rw-r--r-- 1 matthew matthew 0 2010-06-30 13:33 readme.txt

As you can see, you can read and write the file. Anyone else can only read the file (and only if it is outside your home directory, which will have read, write, and execute permission set only for you, the owner). You can remove all write permission for anyone by using chmod, the minus sign, and aw like so:

matthew@seymour:~$ chmod a-w readme.txt
matthew@seymour:~$ ls -l readme.txt
-r--r--r-- 1 matthew matthew 0 2010-06-30 13:33 readme.txt

Now, no one can write to the file (except you, if the file is in your home or /tmp directory because of directory permissions). To restore read and write permission for only you as the owner, use the plus sign and the u and rw options like so:

matthew@seymour:~$ chmod u+rw readme.txt
matthew@seymour:~$ ls -l readme.txt
-rw-r--r-- 1 matthew matthew 0 2010-06-30 13:33 readme.txt

You can also use the octal form of the chmod command, for example, to modify a file’s permissions so that only you, the owner, can read and write a file. Use the chmod command and a file permission of 600, like this:

matthew@seymour:~$ chmod 600 readme.txt
matthew@seymour:~$ ls -l readme.txt
-rw------- 1 matthew matthew 0 2010-06-30 13:33 readme.txt

If you take away execution permission for a directory, files might be hidden inside and may not be listed or accessed by anyone else (except the root operator, of course, who has access to any file on your system). By using various combinations of permission settings, you can quickly and easily set up a more secure environment, even as a normal user in your home directory.

File Permissions with chgrp

You can use the chgrp command to change the group to which a file belongs.

matthew@seymour:~$ chgrp wheel filename

Changing File Permissions with chown

You can use the chown command to change the owner of a file.

matthew@seymour:~$ chown matthew filename

You can also use the chown command to change the group of a file at the same time.

matthew@seymour:~$ chown matthew:wheel filename

Understanding Set User ID and Set Group ID Permissions

Two more types of permission are “set user ID”, known as suid, and “set group ID,” or sgid, permissions. These settings, when used in a program, enable any user running that program to have program owner or group owner permissions for that program. These settings enable the program to be run effectively by anyone, without requiring that each user’s permissions be altered to include specific permissions for that program.

One commonly used program with suid permissions is the passwd command:

matthew@seymour:~$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 42856 2010-01-26 10:09 /usr/bin/passwd

This setting allows normal users to execute the command (as root) to make changes to a root-only accessible file, /etc/passwd.

You also can assign similar permission with the chfn command. This command allows users to update or change finger information in /etc/passwd. You accomplish this permission modification by using a leading 4 (or the mnemonic s) in front of the three octal values.

Files or programs that have suid or guid permissions can sometimes present security holes because they bypass normal permissions. This problem is compounded if the permission extends to an executable binary (a command) with an inherent security flaw because it could lead to any system user or intruder gaining root access. In past exploits, this typically happened when a user fed a vulnerable command with unexpected input (such as a long pathname or option); the command would fail, and the user would be presented a root prompt. Although Linux developers are constantly on the lookout for poor programming practices, new exploits are found all the time, and can crop up unexpectedly, especially in newer software packages that haven’t had the benefit of peer developer review.

Savvy Linux system administrators keep the number of suid or guid files present on a system to a minimum. The find command can be used to display all such files on your system:

matthew@seymour:~$ sudo find / -type f -perm /6000 -exec ls -l {} \;

Note that the programs do not necessarily have to be removed from your system. If your users really do not need to use the program, you can remove the programs execute permission for anyone. You have to decide, as the root operator, whether your users are allowed, for example, to mount and unmount CD-ROMs or other media on your system. Although Linux-based operating systems can be set up to accommodate ease of use and convenience, allowing programs such as mount to be suid might not be the best security policy. Other candidates for suid permission change could include the chsh, at, or chage commands.Working with Files

Managing files in your home directory involves using one or more easily remembered commands.

Creating a File with touch

To create an empty file called filename within your current directory:

matthew@seymour:~$ touch filename

To edit this file, you will need to use a text editor. Several are discussed in Chapter 11. However, it is sometimes useful to create an empty file as this creates an access record because of the time and date information that is connected to the file. You can also use touch to update this information, called a timestamp, without otherwise accessing or modifying a file.

You can create a file in a different location by changing what is after touch. To create a new file in /home/matthew/randomdirectory, if I was already in my home directory, I could use:

matthew@seymour:~$ touch randomdirectory/newfile

or from anywhere using an absolute path:

matthew@seymour:~$ touch /home/matthew/randomdirectory/newfile

or from anywhere using a path shortcut:

matthew@seymour:~$ touch ~/randomdirectory/newfile

Creating a Directory with mkdir

To create an empty directory called newdirectory within your current directory:

matthew@seymour:~$ mkdir newdirectory

You can create a directory in a different location by changing what is after mkdir. To create a new directory in /home/matthew/music, if I was already in my home directory, I could use:

matthew@seymour:~$ mkdir music/newdirectory

or from anywhere using an absolute path:

matthew@seymour:~$ mkdir /home/matthew/music/newdirectory

or from anywhere using a path shortcut:

matthew@seymour:~$ mkdir ~/music/newdirectory

Deleting a Directory with rmdir

To delete an empty directory named directoryname:

matthew@seymour:~$ rmdir directoryname

You can remove a directory in a different location by changing what is after rmdir. To remove a directory in /home/matthew/music, if I was already in my home directory, I could use:

matthew@seymour:~$ rmdir music/directoryname

or from anywhere using an absolute path:

matthew@seymour:~$ rmdir /home/matthew/music/directoryname

or from anywhere using a path shortcut:

matthew@seymour:~$ rmdir ~/music/directoryname

The directory must be empty to be removed using rmdir. However, there is a way to remove a directory with its contents using rm.

Deleting a File or Directory with rm

To delete a file named filename:

matthew@seymour:~$ rm filename

You can remove a directory in a different location by changing what is after rm. To remove a directory in /home/matthew/randomdirectory, if I was already in my home directory, I could use:

matthew@seymour:~$ rm randomdirectory/filename

or from anywhere using an absolute path:

matthew@seymour:~$ rm /home/matthew/randomdirectory/filename

or from anywhere using a path shortcut:

matthew@seymour:~$ rm ~/randomdirectory/filename

If you try to use rm to remove an empty directory, you will receive an error message: rm: cannot remove `random/': Is a directory. In this case, you must use rmdir. However, you can remove a directory end its contents using rm.

Moving or Renaming a File with mv

In Linux land, moving and renaming a file are the same thing. It doesn't matter whether you are moving the directory to another or from one filename to another filename in the same directory, there is only one command to remember. To move a file named filename from ~/documents to ~/archive:

matthew@seymour:~$ mv documents/filename archive

Notice that the filename is not included in the destination. The destination here must be an existing directory. If it is not, the file will be renamed to the term used. Some examples will make this clear.

To rename a file that is in my current directory I could use:

matthew@seymour:~$ mv oldfilename newfilename

To rename a file as I move it from ~/documents to ~/archive:

matthew@seymour:~$ mv documents/oldfilename archive/newfilename

or from anywhere using an absolute path:

matthew@seymour:~$ mv /home/matthew/documents/oldfilename /home/matthew/archive/newfilename

or from anywhere using a path shortcut:

matthew@seymour:~$ rm ~/documents/oldfilename ~/archive/newfilename

Copying a File with cp

Copying works similarly to moving, but retains the original in the original location. To copy a file named filename from ~/documents to ~/archive:

matthew@seymour:~$ cp documents/filename archive

Notice that the filename is not included in the destination. The destination here must be an existing directory. If it is not, the file will be renamed to the term used. Some examples will make this clear.

To copy a file that is in my current directory I could use, and it will work exactly the same as mv, except that both files will exist afterward:

matthew@seymour:~$ cp oldfilename newfilename

To rename a file as I copy it from ~/documents to ~/archive:

matthew@seymour:~$  cp documents/oldfilename archive/newfilename

or from anywhere using an absolute path:

matthew@seymour:~$ cp /home/matthew/documents/oldfilename /home/matthew/archive/newfilename

or from anywhere using a path shortcut:

matthew@seymour:~$ cp ~/documents/oldfilename ~/archive/newfilename

Displaying the Contents of a File with cat

To view the contents of a text file named filename on your screen:

matthew@seymour:~$ cat filename

Notice that the text is displayed on your screen, but that you can not edit or work with the text in any way. This command is convenient when you want to know the contents of a file, but don't need to make any changes. Text editors for the terminal are covered in Chapter 11. This command works best with short files because the contents of longer files will scroll off of the screen too quickly to be read.

Displaying the Contents of a File with less

When you need to view the contents of a longer text file from the command line, you can use less. This produces a paged output, meaning that output will stop each time your screen is full. You can then use your up and down arrow keys and page up and page down keys to scroll through the contents of the file. Then, use q to quit and return to the command line.

matthew@seymour:~$ less filename

There was a program that did gave paged output in the early days of UNIX called more. It was the first paged output program, but did not include the ability to scroll up and down. less was written to add that capability and was named as a bit of hacker humor, because “ less is more.” You can also use more, but today it is merely an alias for less.

Using Wildcards and Regular ExpressionsEach of these commands can be used with pattern-matching strings known as wildcards or regular expressions. For example, to delete all files in the current directory beginning with the letters abc, you can use an expression beginning with the first three letters of the desired filenames. An asterisk (*) is then appended to match all these files. Use a command line with the rm command like this:

matthew@seymour:~$ rm abc*

Linux shells recognize many types of file naming wildcards, but this is different from the capabilities of Linux commands supporting the use of more complex expressions. You learn more about using wildcards in in Chapter 11, “Command Line Masterclass,” and in Chapter 13, “Automating Tasks.”

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT 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 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 InformIT. 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.informit.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