Skip to content
ImagineWorld

Ownership and Permissions

node_idnode-book_the-software-developer-s-guide-to-linux_chapter-06-ownership-and-permissions
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num6
chapter_titleOwnership and Permissions
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

In this chapter, you’ll learn how users and groups are combined with ownership and permissions to create the basic Linux security model. This combination of primitives is used to control access to just about everything on a Linux system – processes, files, network sockets, devices, and more.

First, you’ll get a tour of all the important file information you get from a long listing (with an emphasis on permissions, naturally). Then we’ll cover the common permissions that you’ll encounter on production Linux systems, and finally, we’ll show you all the Linux commands you’ll use to set and modify permissions on files. Together, we’ll do the following:

  • Decipher the output of a long listing
  • Learn about file attributes
  • Understand how ownership and permissions work
  • Understand a common sticking point, the “octal” permission format
  • Learn practical commands for changing ownership and permissions

Let’s dive into this topic by way of a long listing.

Sometimes, when you’re navigating around a system, seeing only file and directory names isn’t enough. When you want more information about the files you’re seeing, use ls with its “long” option: ls -l.

Here is an example of its output when run on the /lib directory of a system. Open a terminal and type ls -l /lib/:

# ls -l /lib/
total 56
drwxr-xr-x 10 root root 4096 Mar 8 02:12 aarch64-linux-gnu
drwxr-xr-x 5 root root 4096 Mar 8 02:12 apt
drwxr-xr-x 3 root root 4096 Mar 8 02:08 dpkg
drwxr-xr-x 2 root root 4096 Mar 8 02:12 init
lrwxrwxrwx 1 root root 39 Jul 6 2022 ld-linux-aarch64.so.1 -> aarch64-linux-gnu/ld-linux-aarch64.so.1
drwxr-xr-x 3 root root 4096 Mar 4 2022 locale
drwxr-xr-x 3 root root 4096 Mar 8 02:12 lsb
drwxr-xr-x 3 root root 4096 Aug 29 2021 mime
-rw-r--r-- 1 root root 386 Feb 16 2023 os-release
drwxr-xr-x 2 root root 4096 Mar 8 02:12 sysctl.d
drwxr-xr-x 3 root root 4096 Apr 18 2022 systemd
drwxr-xr-x 16 root root 4096 Jan 17 2022 terminfo
drwxr-xr-x 2 root root 4096 Mar 8 02:12 tmpfiles.d
drwxr-xr-x 3 root root 4096 Mar 8 02:12 udev
drwxr-xr-x 3 root root 4096 Mar 8 02:12 usrmerge

We see a lot of interesting information here – let’s step through it, field by field.

The first field displays a file’s attributes: file type and permissions. In other words, this field shows us which type of file we’re looking at, and what its file permissions are. The default lists this information in symbolic mode, in contrast to numeric mode, which you can view with -n.

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

The first character here indicates the file type. In the listing above, the - character indicates a regular file. Lines starting with l indicate a symbolic link, which is simply a special file that has no content of its own, and just points to another location on the filesystem. You can think of it as a Windows shortcut or a macOS file alias.

Other common file types are d, indicating a directory, or c, which indicates that you’re looking at a character file – you’ll mainly find the latter in /dev, representing hardware input devices such as keyboards. For more on file types, see the Filesystem section of Chapter 5, Introducing Files.

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

These are the “permission bits,” which determine which users and groups on the system are allowed to read, write, and execute this file. We’ll dive deeper into this in the Permissions section of this chapter.

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

The next field indicates the number of hard links. Hard links are special pointers linking file names to an actual file. So in most situations, this will be 1 for files. In contrast to a symbolic link (“symlink”), which points to a filepath, a hardlink points to an actual file. If you move the file that a symlink points to, this makes the symlink invalid; a hardlink will keep pointing to a file even if that file is moved around, renamed, or otherwise modified.

You may have noticed that while most files have only one link pointing to them, directories have wildly varying numbers of links in this column. This is because each file and directory inside of that directory creates another link reference to it. Even an empty directory starts with two links, “.” (the shortcut for “this directory”) and “..” (the shortcut for “the directory above this one”).

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

The third field shows which user owns the file; all files in our example are owned by root. While you see a username in this example, running ls in numeric mode – ls -ln – will also show a numeric user ID instead of the friendly username you see here.

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

