Home / Users & permissions

Users and permissions

Who can read, write and run each file.

Laptop showing permissions settings with Agenda VR3 nearby.

Reading permissions

ls -l shows strings such as -rwxr-x---. The three groups of letters are the permissions for the owner, the group and everyone else: read, write and execute.

Changing them

chmod u+x script.sh        # let the owner run the script
chmod 640 notes.txt        # owner read/write, group read, others nothing
sudo chown alice:staff report.txt

sudo and least privilege

Use a normal account day to day and sudo only for administrative tasks. Give users and services only the access they need.

Directories have their own rules

On a directory, the permission bits mean something slightly different from a regular file.

  • r lets you list the names inside it.
  • x lets you enter it and access items by name.
  • w lets you change the directory itself: create files, rename them, or remove them. In practice, w usually needs x as well.

That leads to a few behaviours that surprise new users. A directory with r but no x can sometimes be listed, but you cannot cd into it or open files inside it. A directory with x but no r can still be used if you already know a filename, but you cannot ask the system for a full listing.

The biggest surprise is deletion. Whether a file can be deleted is mainly controlled by the parent directory, not the file's own write bit. A read-only file inside a writable directory can still be removed.

Useful patterns: - 700 for a private home subdirectory such as .ssh - 755 for directories that everyone may read and traverse - 750 when a group should have access but others should not

If a permission problem seems odd, check every directory in the path, not just the final file. The File system page helps if you want a refresher on how paths and directories fit together.

Default permissions and umask

You do not usually set permissions from scratch each time. New files and directories start with defaults, and the shell or service process applies a umask to remove bits from those defaults.

A useful mental model is: - files start from 666 (rw-rw-rw-) - directories start from 777 (rwxrwxrwx) - the umask removes permissions

So with umask 022, a new file normally becomes 644 and a new directory 755. With umask 077, they become 600 and 700, which is better for private work.

You can see the current value with umask and, on many systems, a symbolic form with umask -S.

This also explains why a script created with touch backup.sh is not executable straight away. Regular files usually do not get the execute bit by default; add it only when the file really is a program: chmod u+x backup.sh.

If you are testing from the Command line, try creating a file and a directory before and after changing umask in your shell session. It is a quick way to see the rule in action.

One extra practical point: services and cron jobs may run with a different umask from your login shell. If an application keeps creating files that are too open or too restrictive, check the process that created them as part of Troubleshooting.

Shared directories and special permission bits

The basic owner/group/others model is enough for most cases, but two special bits are worth knowing if several users share a system.

The first is setgid on a directory. When this bit is set, new files created inside that directory inherit its group instead of the creator's primary group. That keeps a shared project tree consistent.

Example: - make a group-owned project directory - set the group and keep it on new files with chgrp editors project and chmod 2775 project

Without setgid, one user may create files owned by an unexpected group, and the next person cannot edit them even though the directory itself looked correct.

The second is the sticky bit. On a writable shared directory, it stops users deleting or renaming each other's files. That is why temporary directories are often 1777 rather than plain 777.

Example: chmod 1777 shared-tmp

Be careful with the other special bit, setuid, on executables. It makes a program run with the file owner's effective privileges, which can be necessary for a few system tools but is easy to get wrong. Do not add it casually to scripts or utilities just to avoid a permission error; that belongs in a wider look at Security.

When group permissions still feel too coarse, many Linux systems also support ACLs, which let you grant an extra named user or group access without changing the main owner/group mode bits.