Users, Permissions & Environment Variables

25 minLesson 5 of 16

Learning Objectives

  • Understand Linux ownership model (user, group, others)
  • Read and modify file permissions with chmod
  • Create and manage environment variables
  • Configure persistent settings in .bashrc
  • Use sudo for elevated privileges

Linux Ownership Model

Linux is a multi-user system. Every file has three levels of ownership:

LevelDescription
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 -l

Output 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--:

PositionMeaning
-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

LetterPermissionOn FilesOn Directories
rReadView contentsList contents
wWriteModify contentsAdd/delete files
xExecuteRun as programEnter directory
-NoneDeniedDenied

Changing Permissions

Numeric Method (chmod)

Each permission has a numeric value:

ValuePermission
4Read (r)
2Write (w)
1Execute (x)
0None (-)

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.txt

Symbolic 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.sh

Common Permission Patterns

NumericSymbolicUse Case
755rwxr-xr-xScripts, directories
644rw-r--r--Config files, documents
600rw-------SSH keys, secrets
700rwx------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
whoami
⚠️

Only 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
env

Important System Variables

VariablePurpose
PATHDirectories searched for commands
HOMECurrent user's home directory
USERCurrent username
PWDCurrent working directory
SHELLDefault shell program
LANGSystem 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  # Empty

The .bashrc File

The .bashrc file runs every time you open a terminal. Use it for persistent settings:

# Edit your .bashrc
nano ~/.bashrc

Add 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 ~/.bashrc
ℹ️

Changes 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 cls

For 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
  • chmod changes permissions (numeric: 755, symbolic: u+x)
  • sudo runs commands with root privileges
  • Environment variables configure system behavior (export VAR=value)
  • .bashrc stores persistent settings, aliases, and variables
  • Use source ~/.bashrc to 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.