The following field shows the owning group, which also happens to be root in the example listing above.

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

As you might have guessed, the next field displays the file size. When no additional flags are specified, it is displayed in bytes. To make it more readable the -h (“human-readable”) flag can be used.

If you’re paying attention, you’ll have noticed that directories all seem to have the same file size, 4096:

drwxr-xr-x 1 root root 4096 Apr 18 2022 systemd

Files can take up as much or as little space as they need, but directory storage is allocated in discrete filesystem blocks. Because the smallest blocksize on most filesystems is 4096, directories will report their size as 4096.

This rabbit hole goes deeper, but we don’t think the information will be useful enough in your day-to-day work as a software engineer to warrant including it here. If you’re still curious and want to go deeper, start reading about “Linux inodes.”

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

Next, we see the modification timestamp of the file – the last time this file was changed.

-rw-r--r-- 1 root root 386 Feb 16 2023 os-release

Finally, we come to the filename, which is the only thing you’d see if you were doing a regular listing instead of a long listing (ls instead of ls -l). This will usually just be a plain file or directory name, except in the case of a symbolic link, or symlink – in that case, you’ll see the symlink name, and the file path that it links to.

To change the owner of files or directories, use the chown (change owner) command. The syntax is chown user:group path where user is the name of the owning user and group is the name of the group, while path is the full or relative part to the file or directory.

You can omit the colon and the group to just change the owning user and leave group ownership alone. Of course, the user attempting to change permissions on a file needs to have permission to do so, so in most situations this command will be run as the root user.

In the following command snippet, you see a long listing of a file owned by root, which subsequently has its owner changed using the chown command:

bash-3.2$ ls -l mysecret.txt
-rw-r--r-- 1 root staff 0 Apr 12 15:39 mysecret.txt
bash-3.2$ sudo chown dave mysecret.txt
bash-3.2$ ls -l mysecret.txt
-rw-r--r-- 1 dave staff 0 Apr 12 15:39 mysecret.txt

Here’s a single file from our ls -l file listing earlier. I’ve tweaked the permissions to make this example more illustrative:

rwxr-xr-x 1 root root 386 Aug 2 13:14 os-release

Specifically, look at the permission bits:

rwxr-xr-x

They are displayed in three groups of three here. Imagine them separated into three groups, to make it easier:

rwx r-x r-x

Each of these triplets represents read (r), write (w), and execute (x) permissions for a specific set of users, based on the user and group ownership information for that file. If you see a - character in place of a letter, that action (for the set of users it applies to) is not permitted. Let’s look at them in more detail:

  1. The first three bits represent the permissions of the file owner. In this case, the file’s owner (root) can read, write, and execute the file – rwx.
  2. The second three bits represent permissions for the group owner of the file, which in this case is also root. The permissions here are r-x, or read and execute (no writing!). But because root is also the user-owner of the file, those (more permissive) permissions take precedence; root can write to this file. The reason you often see permissions like this is because a group owner is required to be set on files, and if you don’t want to share a file with other groups, you can just use the owner’s group here. The group permissions on a file are usually less permissive than the owner permissions.
  3. The last three bits represent the permissions that all other system users have for this file (“the world”). This is almost always the most restrictive set of permissions, because most files don’t need to be shared with anyone outside of the owner (and sometimes a separate group owner). In this case, we’re looking at a shared library file that needs to be accessible by all system users, so the permissions are r-x (read and execute, no writing allowed).

“Read,” “write,” and “execute” are terms that make permissions easy to understand, but there is another important way that permissions are represented in Linux and Unix: octal.

As a developer, you’re probably familiar with non-decimal number systems – octal is simply a base-8 system (instead of the base-10 system – decimal – that we usually use as humans, and in contrast to the base-2 system – binary – that computers use).

Because there are only eight possible states for each three-bit permission combination, octal is a perfectly efficient system for representing them.

Think of our 9 bits, again divided into 3-bit chunks. Each 3-bit permissions-chunk can represent an octal number, which requires three bits to represent. These 9 bits give us exactly the amount of room that we need to represent three octal numbers – one for user permissions, one for group permissions, and one for other/world permissions.

