Linux Ownership Model
Linux is a multi-user system. Every file has three levels of ownership:
| Level | Description |
|---|---|
| User (u) | The file's owner (usually who created it) |
| Group (g) | Users in the same group as the owner |
| Others (o) | Everyone else on the system |
Understanding Permissions
List files with permissions using ls -l:
ls -lOutput example:
-rw-rw-r-- 1 ubuntu ubuntu 7 Oct 16 14:23 config.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 16 14:20 scripts/
Breaking down -rw-rw-r--:
| Position | Meaning |
|---|---|
- | File type (- = file, d = directory, l = link) |
rw- | Owner permissions (read, write, no execute) |
rw- | Group permissions (read, write, no execute) |
r-- | Others permissions (read only) |
Permission Types
| Letter | Permission | On Files | On Directories |
|---|---|---|---|
r | Read | View contents | List contents |
w | Write | Modify contents | Add/delete files |
x | Execute | Run as program | Enter directory |
- | None | Denied | Denied |
Changing Permissions
Numeric Method (chmod)
Each permission has a numeric value:
| Value | Permission |
|---|---|
| 4 | Read (r) |
| 2 | Write (w) |
| 1 | Execute (x) |
| 0 | None (-) |
Combine values for each level (owner, group, others):
# rwxr-xr-- = 7 5 4
chmod 754 script.sh
# rw-rw-r-- = 6 6 4
chmod 664 config.txt
# rwx------ = 7 0 0
chmod 700 private_key.pem
# rwxrwxrwx = 7 7 7 (dangerous!)
chmod 777 file.txtSymbolic Method
# Add execute for owner
chmod u+x script.sh
# Remove write for others
chmod o-w config.txt
# Add read for everyone
chmod a+r readme.txt
# Set exact permissions for group
chmod g=rx script.shCommon Permission Patterns
| Numeric | Symbolic | Use Case |
|---|---|---|
755 | rwxr-xr-x | Scripts, directories |
644 | rw-r--r-- | Config files, documents |
600 | rw------- | SSH keys, secrets |
700 | rwx------ | Private directories |
The Superuser (root)
The root user has unrestricted access to everything. Use sudo to run commands with root privileges:
# Run a single command as root
sudo apt update
# Switch to root shell
sudo su
# Notice prompt changes from $ to #
exit # Return to normal user
# Check who you are
whoamiOnly use sudo when necessary. Running everything as root is a security risk — one mistake can destroy the system.
Environment Variables
Environment variables store configuration that affects how programs behave.
Viewing Variables
# Print a specific variable
echo $HOME
echo $USER
echo $PATH
# List all environment variables
printenv
# Or use env
envImportant System Variables
| Variable | Purpose |
|---|---|
PATH | Directories searched for commands |
HOME | Current user's home directory |
USER | Current username |
PWD | Current working directory |
SHELL | Default shell program |
LANG | System language setting |
Creating Variables
# Create a variable (current session only)
export PROJECT_NAME="nextgen-playground"
echo $PROJECT_NAME
# Use in commands
echo "Working on $PROJECT_NAME"Deleting Variables
unset PROJECT_NAME
echo $PROJECT_NAME # EmptyThe .bashrc File
The .bashrc file runs every time you open a terminal. Use it for persistent settings:
# Edit your .bashrc
nano ~/.bashrcAdd at the end of the file:
# Custom environment variables
export EDITOR="nano"
export DEVOPS_ENV="learning"
# Custom aliases (shortcuts)
alias ll='ls -la'
alias gs='git status'
alias update='sudo apt update && sudo apt upgrade -y'
alias ports='netstat -tuln'
# Custom prompt (optional)
export PS1='\u@\h:\w\$ 'Apply changes without restarting:
source ~/.bashrcChanges to .bashrc only take effect in new terminal sessions or after running source ~/.bashrc. Be careful not to modify the original content — add your customizations at the end.
Aliases
Aliases create shortcuts for long commands:
# Create a temporary alias (current session)
alias cls='clear'
# View all aliases
alias
# Remove an alias
unalias clsFor persistent aliases, add them to ~/.bashrc.
Practical Exercise
# 1. Create a project directory
mkdir -p ~/devops-lab
chmod 755 ~/devops-lab
# 2. Create a script
echo '#!/bin/bash' > ~/devops-lab/hello.sh
echo 'echo "Hello from $USER on $(hostname)"' >> ~/devops-lab/hello.sh
# 3. Make it executable
chmod +x ~/devops-lab/hello.sh
# 4. Run it
~/devops-lab/hello.sh
# 5. Check permissions
ls -la ~/devops-lab/Summary
- Linux uses user/group/others ownership with read/write/execute permissions
chmodchanges permissions (numeric:755, symbolic:u+x)sudoruns commands with root privileges- Environment variables configure system behavior (
export VAR=value) .bashrcstores persistent settings, aliases, and variables- Use
source ~/.bashrcto reload without restarting
Next Steps
With file management and permissions mastered, you're ready to write Bash scripts — automating repetitive tasks is a core DevOps skill.