Why Linux?
Almost every server, container and cloud VM runs Linux. Docker containers are Linux processes. Kubernetes nodes are Linux machines. If you are comfortable in a Linux terminal, everything else in DevOps becomes easier.
Practice setup: Use Ubuntu in a VM, WSL2 on Windows, a cloud free-tier VM, or a Docker container: docker run -it ubuntu:24.04 bash.
The Filesystem Layout
| Path | Purpose |
/ | Root of everything |
/home | User home folders |
/etc | Configuration files (ssh, nginx, hosts) |
/var/log | Log files |
/var/lib | Application data (databases, Docker) |
/usr/bin, /bin | Programs |
/tmp | Temporary files |
/proc, /sys | Live kernel and process information |
Essential Commands
bash
pwd # where am I?
ls -la # list files with details, including hidden
cd /var/log # change directory
mkdir -p app/logs # create nested folders
cp file.txt backup.txt # copy
mv old.txt new.txt # move or rename
rm -r olddir # delete a folder (careful, no recycle bin)
cat file.txt # print a file
less /var/log/syslog # page through a big file (q to quit)
head -n 20 file.txt # first 20 lines
tail -n 50 -f app.log # last 50 lines and follow new lines live
touch newfile # create empty file
find /var -name "*.log" -mtime -1 # logs changed in the last day
du -sh * # size of each item here
df -h # disk space per filesystem
free -h # memory
uptime # load average
which python3 # where is a program?
Search Text: grep, pipes and redirection
bash
grep "ERROR" app.log # lines containing ERROR
grep -i -n "timeout" app.log # ignore case, show line numbers
grep -r "DB_HOST" /etc/myapp/ # search folders recursively
# A pipe sends the output of one command into the next
cat access.log | grep " 500 " | wc -l # count server errors
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head # top client IPs
# Redirection
echo "hello" > file.txt # overwrite
echo "more" >> file.txt # append
command 2> errors.txt # send errors to a file
command > out.txt 2>&1 # send both outputs to a file
Mental model: Everything is text. Small tools (grep, awk, sed, sort, cut) do one job each. Pipes combine them like building blocks.
Users and Permissions
Every file has an owner, a group and permissions for user, group and others. Each set can have read (4), write (2) and execute (1).
bash
ls -l deploy.sh
# -rwxr-xr-- 1 alice devs 220 Nov 17 10:00 deploy.sh
# |\_/\_/\_/
# | | | +-- others: r-- (4)
# | | +----- group: r-x (5)
# | +-------- owner: rwx (7) -> numeric mode 754
chmod 754 deploy.sh # set permissions numerically
chmod +x deploy.sh # add execute for everyone
chmod u+w,g-w file # symbolic: add owner write, remove group write
chown alice:devs deploy.sh # change owner and group
sudo apt update # run one command as administrator (root)
| Number | Meaning |
| 7 | rwx (read, write, execute) |
| 6 | rw- |
| 5 | r-x |
| 4 | r-- |
| 0 | no permission |
Security: Avoid chmod 777. It lets anyone change the file. Give only the permissions needed. SSH private keys must be chmod 600.
Processes and Services
bash
ps aux | grep nginx # find a process
top # live view (q to quit); htop is friendlier
kill 1234 # ask a process to stop (SIGTERM)
kill -9 1234 # force kill (last resort)
# systemd manages services on modern Linux
sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl enable nginx # start on boot
sudo systemctl restart nginx
journalctl -u nginx -n 100 --no-pager # service logs
journalctl -u nginx -f # follow logs
Create your own service by placing a unit file in /etc/systemd/system/:
ini
# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
[Service]
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 app.py
Restart=on-failure
Environment=PORT=8080
[Install]
WantedBy=multi-user.target
bash
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
Packages
| Distro family | Update and install |
| Debian / Ubuntu | sudo apt update && sudo apt install -y nginx |
| RHEL / Fedora / Amazon Linux | sudo dnf install -y nginx |
| Alpine (small containers) | apk add --no-cache curl |
SSH: Remote Access
bash
ssh-keygen -t ed25519 -C "me@example.com" # create a key pair
ssh-copy-id user@10.0.0.5 # install your public key on the server
ssh user@10.0.0.5 # log in
ssh -i ~/.ssh/mykey.pem ec2-user@host # use a specific key
scp report.txt user@host:/tmp/ # copy a file
rsync -avz ./site/ user@host:/var/www/site/ # efficient sync
ssh -L 8080:localhost:80 user@host # tunnel a remote port to your machine
Use keys instead of passwords, and disable password login and root login in /etc/ssh/sshd_config on real servers.
Network Troubleshooting Commands
bash
ip addr # IP addresses
ping -c 3 example.com # reachability
curl -I https://example.com # HTTP headers
curl -s localhost:8080/health # test an API
ss -tulpn # which process listens on which port
dig example.com # DNS lookup
traceroute example.com # path to a host
Cron: Scheduled Jobs
bash
crontab -e
# minute hour day-of-month month day-of-week command
0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1 # every day 02:00
*/5 * * * * /opt/scripts/check.sh # every 5 minutes
Bash Scripting
A script is a text file of commands. Start with a shebang line and make the file executable.
bash
#!/usr/bin/env bash
set -euo pipefail # stop on errors, unset variables and failed pipes
APP_NAME="myapp"
LOG_DIR="/var/log/${APP_NAME}"
TODAY=$(date +%F) # command substitution
echo "Backing up ${APP_NAME} on ${TODAY}"
# Conditions
if [[ ! -d "$LOG_DIR" ]]; then
echo "Creating $LOG_DIR"
mkdir -p "$LOG_DIR"
fi
# Loop over files
for f in /etc/${APP_NAME}/*.conf; do
echo "Found config: $f"
done
# Function with arguments
check_port() {
local host="$1" port="$2"
if timeout 3 bash -c "</dev/tcp/${host}/${port}" 2>/dev/null; then
echo "OK ${host}:${port}"
else
echo "FAIL ${host}:${port}"
return 1
fi
}
check_port localhost 22
echo "Script finished with exit code $?"
bash
chmod +x backup.sh
./backup.sh
| Item | Meaning |
$1, $2 | Script arguments |
$? | Exit code of the last command (0 means success) |
$#, $@ | Number of arguments, all arguments |
set -e | Exit when any command fails |
set -u | Error on unset variables |
set -o pipefail | Fail if any command in a pipe fails |
"${var}" | Always quote variables to handle spaces safely |
Practice Tasks
- Write a script that prints disk usage and warns if any filesystem is above 80 percent
- Create a systemd service that runs a tiny Python web server and restarts on failure
- Use
awk and sort to find the five most frequent URLs in a sample access log
- Set up SSH key login to a VM and disable password login
- Add a cron job that deletes log files older than 7 days
Conclusion
Linux fundamentals plus Bash scripting are the base of DevOps automation. You can now move around, read logs, manage permissions and services, connect with SSH and write safe scripts.
Quick Summary: navigate; grep + pipes; chmod/chown; systemctl; SSH keys; cron; Bash with set -euo pipefail.