File attributes

File Attributes

Example file attribute anotomy

# 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--
binarymeaningls char
1000regular file-
0100directoryd
1010symlinkl
0110block deviceb
0010char devicec
0001named pipep
1100sockets

9 bits: Permissions

  • r the read bit allows the file to be opened and read
  • w write 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.
  • e executable 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)
OctalBinaryPerms
0000
1001–x
2010-w-
3011-wx
4100r–
5101r-x
6110rw-
7111rwx
# 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 effects
A note about x permissions
  • x execute on a directory means you can traverse (cd) in
  • chmod 711 myprog gives all perms to the owner and exec x for everyone else. if myprog was 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
umask

umask is a shell command to influence the default permissions given to the files you create.
You define as three digit ocavlue 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 = 755

3 bits - special

binarymeaningls char
100setuids or S
010setgids or S
001stickyt or T
setuid / setgid
setuid = set user id (run as user ID)
setguid = set group id (run as group ID)
# Basically allows an executable to have some code that allows them access files or processes that would otherwise be offlimits to the user that runs them
# 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 bits
sticky

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

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 sizes

Breakdown 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 file

bonus flags

linux has bonus flags to use on files for edge cases

if a file is behaving stangely check it with lsattr to see it has one or more flags enabled

lsattr   # list bonus flags
chattr # change bonus flags

Example 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

FlagFSaMeaning
AXBENever update access time (st_atime; for performance)
aXBEAllow writing only in append modeb
CBDisable copy-on-write updates
cBCompress contents
dXBEDo not back up; backup utilities should ignore this file
iXBEMake file immutable and undeletableb
XBAvoid data compression if it is the default

a X = XFS, B = Btrfs, E = ext3 and ext4
b Can be set only by root