Linux System Administration

Comprehensive guide to managing and maintaining Linux server environments
Server Administration Intermediate 35 min read

Overview

This comprehensive guide covers essential Linux system administration tasks, from basic system management to advanced server configuration. Whether you're managing Ubuntu, CentOS, RHEL, or other Linux distributions, this guide provides practical commands, best practices, and troubleshooting techniques for effective server administration.

Quick Reference

  • Package Management: apt, yum, dnf, zypper
  • Service Management: systemd, service, chkconfig
  • User Management: useradd, usermod, groupadd
  • File Permissions: chmod, chown, chgrp

1. System Information and Monitoring

1.1 System Information Commands

Essential commands for gathering system information and monitoring performance.

Basic System Information:

# System information
uname -a
hostnamectl
lsb_release -a

# Hardware information
lscpu
free -h
lsblk
df -h

# Memory usage
cat /proc/meminfo
vmstat 1 5

# CPU usage
top
htop
iostat 1 5

1.2 Process Management

Managing processes and system resources effectively.

Process Commands:

# List running processes
ps aux
ps -ef

# Real-time process monitoring
top
htop

# Kill processes
kill -9 PID
pkill process_name
killall process_name

# Process priority
nice -n 10 command
renice 10 PID

2. Package Management

2.1 APT (Debian/Ubuntu)

Package management for Debian-based distributions.

APT Commands:

# Update package lists
sudo apt update

# Upgrade packages
sudo apt upgrade
sudo apt full-upgrade

# Install packages
sudo apt install package_name
sudo apt install package1 package2

# Remove packages
sudo apt remove package_name
sudo apt purge package_name

# Search packages
apt search keyword
apt show package_name

# Clean up
sudo apt autoremove
sudo apt autoclean

2.2 YUM/DNF (Red Hat/CentOS/Fedora)

Package management for Red Hat-based distributions.

YUM/DNF Commands:

# Update packages
sudo yum update
sudo dnf update

# Install packages
sudo yum install package_name
sudo dnf install package_name

# Remove packages
sudo yum remove package_name
sudo dnf remove package_name

# Search packages
yum search keyword
dnf search keyword

# List installed packages
yum list installed
dnf list installed

# Clean cache
sudo yum clean all
sudo dnf clean all

3. User and Group Management

3.1 User Management

Creating and managing user accounts and permissions.

User Commands:

# Create user
sudo useradd -m -s /bin/bash username
sudo passwd username

# Modify user
sudo usermod -aG groupname username
sudo usermod -s /bin/zsh username

# Delete user
sudo userdel username
sudo userdel -r username  # Remove home directory

# List users
cat /etc/passwd
getent passwd

# Switch user
su - username
sudo -u username command

3.2 Group Management

Managing groups and group memberships.

Group Commands:

# Create group
sudo groupadd groupname

# Add user to group
sudo usermod -aG groupname username
sudo gpasswd -a username groupname

# Remove user from group
sudo gpasswd -d username groupname

# Delete group
sudo groupdel groupname

# List groups
groups username
getent group

4. File System Management

4.1 File Permissions

Managing file and directory permissions effectively.

Permission Commands:

# Change permissions
chmod 755 filename
chmod u+x filename
chmod g-w filename
chmod o+r filename

# Change ownership
chown username filename
chown username:groupname filename
chown -R username:groupname directory/

# Change group
chgrp groupname filename
chgrp -R groupname directory/

# View permissions
ls -l
stat filename

# Set default permissions
umask 022

4.2 Disk Management

Managing disk space and file systems.

Disk Commands:

# List disks and partitions
lsblk
fdisk -l
parted -l

# Check disk usage
df -h
du -h directory/
du -sh *

# Mount filesystems
sudo mount /dev/sdb1 /mnt
sudo umount /mnt

# Create filesystem
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.xfs /dev/sdb1

# Check filesystem
sudo fsck /dev/sdb1

5. Service Management

5.1 Systemd Service Management

Managing services with systemd (modern Linux distributions).

Systemd Commands:

# Service control
sudo systemctl start service_name
sudo systemctl stop service_name
sudo systemctl restart service_name
sudo systemctl reload service_name

# Service status
sudo systemctl status service_name
sudo systemctl is-active service_name
sudo systemctl is-enabled service_name

# Enable/disable services
sudo systemctl enable service_name
sudo systemctl disable service_name

# List services
systemctl list-units --type=service
systemctl list-unit-files --type=service

5.2 Traditional Service Management

Managing services with traditional init systems.

Traditional Commands:

# Service control
sudo service service_name start
sudo service service_name stop
sudo service service_name restart

# Service status
sudo service service_name status

# Enable/disable services
sudo chkconfig service_name on
sudo chkconfig service_name off
sudo chkconfig --list

6. Network Configuration

6.1 Network Interface Management

Configuring and managing network interfaces.

Network Commands:

# List network interfaces
ip addr show
ifconfig

# Configure IP address
sudo ip addr add 192.168.1.10/24 dev eth0
sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0

# Bring interface up/down
sudo ip link set eth0 up
sudo ip link set eth0 down
sudo ifup eth0
sudo ifdown eth0

# Configure routing
sudo ip route add default via 192.168.1.1
sudo route add default gw 192.168.1.1

6.2 Network Troubleshooting

Diagnosing and resolving network connectivity issues.

