guest@linuxthings: ~/guides/properties.md

guest@linuxthings:~/guides$ man properties

File Properties

Reading Permissions

Every file has an owner, a group and permissions for three classes of user: the owner (u), the group (g) and everyone else (o).

ls -l script.sh
-rwxr-x--- 1 smith dev 812 Mar  3 10:15 script.sh

Position

Meaning

1

File type: - file, d directory, l symbolic link

2-4

Owner permissions (rwx)

5-7

Group permissions (r-x)

8-10

Permissions for everyone else (---)

On a directory, r lets you list its contents, w lets you create and delete files inside it, and x lets you cd into it.

Permissions (chmod)

Mode

Octal

Meaning

rwx

7

Read, Write, Execute

rw-

6

Read, Write

r-x

5

Read, Execute

r--

4

Read only

-wx

3

Write, Execute

-w-

2

Write only

--x

1

Execute only

---

0

No access

Each digit is the sum of r=4, w=2 and x=1, and the three digits are for owner, group and others.

chmod 755 script    # rwx for owner, rx for others
chmod 644 file      # Owner can write, everyone can read
chmod 600 secret    # Private file, owner only
chmod 700 mydir     # Private directory, owner only
chmod u+x script    # Add execute for owner
chmod go-w file     # Remove write for group and other
chmod a+r file      # Everyone can read
chmod o=r file      # Set others to exactly read-only
chmod ug+rw,o-rwx f # Several changes at once
chmod -R u+rwX dir  # Recursive; X adds execute only to directories
chmod --reference=a b  # Copy permissions from file a to file b

Special Permissions

Bit

Octal

Symbolic

Effect

setuid

4000

u+s

A program runs with its owner's privileges

setgid

2000

g+s

On a directory, new files inherit the directory's group

sticky

1000

+t

In a shared directory, only a file's owner can delete it

chmod 2770 /srv/shared       # Group-shared directory (setgid)
chmod 1777 /scratch          # World-writable, but sticky like /tmp
ls -ld /tmp                  # drwxrwxrwt: the t is the sticky bit
sudo find / -type f -perm /6000 -ls 2>/dev/null  # List setuid/setgid programs
A setuid program owned by root runs as root. Treat any unexpected setuid file as a security problem.

Ownership (chown/chgrp)

sudo chown user file       # Change owner
sudo chown user:group file # Change owner and group
sudo chown :group file     # Change group only
sudo chown -R user dir/    # Change a whole directory tree
chgrp group file           # Change group only

Default Permissions (umask)

umask        # Show the current mask, e.g. 0022
umask -S     # Show it symbolically: u=rwx,g=rx,o=rx
umask 022    # Typical default. New files get 644, dirs 755.
umask 002    # Group-friendly: files 664, dirs 775
umask 077    # Private: files 600, dirs 700

The mask removes permissions from the defaults of 666 for files and 777 for directories. Put your preferred umask in ~/.bashrc to make it stick.

Access Control Lists (ACLs)

ACLs grant access to specific extra users or groups without changing a file's owner or group.

getfacl file                    # Show the ACL
setfacl -m u:maggie:r file      # Give user maggie read access
setfacl -m g:devs:rw file       # Give group devs read/write
setfacl -x u:maggie file        # Remove maggie's entry
setfacl -b file                 # Remove all ACL entries
setfacl -d -m g:devs:rw dir     # Default ACL inherited by new files in dir

A + at the end of the permissions in ls -l (for example -rw-r-----+) means the file has an ACL.

Inspecting Files (stat, file)

stat file            # Size, inode, permissions, owner and all timestamps
stat -c '%a %U %n' * # Custom format: octal mode, owner, name
file file            # Guess the file type from its contents
touch -d "2 days ago" file   # Set a file's timestamps

Attributes (chattr/lsattr)

sudo chattr +i file  # Make file immutable (cannot be changed/deleted)
sudo chattr -i file  # Remove the immutable flag
sudo chattr +a log   # Append-only: data can be added but not rewritten
lsattr file          # List attributes

Immutable files cannot be deleted even by root until the flag is removed. Attribute support depends on the filesystem (ext4 supports these).

guest@linuxthings:~/guides$