Posted on
Getting Started

Getting Started with Linux Bash Filesystem, Permissions, Commands and Users/Groups

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering Linux: Filesystems, Users, Permissions, and More

Your comprehensive guide to Linux fundamentals—from navigating the filesystem tree to managing users and securing your system.


1. Why Linux Uses a Single Filesystem Tree

Linux organizes files and directories into one unified tree under / (root). This design:

  • Simplifies Access: All devices (hard drives, USB sticks) and partitions are mounted at specific points (e.g., /mnt or /media), making them appear as subdirectories.

  • Ensures Consistency: Applications find files (e.g., libraries in /lib) predictably, regardless of physical storage.

  • Facilitates Sharing: Critical paths (like /home) are standardized across systems.

Key Considerations:

  • Separation of Concerns: System files (/bin, /etc) are isolated from user data (/home).

  • Mount Points: Temporary or removable storage is attached to empty directories (e.g., mounting a backup drive at /backup).

  • Scalability: Large directories (like /var/log) can reside on separate partitions to prevent filling up /.


2. Filesystem Hierarchy Standard (FHS)

The FHS defines directory structures across Linux distributions. It ensures:

  • Compatibility: Software installs correctly on any FHS-compliant system.

  • Predictability: Admins know where to find config files (/etc), binaries (/bin, /sbin), and logs (/var/log).


3. Root Directory (/) at Boot

At boot, the kernel needs minimal tools to start the system:
Essential at Boot:

  • /bin – Core user binaries (e.g., bash, ls).

  • /sbin – System administration binaries (e.g., init, mount).

  • /etc – Configuration files (e.g., fstab for mounts).

  • /lib – Shared libraries for /bin and /sbin.

  • /dev – Device files (e.g., sda, tty).

Post-Boot Availability:

  • /usr – User applications and docs (mountable later).

  • /opt – Optional third-party software.

  • /home – User directories.


4. Key Subdirectory Trees

Directory Purpose & Contents
/bin Essential user binaries (cp, mv, grep).
/sbin System admin binaries (fdisk, ifconfig).
/etc Configuration files (passwd, network/).
/dev Device files (disk0, random).
/proc Virtual filesystem for process/kernel info.
/var Variable data (logs, caches, mail).
/tmp Temporary files (auto-cleared on reboot).
/usr Read-only user apps and data (/usr/bin, /usr/lib).
/home User personal directories (~).
/boot Bootloader files (kernels, initramfs).

5. Environment Variables

View All:

printenv  # Or: env  

Set Temporarily:

MY_VAR="Hello"  # Only affects current shell  

Set Permanently:
Add to ~/.bashrc or /etc/profile:

export PATH="$PATH:/my/custom/path"  

Use Variables:

echo $PATH  
cd $HOME  

6. Command History

  • View History:

    history  
    
  • Execute Last Command:

    !!  
    
  • Edit & Run:

    !vim:p     # Print last `vim` command without running  
    ^old^new^  # Replace "old" with "new" in last command  
    

7. Aliases

Create Shortcuts:

alias ll='ls -alh'  

Make Permanent:
Add to ~/.bashrc:

alias update='sudo apt update && sudo apt upgrade'  

Remove Alias:

unalias ll  

8. User Accounts

Key Attributes (stored in /etc/passwd):

  • Username, UID, primary group ID (GID), home directory, login shell.

Create User:

sudo useradd -m -s /bin/bash alice  # -m creates /home/alice  

Modify User:

sudo usermod -aG developers alice  # Add to "developers" group  

Lock/Delete:

sudo passwd -l alice    # Lock account  
sudo userdel -r alice   # Delete account & home dir  

9. Password Management

  • Set Password:

    sudo passwd alice  
    
  • Storage: Hashed passwords in /etc/shadow (readable only by root).

  • Enforce Changes:

    sudo chage -M 90 -W 7 alice  # Expire every 90 days, warn 7 days prior  
    

10. The Root Account

  • Purpose: Superuser with unrestricted access.

  • When to Use: Only for system administration (installing packages, modifying system files).

  • Safer Alternative: Use sudo for temporary privilege escalation.


11. Secure Shell (SSH)

Log in Remotely:

ssh user@hostname  

Execute Remote Commands:

ssh user@hostname "ls /tmp"  

Key-Based Authentication:

ssh-keygen  # Create keys  
ssh-copy-id user@hostname  # Copy public key  

12. Group Membership

Why Groups Matter:

  • Share file access among team members.

  • Restrict resources (e.g., developers group can access /opt/code).

Commands:

sudo groupadd developers      # Create group  
sudo groupdel developers      # Delete group  
sudo usermod -aG developers alice  # Add user to group  

User Private Groups (UPG):

  • Each user gets a dedicated group with their name (GID=UID).

  • Files created by the user default to their UPG, enhancing privacy.


13. File Permissions: Owner, Group, World

Permissions are displayed as -rwxr-xr--:

  • Owner: User who owns the file.

  • Group: Group with access.

  • World: All other users.

Symbol Permission
r Read
w Write
x Execute

Change Permissions:

chmod u+x script.sh    # Add execute for owner  
chmod g-w file.txt     # Remove write for group  
chmod o-r document     # Remove read for others  

Change Ownership:

sudo chown alice:developers file.txt  # user:group  
sudo chgrp developers /opt/code       # Change group  

umask: Controls default permissions for new files.

umask 022  # Files: 644 (rw-r--r--), Directories: 755 (rwxr-xr-x)  

Access Control Lists (ACLs): Extend permissions beyond owner/group/world.

setfacl -m u:bob:rwx file.txt  # Grant Bob full access  
getfacl file.txt               # View ACLs  

Conclusion

Mastering Linux’s structure, user management, and permissions unlocks efficient, secure system administration. Practice these commands in a safe environment, and soon they’ll become second nature!

Pro Tip: Bookmark this guide and refer to man [command] for deep dives into any tool. Happy administering! 🐧