What is File permissions?

On Linux and similar systems, the setting for each file and directory that decides whether read (r), write (w) and execute (x) are allowed for the owner (user), the group and others. ls -l shows it as rwxr-xr--, and chmod also expresses it in octal such as 754 (the sum of r = 4, w = 2, x = 1). The permissions of newly created files are decided by the umask. root (the superuser) can generally read and write files regardless of these restrictions.

Japanese name: パーミッション

File permissions appears in 6 practice questions across 1 topic, including Linux and Command-Line Basics.

Field: Linux and Command-Line Basics

Reading the notation

Of the first 10 characters of ls -l, the first is the file type (- for a regular file, d for a directory, l for a symbolic link), and each following group of three is the permission of the owner, the group and others. In octal each digit is the sum of r = 4, w = 2 and x = 1, so rwxr-xr-- is 754 and rw-r--r-- is 644. Besides specifying numbers, as in chmod 754 file, symbols can change only part of it, as in chmod g+w file or chmod o-r file.

Directory permissions

For a directory, r is permission to read the list of names in it, w to create and delete files in it, and x to enter it and reach files by name. So whether a file can be deleted is decided not by the file itself but by the w and x of the directory. A directory that anyone can write to, such as /tmp, is given the sticky bit (t) so that people cannot delete other people's files.

Special bits and umask

There is setuid, which runs a program with the owner's privileges (s in the owner's x position), setgid, which runs it with the group's privileges, and the sticky bit mentioned above, expressed in octal as the 4000, 2000 and 1000 places. New files are created from 666 and directories from 777 with the umask bits removed, so with umask 022 files become 644 and directories 755.

Try it: questions about File permissions

touch creates a new file from 666, and mkdir a new directory from 777, with the umask bits removed. Which of the following is the correct pair of permissions when a new file and a new directory are created in a shell whose umask is 027?

  1. Afile 637, directory 750
  2. Bfile 640, directory 750
  3. Cfile 750, directory 750
Show answer and explanation

Answer: B. file 640, directory 750

The umask gives the permission bits to "remove" from newly created files and directories. 027 removes write (2) from the group and read, write and execute (7) from others, so a file becomes rw-r----- (640) from 666 and a directory becomes rwxr-x--- (750) from 777. This is not octal subtraction but bitwise removal (666 AND NOT 027). With the commonly used umask 022, the same calculation gives 644 for files and 755 for directories.

The directory d has permissions rwx--x--x (711), and the file f.txt in it has rw-r--r-- (644). A user who is neither the owner nor the superuser runs "ls d" and "cat d/f.txt". Which of the following is the result?

  1. Als d shows the listing, but cat d/f.txt is not permitted because the directory permissions are insufficient
  2. BNeither ls d nor cat d/f.txt is permitted
  3. Cls d is not permitted, but cat d/f.txt shows the contents
Show answer and explanation

Answer: C. ls d is not permitted, but cat d/f.txt shows the contents

For a directory, r is permission to read the list of names in it, and x is permission to enter it and reach a file by name. Others have only x, so ls d gives "Permission denied", but d/f.txt, whose name is known, can be reached. f.txt itself grants r to others, so cat can show its contents. Conversely, in a directory with r but no x, the names can be listed but the files in it cannot be opened.

FAQ

Does chmod 777 solve the problem?

"Permission denied" may disappear, but anyone can then modify or run the file, and other users or programs may change its contents. The basic approach is to first check which users need to do which operations, match the owner and group with chown and chgrp, and grant only the permissions needed.

How can I let only my team members read files on a shared server?

Create a group for the team, add the members, set the group of the files and directories to that group, and give the group r (r and x for directories) and others nothing (for example 640 for files and 750 for directories). To set permissions finely per person, ACLs (setfacl, getfacl) can also be used.

File permissions in the explanations

Questions that mention File permissions (6)

  1. touch creates a new file from 666, and mkdir a new directory from 777, with the umask bits removed. Which of the following is the correct pair of permissions when a new file and a new directory are created in a shell whose umask is 027?
  2. The directory d has permissions rwx--x--x (711), and the file f.txt in it has rw-r--r-- (644). A user who is neither the owner nor the superuser runs "ls d" and "cat d/f.txt". Which of the following is the result?
  3. In the home directory, "ls" shows only notes.txt, while "ls -a" shows ".", "..", ".bashrc" and "notes.txt". Which of the following correctly explains this result?
  4. After "chmod 754 run.sh" is run on the file run.sh, which of the following is the permission notation shown at the start of the line from "ls -l run.sh"?
  5. Which of the following correctly describes the sudo command on Linux?
  6. You created a public-key authentication key pair with ssh-keygen on your own PC and want to connect to the server server1 with ssh without a password. Which of the following is the correct way to place the keys?

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Superuser Shell Principle of least privilege SSH cron Environment variable Git grep Hard link I/O redirection

Back to the glossary