Troubleshooting Commands:

# Test connectivity
ping 8.8.8.8
ping google.com

# Trace route
traceroute 8.8.8.8
mtr 8.8.8.8

# DNS resolution
nslookup google.com
dig google.com
host google.com

# Port connectivity
telnet hostname port
nc -zv hostname port
nmap -p port hostname

7. Log Management

7.1 System Logs

Managing and analyzing system logs for troubleshooting.

Log Commands:

# View system logs
journalctl
journalctl -u service_name
journalctl -f  # Follow logs

# Traditional log files
tail -f /var/log/syslog
tail -f /var/log/messages
tail -f /var/log/auth.log

# Search logs
grep "error" /var/log/syslog
journalctl | grep "error"

# Log rotation
sudo logrotate -f /etc/logrotate.conf

7.2 Log Analysis

Analyzing logs for security and performance issues.

Analysis Commands:

# Count log entries
grep -c "error" /var/log/syslog
wc -l /var/log/syslog

# Find recent errors
grep "$(date +%b\ %d)" /var/log/syslog | grep -i error

# Monitor failed login attempts
grep "Failed password" /var/log/auth.log
grep "Invalid user" /var/log/auth.log

# Analyze web server logs
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr

8. Security Configuration

8.1 Firewall Management

Configuring firewall rules for network security.

UFW (Ubuntu Firewall):

# Enable/disable firewall
sudo ufw enable
sudo ufw disable

# Allow/deny ports
sudo ufw allow 22
sudo ufw allow 80/tcp
sudo ufw deny 23

# Allow from specific IP
sudo ufw allow from 192.168.1.0/24

# View status
sudo ufw status
sudo ufw status verbose

iptables (Advanced Firewall):

# List rules
sudo iptables -L
sudo iptables -L -n -v

# Allow SSH
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Allow HTTP/HTTPS
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# Drop all other traffic
sudo iptables -A INPUT -j DROP

# Save rules
sudo iptables-save > /etc/iptables/rules.v4

8.2 SSH Security

Securing SSH access and configuration.

SSH Configuration:

# Edit SSH config
sudo nano /etc/ssh/sshd_config

# Key configurations:
# Port 22
# PermitRootLogin no
# PasswordAuthentication no
# PubkeyAuthentication yes
# AllowUsers username

# Restart SSH service
sudo systemctl restart ssh

# Generate SSH key pair
ssh-keygen -t rsa -b 4096
ssh-copy-id username@server

9. Backup and Recovery

9.1 Backup Strategies

Implementing effective backup solutions for Linux systems.

Backup Commands:

# Create tar backup
tar -czf backup_$(date +%Y%m%d).tar.gz /home/username
tar -czf backup_$(date +%Y%m%d).tar.gz /etc /var/log

# Create incremental backup
tar -czf backup_$(date +%Y%m%d).tar.gz --newer-mtime="1 week ago" /home/username

# Rsync backup
rsync -avz /home/username/ /backup/username/
rsync -avz --delete /home/username/ /backup/username/

# DD backup (disk image)
sudo dd if=/dev/sda of=/backup/disk_image.img bs=4M

# Restore from backup
tar -xzf backup_20231201.tar.gz -C /restore/location/

9.2 Automated Backups

Setting up automated backup scripts and cron jobs.

Backup Script Example:

#!/bin/bash
# backup_script.sh

BACKUP_DIR="/backup"
SOURCE_DIR="/home/username"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="backup_${DATE}.tar.gz"

# Create backup
tar -czf "${BACKUP_DIR}/${BACKUP_FILE}" "${SOURCE_DIR}"

# Remove old backups (keep last 7 days)
find "${BACKUP_DIR}" -name "backup_*.tar.gz" -mtime +7 -delete

# Log backup completion
echo "Backup completed: ${BACKUP_FILE}" >> /var/log/backup.log

Cron Job Setup:

# Edit crontab
crontab -e

# Daily backup at 2 AM
0 2 * * * /path/to/backup_script.sh

# Weekly full backup
0 2 * * 0 /path/to/full_backup_script.sh

# List cron jobs
crontab -l

10. Performance Tuning

10.1 System Optimization

Optimizing Linux system performance for better efficiency.

Performance Commands:

# Monitor system resources
htop
iotop
nethogs

# Check I/O performance
iostat -x 1 5
sar -d 1 5

# Memory optimization
echo 3 > /proc/sys/vm/drop_caches
sysctl vm.swappiness=10

# CPU optimization
echo performance > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# Network optimization
echo 'net.core.rmem_max = 16777216' >> /etc/sysctl.conf
echo 'net.core.wmem_max = 16777216' >> /etc/sysctl.conf
sysctl -p

10.2 Kernel Parameters

Optimizing kernel parameters for better performance.

Kernel Tuning:

# Edit sysctl.conf
sudo nano /etc/sysctl.conf

# Key optimizations:
# net.core.rmem_max = 16777216
# net.core.wmem_max = 16777216
# net.ipv4.tcp_rmem = 4096 65536 16777216
# net.ipv4.tcp_wmem = 4096 65536 16777216
# vm.swappiness = 10
# fs.file-max = 2097152

# Apply changes
sudo sysctl -p

Download the Complete Guide

Get the full PDF version with additional commands, scripts, and advanced administration techniques.

Download PDF