OctalBinaryMeaning
00No permissions (—)
11Execute permission (–x)
210Write permission (-w-)
311Write and execute permissions (-wx)
4100Read permission (r–)
5101Read and execute permissions (r-x)
6110Read and write permissions (rw-)
7111Read, write, and execute permissions (rwx)

Table 8.1: Octal permissions

You’ll notice that this is laid out so that octal addition works – adding “read” (4) and “execute” (1) together gives you “read and execute” (5).

This may seem strange and arbitrary (and it is!), but you’ll get the hang of it quickly. You’ll mostly be using 7 (all), 6 (read/write), 5 (read/execute), 4 (read), and 0 (no permissions) while working.

The most common permissions you’ll see are:

  • -rw-r--r-- (644): Owner can read and write; all others can only read.
  • -rwxr-xr-x (755): Owner can do everything; all others can read/execute the file. This is a common one for executable files like scripts and binaries, and the default permission for directories.
  • -rw------ (600): Only the owner can read and write, no one else can do anything with it. This is common for secret keys, files that contain passwords, and other sensitive information. SSH, for example, won’t use keys that are group- or world-readable until you change their permissions to make them secret.

Changing ownership (chown) and permissions (chmod)

Section titled “Changing ownership (chown) and permissions (chmod)”

You’ll use two commands to change ownership and permissions on files: chown and chmod.

chown (change owner) is used to change the owner and group of a file. It’s used in the following way:

chown [OPTION]... [OWNER][:[GROUP]] FILE...

For example, imagine we have this file:

$ ls -lh testfile
-rw-r--r-- 1 dave dave 10 Aug 14 16:18 testfile

Let’s change the owner to chris (presuming there’s a chris user on the system):

$ chown chris testfile
$ ls -lh testfile
-rw-r--r-- 1 chris dave 10 Aug 14 16:18 testfile

We’ve changed the owner, but if we wanted to change the group, too, we could have run:

$ chown chris:staff testfile
$ ls -lh testfile
-rw-r--r-- 1 chris staff 10 Aug 14 16:18 testfile

One common task is changing the owner and group for all files in a given directory. You can do this with the -R or --recursive option:

$ chown -R dave:staff /home/dave

This will recursively set ownership for /home/dave/ and every file and directory inside it.

chmod (change mode) is used to change the permissions of a file. You can use regular or octal permissions here:

chmod [OPTION]... MODE[,MODE]... FILE...

chmod [OPTION]... OCTAL-MODE FILE...

Your options can be given in the form: ugo{+,-}rwx

  1. ugo (user, group, other – assumed to be all three if you don’t specify).
  2. + to add permissions, - to remove them.
  3. rwx (read, write, execute – any or all of these letters).

For example, to add execute permissions for the user who owns the file:

$ chmod u+x testfile
$ /tmp ls -lh testfile
-rwxr--r-- 1 dave dave 10 Aug 14 16:18 testfile

To add write and execute permissions for the group and all other users:

$ chmod go+wx testfile
$ /tmp ls -lh testfile
-rwxrwxrwx 1 dave dave 10 Aug 14 16:18 testfile

Oops, we actually wanted to remove all permissions for others:

$ chmod o-rwx testfile
$ /tmp ls -lh testfile
-rwxrwx--- 1 dave dave 10 Aug 14 16:18 testfile

Octal format for permissions can be set just like you’d expect:

$ chmod 744 testfile
$ /tmp ls -lh testfile
-rwxr--r-- 1 dave dave 10 Aug 14 16:18 testfile

Actually, let’s make that file read-only:

$ chmod 400 testfile
$ /tmp ls -lh testfile
-r-------- 1 dave dave 10 Aug 14 16:18 testfile

Both chown and chmod allow us to use a --reference argument, with which we can pass a file from which ownership or permissions will be copied.

In this chapter, we covered everything you need to know to solve the most common Linux permissions problems: you learned how to view permissions on files and how to modify them. More importantly, we showed you how to reason about permissions and how they tie in with Linux users and groups, which is where many people stumble.

Make sure you have a strong grasp of the material in this chapter; a huge percentage of the troubleshooting you’ll have to do during your career will revolve around file ownership and permission problems. Thankfully, most of these problems arise from a simple lack of understanding, which you no longer have. Go forth and troubleshoot!