File Attributes
Anatomy
# Example file attributes broken down
-rw-rw-r-- 2 mat mat 12 May 26 19:50 original.txt
# ┌----- type ----┐ ┌---------- mode ----------------┐
[ 4 bits: file type ][ 3 bits: special ][ 9 bits: permissions ]
1000 000 110110100
codes = regular file n/a for this rw-rw-r--Type
4 bits: File Type
| binary | meaning | ls char |
|---|---|---|
| 1000 | regular file | - |
| 0100 | directory | d |
| 1010 | symlink | l |
| 0110 | block device | b |
| 0010 | char device | c |
| 0001 | named pipe | p |
| 1100 | socket | s |
Mode
9 bits: Permissions
rthe read bit allows the file to be opened and readwwrite bit controls modifying or truncating a file’s contents. Deleting or renaming it requires write permission on the parent directory, since that’s where the filename-to-inode mapping is stored — each directory manages only its own entries.eexecutable bit allows files to be executed, there two types binaries that run on the cpu directly and scripts which need to be interpreted by a shell or other program
chmod change mode (permissions)
| Octal | Binary | Perms |
|---|---|---|
| 0 | 000 | — |
| 1 | 001 | –x |
| 2 | 010 | -w- |
| 3 | 011 | -wx |
| 4 | 100 | r– |
| 5 | 101 | r-x |
| 6 | 110 | rw- |
| 7 | 111 | rwx |
# Options for chmod targeting
u = user (owner)
g = group
o = other (everyone)
a = all 3
# The main ways to use chmod
chmod u+w # write access for owner
chmod ug=rw,o=r # r/w to user and group, read for others
chmod a-x # remove execute for all
chmod g=u # group gets the permissions user has
# recursively give group write to all its contents, preserving bits that were not explicitly set
chmod -R g+w mydir
# -> warning execute on a directory has different effectsA note about x permissions
xexecute on a directory means you can traverse (cd) inchmod 711 myproggives all perms to the owner and execxfor everyone else. ifmyprogwas a shell script it would need read and exec permissions so it can be read by the interpreter. Binary files only need exec permissions.
chown - change owner
To change a file’s group, you must either be the superuser or be the owner of the file and belong to the group you’re changing.
chown # change a files user and group
# sudo chown user:group file
sudo chown -R mat backup/ # just the user
sudo chown -R matt:staff backup/ # user and group
# This command works too although it can be done with chown
# It's still useful in a scripting setting
sudo chgrp group fileumask
umask is a shell command to influence the default permissions given to the files you create.
You define as three digit octal value that represents the permissions to take away
umask 027 # allow all permissoins for owner but forbid write perms to the group and no perms for anyone else
# default is typically 022 = 7553 bits: Special
| binary | meaning | ls char |
|---|---|---|
| 100 | setuid | s or S |
| 010 | setgid | s or S |
| 001 | sticky | t or T |
setuid / setguid
Pithy
setuid/setguidallows an executable to have some code that allows the calling user to access files or processes that would otherwise be off-limits to the caller.
This is different from running as sudo, these files while at installation time may have required special permissions, their purpose is to avoid requiring the user provide special permissions once setup and instead allow elevated permissions at runtime.
setuid = set user id (run as user ID)
setguid = set group id (run as group ID)Example of setuid file:
# owner's execute bit is S meaning "everyone" who executes will get the owners system permissions
# s = setuid and executable
# S = setuid set, but no execute permission
-rwSrw-r-x 1 mat mat 0 May 27 11:18 testfile
# Remember, ls is just formatting it this way the "x" still exists theres always 16 bitsReal setuid examples
passwd
# passwd binary needs to edit /etc/passwd which requires root user access
# Thus somewhere in this program is upgrades to those rights and then downgrades ASAP for security
/usr/bin/passwd
-rwsr-xr-x 1 root root /usr/bin/passwd
# Search for other setuid and setguid binaries on your system
find / -perm /6000 -type f 2>/dev/nullsudo
Another prolific example sudo, which checks a users group and then escalates their permissions to the owner of the sudo binary
# To demonstrate, if you chown this binary and didn't have the root password
# you'd have to boot to single user mode to recover root access, since you
# wouldn't be able to elavate your permissons beyond the owner of the binary
-rwsr-xr-x 1 root root /usr/bin/sudosticky
sticky bit is only useful on directories (the name is historical).
It makes it so you can only delete or rename a file if you are the own of the dir/file or are the root user. This is used by the /tmp directory to make it more private and secure
bonus flags
Linux has bonus flags to use on files for edge cases
if a file is behaving strangely check it with
lsattrto see it has one or more flags enabled
lsattr # list bonus flags
chattr # change bonus flagsExample using the bonus i flag = immutable
chattr +i testfile
lsattr testfile
----i---------e------- testfile # flag is now set
rm testfile
rm: cannot remove 'testfile'
sudo rm testfile
rm: cannot remove 'testfile' # even sudo can't remove it
sudo chattr -i testfile # removing the flag
lsattr testfile
--------------e------- testfile
rm testfile Some of the optional flags
| Flag | FSa | Meaning |
|---|---|---|
| A | XBE | Never update access time (st_atime; for performance) |
| a | XBE | Allow writing only in append modeb |
| C | B | Disable copy-on-write updates |
| c | B | Compress contents |
| d | XBE | Do not back up; backup utilities should ignore this file |
| i | XBE | Make file immutable and undeletableb |
| X | B | Avoid data compression if it is the default |
a X = XFS, B = Btrfs, E = ext3 and ext4
b Can be set only by root
ls list
ls -a # a = all
ls -l # l = long list
ls -ld # d = directory -> list attributes for the directory itself
ls -t # t = time -> sort by modifcation time
ls -tr # tr = time reversed -> sort by modifcation time REVERSED
ls -F # -> puts a slash after directory names (easier determine file vs dir)
ls -R # R = recursive
ls -h # h = human readable -> converts file sizesBreakdown of ls output
-rwSrw-r-x 2 mat admins 49 May 27 11:18 testfile
- # type
rwSrw-rwx # mode
2 # link count (hard links) <- directories always have 2 parent dir and the special file .
mat # owner ---┐ if text versions can't be determined
admins # group owner ---┙ ls shows the fields as their uid numbers
# this can happen if the user or group is deleted from /etc/passwd or /etc/group
49 # size in bytes
May 27 11:18 # date of last modification
testfile # name of fileGit
Git facts
- Git does not track modes, owners, or modification times.
- Git tracks the
xexecutable bit on a file