Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Reading ls -l

ls -l /var/www/site
-rw-r----- 1 deploy www-data 1532 Nov 30 10:12 config.php
drwxr-xr-x 2 deploy www-data 4096 Nov 30 10:12 public

The first ten characters decode like this, taking -rw-r----- as the example:

  • Character 1, type: - a regular file, d a directory, l a symbolic link.
  • Characters 2 to 4, owner: rw-, so deploy may read and write.
  • Characters 5 to 7, group: r--, so members of www-data may read.
  • Characters 8 to 10, others: ---, so everyone else gets nothing.

The two names after the link count are the owner and the group.

Read, Write, and Execute

BitOn a fileOn a directory
rRead the contentList the names
wChange the contentCreate, delete, rename
xRun as a programEnter and reach files

On a directory, w works only together with x. Deleting a file depends on the directory's permissions, not the file's.

Numeric Notation

Each set is a sum: read is 4, write is 2, execute is 1. Three digits give owner, group, and others.

ModeStringTypical use
644rw-r--r--Ordinary files
755rwxr-xr-xDirectories, scripts
640rw-r-----Config for one group
600rw-------Private keys
700rwx------Private directories

Changing Owner and Permissions

sudo chown deploy:www-data config.php
chmod 640 config.php
chmod g+w uploads
chmod o-r notes.txt

The owner and group are changed with chown, explained in the chown command guide. Modes are changed with chmod, either as numbers or as letters such as g+w; see the chmod command. Never use 777 to make an error go away; chmod 777 explains why.

Defaults and the umask

Most programs create new files with 666 and new directories with 777, and the kernel removes the bits set in the umask. With the common umask of 022, files get 644 and directories 755. How to read and change it is in umask in Linux.

Special Bits and ACLs

  • Special bits: setuid, setgid, and the sticky bit add a fourth digit in front, as in 1777 on /tmp. See sticky bit, SUID, and SGID.
  • ACLs: when owner, group, and others are not enough, access control lists give extra users or groups their own rights. A + after the permission string in ls -l shows an ACL. See setfacl and getfacl.

Root Is Not Held Back

The root user can read and write any file whatever its mode. The only limit is execute: root can run a file only if at least one execute bit is set. Permissions protect users from each other, not from root, which is why services should not run as root.

Safe Values on a Server

  • Web files: 644 for files and 755 for directories, owned by the deploy user, with the web server's group able to read.
  • Upload folders: writable by the web server's group only where the application must write, such as 775 on that folder alone.
  • Config files with secrets: 640 with the application's group, or 600 when only the owner reads them.
  • SSH: 700 on ~/.ssh, 600 on private keys and authorized_keys. The ssh client ignores a private key that others can access, and with its default StrictModes setting the SSH server refuses key logins when the home directory, ~/.ssh, or authorized_keys is writable by others.
Was this answer helpful?
Loading...