Listing Files & System Identity
ls Command Variations
-
lsShows visible files and directories. -
ls -aAlso shows hidden files (starting with `.`). -
ls -lDetailed view: permissions, owner, group, size, modification time. -
ls -laCombines detailed view with hidden files. -
ls -ldLists directory entries of the specified directory, not its contents.
System Account Files
-
/etc/passwdContains user account details. -
/etc/groupContains group information. -
/etc/shadowStores password-related data and is protected.
Deconstructing the ls -l Output
A long listing gives exactly 7 columns of information for each file or directory. Here is a typical example:
Permissions Basics
Linux permissions are expressed with r, w, and x, and they apply separately to
the owner, group, and others. Numeric permissions use
octal values.
- - : Regular file (d for directory)
- rwx : Owner can read, write, and execute (7)
- r-x : Group can read and execute (5)
- r-- : Others can only read (4)
- . : Access method (period for SELinux, + for alternate access methods like ACLs)
Effective Permissions (How Linux Decides)
When you try to access a file, Linux calculates your effective permissions in a strict top-down order:
- Are you the Owner? If yes, it applies the User permissions and stops checking.
- Are you in the Group? If not the owner, but in the group, it applies the Group permissions and stops checking.
- Everyone Else: If you are neither, it applies the Others permissions.
| Numeric | Symbolic | Meaning / Typical Use Case |
|---|---|---|
| 644 | -rw-r--r-- | Owner can read/write; group/others can read only. Used for normal files. |
| 750 | -rwxr-x--- | Owner has full access; group can read/execute; others have no access. |
| 700 | -rwx------ | Only the owner has full access. Used for private scripts or keys. |
Modifying Permissions & Ownership
Changing Permissions:
chmod
The chmod
command changes file or directory permissions.
1. Character Mode
Use characters to Add (+), Subtract (-), or explicitly Set (=) permissions.
2. Numeric Mode
Add the values (Read=4, Write=2, Execute=1) for each user level.
Options
-R: Modify recursively
-v: Verbose (diagnostic entry for every file)
-c: Report changes
-f: Hide most error messages
Note: Only the owner (or root) can change permissions.
Changing Ownership:
chown & chgrp
Every file has both an owner user and an owner group. Changing them usually
requires sudo.
-
chown user fileChanges the owner of a file. -
chown user:group fileChanges both owner and group simultaneously. -
chown user: fileChanges owner and automatically sets group to user's login group. -
chown :group fileShortcut to change only the group. -
chgrp group fileDedicated command to change only the group.
Note: Add the -R flag to recursively change ownership through a directory structure.
Default Permissions & umask
When you create a new file or directory, Linux automatically assigns default permissions based on a starting "base" permission minus a subtractive mask known as the umask.
Base Permissions
- Files: Start at
0666(Read/Write for everyone) - Directories: Start at
0777(Read/Write/Execute for everyone)
Typical User Defaults
- Root User: umask
0022→ Files644, Dirs755 - Regular User: umask
0002→ Files664, Dirs775
Configuring Umask
Running umask by itself displays your current mask. Running umask 0027 changes it for the current session. To make the change permanent, it must be added to a configuration file:
- System-wide: Set in
/etc/profile,/etc/bash.bashrc, or/etc/pam.d/common-session - User-specific: Add
umask 0027to the bottom of your~/.bashrc(which gets its initial copy from/etc/skel).
Special Permissions (SUID, SGID, Sticky Bit)
Special permissions are used in place of the execute permission (x). They allow users or groups to execute files with privileges they wouldn't normally have, or provide special restrictions on directories.
"Thinking of it as a 'role' is the exact right mindset for a DevOps engineer. You are not just getting the ability to run the file; you are temporarily putting on the 'ID badge' of the file's owner (SUID) or the file's group (SGID) for the exact duration of that program. Once the program finishes, you go right back to being your normal user."
SUID (Set Owner User ID)
Applied to the Owner. When an executable file has SUID set, a user executing it temporarily inherits the permissions of the file's owner.
- Command:
chmod u+s file - Appearance: Replaces the user's
xwith ans(e.g.,-rwsr-xr-x). - Real-world Use: The
passwdcommand is owned by root and has SUID set. This allows normal users to temporarily gain root privileges just long enough to change their password in the protected/etc/shadowfile. - Note: Linux completely ignores SUID on directories.
SGID (Set Group ID)
Applied to the Group. Can be used on files (similar to SUID but for group permissions) but is considered a DevOps Superpower when applied to directories.
- Command:
chmod g+s directory - Appearance: Replaces the group's
xwith ans(e.g.,drwxrws---). - Real-world Use: Setting SGID on a shared team folder ensures that any new files created inside it automatically inherit the directory's group ownership (e.g.,
devteam) rather than the creator's primary personal group. This guarantees the whole team can collaborate on new files without permission issues.
Sticky Bit
Applied to Others. Used almost exclusively on directories that give write access to everyone, to prevent users from deleting each other's work.
- Command:
chmod o+t directory - Appearance: Replaces others'
xwith at(e.g.,drwxrwxrwt). - Real-world Use: The
/tmpdirectory has the sticky bit set. Anyone can write files to it, but you are only permitted to delete or rename files that you personally own.
Broken Permissions: Lowercase vs. Uppercase
If you ever see a capital S (or T for sticky bit) instead of a lowercase one, it is a visual warning from Linux that the configuration is broken. It means the special permission was applied, but the underlying execute (x) permission is missing. Special execution rules cannot work if the file isn't allowed to be executed in the first place!
File Attributes: lsattr & chattr
While permissions control who can access a file, attributes control how the operating system manages the file. Attributes can override standard permissions. For example, an attribute can make a file completely undeletable, even by users who otherwise have full write permissions.
lsattr
Lists the attributes of files or directories.
-R: Recursively list attributes-a: List all files (including hidden)-d: List directories like files
chattr
Changes the attributes of files or directories.
+: Add attributes-: Remove attributes=: Set exactly these attributes
The Immutable Flag (i)
This is the most critical attribute for a DevOps engineer to know. When the immutable flag is set on a file, it cannot be modified, deleted, renamed, or linked to. This applies even if the file's standard permissions give you write access.
It is heavily used to protect critical configuration files from accidental or malicious modification. Modifying this attribute requires root privileges (using sudo).
Other Notable Attributes
| Attribute | Name | Description |
|---|---|---|
| e | Block extents | File is stored using block extents (contiguous data blocks). Often enabled by default on modern filesystems. |
| a | Append only | File can only be opened for writing in append mode. Existing data cannot be overwritten. Excellent for log files. |
| s | Secure deletion | If deleted, its data is overwritten with zeroes (secure wipe). |
| u | Undeletable | When deleted, contents are saved to allow for later undeletion using recovery tools. |
File Systems & Standard Directories
File Systems & Mount Points
Similar to how a dresser uses drawers to organize clothing, a hard drive uses directories to organize files. Drives are divided into partitions, and each partition contains a file system (a method for storing files).
-
lsblkLists all disks and partitions. -
lsblk -fShows the different file systems and mount points in the drives. -
/The root directory. Every directory is a subdirectory of the root. A mount point is a directory through which we access the space on a partition.
System Directories
Standard directories in the root belong to the OS and are protected.
-
/binEssential command line utilities and binaries. -
/devHardware and software device drivers. -
/etcBasic configuration files. -
/libShared program libraries required by the kernel and binaries. -
/mediaAccess to temporary and removable file systems (CD-ROMs, etc). -
/sbinBinaries for booting and root user administration. -
/varSystem logs, printer spools, network service configs.
User Directories
Each user has an assigned folder in /home named with their user account.
-
/home/userArea where the user can create subdirectories and work with files. Other users cannot access it by default. -
Desktop/Directory representing the desktop environment for the user. -
Documents/Default save location for most applications. -
Downloads/Default directory for files downloaded from the Internet.