>_cat linux-server.md|Linux System
Linux Server Administration Cheatsheet
SPEC: MDX·/linux-server·Kernel, systemd, disk partitioning, LVM, and OS maintenance.
Essential reference for daily Linux server administration, diagnostics, security hardening, user management, and system performance tuning.
System & Hardware Information
# Kernel, distribution, and hostname
uname -a # Kernel version, OS architecture, and hostname
cat /etc/os-release # Linux distribution details (Ubuntu, Debian, RHEL, etc.)
uptime # System uptime and load averages (1 min, 5 min, 15 min)
hostnamectl status # Hostname and machine ID
sudo hostnamectl set-hostname srv-prod-01 # Change hostname permanently
# Hardware & CPU Architecture
lscpu # CPU architecture, cores, threads, and sockets
sudo lshw -short # Complete hardware inventory summary
lspci # List PCI / PCIe devices (NICs, GPUs, RAID controllers)
lsusb # List USB devices
sudo dmidecode -t system # Server manufacturer, model, and serial number
sudo dmidecode -t memory # Detailed RAM specifications (type, speed, slot capacity)
# Memory (RAM) Information
free -h # Total, used, free, and cached memory (human-readable)
cat /proc/meminfo # In-depth kernel memory metricsResource Monitoring & Performance Diagnostics
# Real-time process viewing
top # Standard interactive viewer (P: sort CPU, M: sort RAM, c: full path, k: kill)
htop # Enhanced interactive viewer (apt/dnf install htop)
# Virtual memory & I/O statistics
vmstat 1 5 # Virtual memory, swap, I/O, and CPU metrics every 1 sec (5 times)
iostat -xz 1 # Disk I/O utilization and await latency (from sysstat package)
sudo iotop -o # Show active processes performing disk reads/writes
# Per-process and multi-core CPU metrics
pidstat 1 # Per-PID resource statistics every second
mpstat -P ALL 1 # Individual CPU core workload distribution
# Kernel ring buffer & OOM killer alerts
sudo dmesg -T | grep -iE "oom|out of memory|kill" # Check if Out-Of-Memory killer terminated processes
sudo dmesg -T | tail -50 # Recent kernel events with human-readable timestamps
# Periodic command execution
watch -n 2 'free -h; echo ""; uptime' # Execute command every 2 secondsProcess Management
# Finding processes
ps aux # List all running processes
ps aux | grep nginx # Search for a specific process
ps -ef --forest # Display process tree with parent-child relationships
# Identifying high-resource consumers
ps aux --sort=-%cpu | head -10 # Top 10 CPU-consuming processes
ps aux --sort=-%mem | head -10 # Top 10 Memory-consuming processes
# Terminating processes (Signals)
kill -15 <PID> # SIGTERM: Request graceful termination (recommended)
kill -9 <PID> # SIGKILL: Force kill immediately
killall -9 <process_name> # Terminate all processes matching binary name
pkill -f "python app.py" # Terminate processes matching full command-line arguments
# Process Priority (Nice & Renice)
# Nice values range from -20 (highest priority) to 19 (lowest priority)
nice -n 10 backup.sh & # Run task with low priority to avoid spiking system load
sudo renice -n -5 -p <PID> # Elevate priority of a running process
# Running detached background jobs
nohup ./long-task.sh > task.log 2>&1 &
# View active background jobs in shell:
jobs -l
disown -h %1 # Detach job from current shell session
# Dealing with Zombie (Defunct) processes
# Zombie processes cannot be killed directly. Find their Parent PID (PPID):
ps -o ppid= -p <zombie_pid>
# Terminate or restart the parent process:
kill -15 <parent_pid>Users, Groups & Sudoers Administration
# Identifying active users and logins
whoami # Current active username
id # UID, GID, and active group memberships
w # Who is currently logged in, from where, and their active command
last -n 10 # Last 10 successful logins to the system
# User creation & management
sudo useradd -m -s /bin/bash deploy # Create user 'deploy', create home directory, set default shell
sudo passwd deploy # Set or change user password
# Group management
sudo usermod -aG sudo deploy # Add user to sudoers group (Ubuntu / Debian)
sudo usermod -aG wheel deploy # Add user to wheel group (RHEL / CentOS / Rocky)
# Account lock & removal
sudo passwd -l deploy # Lock account (prevent password login)
sudo passwd -u deploy # Unlock account
sudo userdel -r deploy # Delete user along with home directory
# Sudoers Configuration
sudo visudo # Safely edit /etc/sudoers with syntax validation
# Common sudoers rule example:
# Allow deploy user full sudo access without password prompt:
# deploy ALL=(ALL) NOPASSWD: ALLSSH Hardening & Security
Key configuration lives in /etc/ssh/sshd_config.
# Copy client public key to server
# From local client terminal:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip
# Required SSH file permissions on server (Critical: strict permissions prevent auth rejections!)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# Recommended directives in /etc/ssh/sshd_config:
# PermitRootLogin no # Disable direct root login
# PasswordAuthentication no # Require SSH keys, disable passwords
# PubkeyAuthentication yes # Enable public key authentication
# Port 22 # SSH port (change if needed to reduce automated bots)
# MaxAuthTries 3 # Maximum authentication attempts per connection
# Test SSH configuration syntax before restarting (Prevents lockout!)
sudo sshd -t
# Restart SSH daemon
sudo systemctl restart ssh # Ubuntu / Debian
sudo systemctl restart sshd # RHEL / CentOS / Rocky
# Monitor failed brute-force login attempts
sudo grep "Failed password" /var/log/auth.log | tail -20 # Debian/Ubuntu
sudo grep "Failed password" /var/log/secure | tail -20 # RHEL/CentOSFile Permissions & Special Bits
# Standard permissions
chmod 755 script.sh # Owner: rwx (7), Group: r-x (5), Others: r-x (5)
chmod 644 config.conf # Owner: rw- (6), Group: r-- (4), Others: r-- (4)
chmod +x script.sh # Make file executable
# Ownership
sudo chown deploy:www-data file.txt # Set owner to 'deploy' and group to 'www-data'
sudo chown -R deploy:www-data /var/www/ # Recursively apply ownership
# Bulk web directory permissions repair
# Standard fix for Laravel, WordPress, and Next.js permission errors:
find /var/www/html -type d -exec chmod 755 {} + # All directories to 755
find /var/www/html -type f -exec chmod 644 {} + # All files to 644
# Special permissions (Sticky Bit & SGID)
sudo chmod +t /shared/dir # Sticky Bit: Only file owners can delete their files in this folder
sudo chmod g+s /shared/projects # SGID: Newly created files inherit the parent directory's groupService Management (Systemd) & Logs
# Managing services
sudo systemctl start <service>
sudo systemctl stop <service>
sudo systemctl restart <service>
sudo systemctl reload <service> # Reload configuration without dropping connections (e.g. Nginx)
sudo systemctl enable <service> # Enable service to start on system boot
sudo systemctl disable <service> # Disable autostart on boot
sudo systemctl status <service> # Check status, runtime state, and recent logs
# Inspecting logs with journalctl
sudo journalctl -u <service> -n 100 --no-pager # Last 100 log lines for a service
sudo journalctl -u <service> -f # Real-time tail output
sudo journalctl -p err -b # Show all errors logged since current boot
sudo journalctl -xe # Extended recent errors with explanationsTask Scheduling (Cron & Timers)
# User crontab management
crontab -e # Edit current user's crontab
crontab -l # List current user's crontab
sudo crontab -u www-data -e # Edit crontab for another user
# Cron syntax breakdown:
# .---------------- Minute (0 - 59)
# | .------------- Hour (0 - 23)
# | | .---------- Day of Month (1 - 31)
# | | | .------- Month (1 - 12)
# | | | | .---- Day of Week (0 - 6, 0 = Sunday)
# | | | | |
# * * * * * <command to execute>
# Practical examples:
# 0 2 * * * /usr/local/bin/backup.sh > /var/log/backup.log 2>&1 # Run at 02:00 AM daily
# */15 * * * * /usr/bin/php /var/www/artisan schedule:run >> /dev/null 2>&1 # Every 15 minutes
# @reboot /usr/local/bin/startup-script.sh # Execute once upon system boot
# View systemd active timers
systemctl list-timersSystem Limits & Kernel Tuning (sysctl)
# File descriptors and process limits
ulimit -n # Max open file descriptors per session
ulimit -u # Max processes per user
# Permanent limits in /etc/security/limits.conf
# Add directives for high-load web/database servers:
# * soft nofile 65535
# * hard nofile 65535
# * soft nproc 32768
# * hard nproc 32768
# Kernel parameter inspection and runtime changes
sudo sysctl -a # Display all active kernel parameters
sudo sysctl -w net.core.somaxconn=1024 # Apply parameter change immediately
# Persistent tuning in /etc/sysctl.d/99-server.conf:
# net.core.somaxconn = 65535
# fs.file-max = 2097152
# vm.max_map_count = 262144
# Apply sysctl changes without rebooting:
sudo sysctl -p /etc/sysctl.d/99-server.confPower Management & System Maintenance
# Flush unwritten file system buffers to disk
sync
# Shutdown & Scheduled Reboot
sudo shutdown -h now # Shut down system immediately
sudo reboot # Reboot system immediately
sudo shutdown -r +15 "Kernel maintenance reboot in 15 minutes" # Scheduled broadcast reboot
sudo shutdown -c # Cancel a pending shutdown/reboot