Linux

🐧 Linux System Administration

Linux is the backbone of virtually every server, container, and cloud environment. This reference covers essential commands and patterns for day-to-day system administration on Ubuntu/Debian and RHEL/Rocky distributions.

Filesystem & Permissions

Linux permissions are expressed as three groups (owner, group, others) each with read (r=4), write (w=2), and execute (x=1) bits.

bash
chmod 755 /var/www/html          # rwxr-xr-x (owner: rwx, group: rx, other: rx)
chmod 600 ~/.ssh/id_rsa          # rw------- (private key — owner read/write only)
chmod -R 644 /var/www/html/*.html  # Recursive set for files
chown -R www-data:www-data /var/www/html  # Change owner and group
chown deploy:deploy /opt/app

# File listing with details
ls -lah /etc/nginx/              # Long format, human sizes, hidden files
ls -lt /var/log/ | head -20      # Sort by modification time

# Finding files
find /var/log -name "*.log" -mtime +30 -delete   # Delete logs older than 30 days
find /opt/app -type f -name "*.conf" -exec grep -l "debug" {} \;
locate nginx.conf                # Fast file lookup (requires updatedb)

# Disk usage
df -hT                           # Disk space by filesystem type
du -sh /var/log/*                # Size of each log directory
ncdu /                           # Interactive disk usage explorer

Process Management

bash
ps aux                           # All processes (full detail)
ps aux | grep nginx              # Filter by name
pgrep -a nginx                   # Process IDs matching name
htop                             # Interactive process manager (preferred)

# Signals
kill -15 1234                    # SIGTERM — graceful shutdown
kill -9 1234                     # SIGKILL — immediate kill (last resort)
killall nginx                    # Kill all processes named nginx
pkill -f "python worker.py"      # Kill by pattern match

# Background jobs
nohup ./long-script.sh > /var/log/script.log 2>&1 &  # Run detached
jobs -l                          # List background jobs
disown %1                        # Detach job from terminal

# Priority
nice -n 10 ./batch-job.sh        # Start with lower CPU priority
renice +5 -p 1234               # Change priority of running process

Networking

bash
ip addr show                     # Show all interfaces
ip route show                    # Show routing table
ip link set eth0 up              # Enable interface

# Connectivity
ping -c 4 8.8.8.8                # Test ICMP reachability
traceroute api.maxiscomputers.com  # Trace network path
mtr --report api.maxiscomputers.com  # Combined ping + traceroute

# DNS
dig api.maxiscomputers.com       # DNS resolution (full detail)
dig +short api.maxiscomputers.com  # Just the IPs
nslookup api.maxiscomputers.com  # Legacy DNS tool
resolvectl status                # systemd-resolved status

# Ports & Connections
ss -tulpn                        # All listening sockets with PID
ss -tnp state established        # Active TCP connections
netstat -tulpn                   # Legacy equivalent

# Firewall (UFW — Ubuntu)
ufw status verbose
ufw allow 443/tcp
ufw allow from 10.0.0.0/8 to any port 22
ufw deny 23/tcp

# Firewall (firewalld — RHEL/Rocky)
firewall-cmd --list-all
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

# Traffic capture
tcpdump -i eth0 port 80 -w /tmp/capture.pcap
tcpdump -r /tmp/capture.pcap -nn "tcp"

systemd Services

bash
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx           # Reload config without downtime
systemctl status nginx           # View status + recent logs
systemctl enable nginx           # Auto-start on boot
systemctl disable nginx
systemctl is-active nginx

# Logs (journald)
journalctl -u nginx -f           # Follow service logs
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx -p err       # Errors only
journalctl --disk-usage
journalctl --vacuum-size=500M    # Trim journal to 500MB
ini
[Unit]
Description=Maxi's Computers API Server
After=network-online.target postgresql.service
Wants=network-online.target
Requires=postgresql.service

[Service]
Type=simple
User=deploy
Group=deploy
WorkingDirectory=/opt/api-server
ExecStart=/usr/bin/node /opt/api-server/dist/index.js
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
StandardOutput=journal
StandardError=journal
SyslogIdentifier=api-server
LimitNOFILE=65536
NoNewPrivileges=yes
ProtectSystem=strict
PrivateTmp=true

[Install]
WantedBy=multi-user.target

SSH Hardening

Disable password auth Only allow SSH key-based authentication on production servers. Password authentication is vulnerable to brute-force attacks.

bash

## Shell Scripting Patterns

Performance Tuning

bash

## Text Processing (grep, awk, sed)