This tutorial covers essential skills for intermediate system administrators, including user management, process monitoring, disk partitioning, logging and auditing, and basic shell scripting. By the end, you’ll have a solid foundation to effectively manage Linux/Unix systems.
Step 1: User and Group Management
Learn to create, modify, and delete users and groups using commands like useradd, usermod, userdel, groupadd, groupmod, and groupdel. Understand file permissions and ownership.
Example:
# Create a new user
useradd -m johndoe
# Add user to supplementary groups
usermod -aG wheel,developers johndoePractice Exercise
Create a script that adds a new user, assigns them to a primary group, and sets their password. The username, group, and password should be provided as arguments to the script.
Show Solution
#!/bin/bash
username=$1
group=$2
password=$3
# Create user and assign primary group
useradd -g $group $username
# Set password for user
echo $password | passwd --stdin $username
```
This script takes three command line arguments: the username, primary group, and password. It uses `useradd` to create the user with the specified primary group, and `passwd` to set the user's password from stdin.Step 2: Process Management
Use commands like ps, top, htop, kill, and nice to monitor and control processes. Understand process states, signals, and priorities.
Example:
# View processes for current user
ps -u $USER
# Renice a process
renice 10 PIDPractice Exercise
Write a script that monitors a specific process and logs a message if the CPU usage exceeds a given threshold. The process name and CPU threshold should be provided as arguments.
Show Solution
#!/bin/bash
process=$1
threshold=$2
while true; do
cpu_usage=$(ps -o pcpu= -C $process | awk '{sum+=$1} END {print sum}')
if ((cpu_usage > $threshold)); then
logger "$process exceeded CPU threshold of $threshold% with $cpu_usage%"
fi
sleep 1
done
```
This script takes the process name and CPU threshold as arguments. It runs in an infinite loop, using `ps` to get the total CPU usage of the named process. If usage exceeds the threshold, it logs a message using `logger`. The `sleep` command adds a 1 second delay between checks.Step 3: Disk Partitioning and File Systems
Learn to create, format, and mount disk partitions using fdisk, parted, mkfs, and mount. Understand the differences between file systems like ext4, XFS, and Btrfs.
Example:
# Create ext4 file system
mkfs.ext4 /dev/sdb1
# Mount file system
mount /dev/sdb1 /mnt/dataPractice Exercise
Write a script that checks the disk space usage of a mount point specified as an argument. If usage exceeds 90%, add a new disk, create a partition, format it, and mount it to an alternate location. Assume the new disk is /dev/sdc and mount it to /mnt/overflow.
Show Solution
#!/bin/bash
mount_point=$1
usage=$(df -h $mount_point | tail -1 | awk '{print $5}' | cut -d'%' -f1)
if ((usage > 90)); then
fdisk /dev/sdc <<EOF
n
p
1
w
EOF
mkfs.ext4 /dev/sdc1
mkdir /mnt/overflow
mount /dev/sdc1 /mnt/overflow
logger "Mounted overflow disk due to $usage% usage on $mount_point"
fi
```
This script checks the usage percentage of the provided mount point using `df`. If over 90%, it uses `fdisk` to non-interactively create a new primary partition spanning the entire new disk. It creates an ext4 filesystem, makes the mount directory, and mounts the new partition. Finally it logs a message about the overflow mount.Step 4: Logging and Auditing
Configure system logging with rsyslog or syslog-ng. Manage log rotation with logrotate. Use auditd to track security-relevant events.
Example auditd rule:
-w /etc/passwd -p wa -k identity
This watches /etc/passwd for writes and attribute changes, logging them with the key 'identity'.
Practice Exercise
Create a logrotate configuration file for a custom log directory /var/log/myapp/. Rotate the logs weekly, keep 4 rotations, compress the rotated logs, and restart the myapp service after rotation.
Show Solution
/var/log/myapp/*.log {
weekly
rotate 4
compress
postrotate
/bin/systemctl restart myapp
endscript
}
```
This `logrotate` configuration matches any `.log` file in `/var/log/myapp/`. It rotates on a weekly basis, keeping the 4 most recent rotations. The `compress` directive compresses rotated logs. The `postrotate` script restarts the `myapp` service after rotation to ensure it starts new log files.Step 5: Basic Shell Scripting
Automate tasks using bash scripts. Understand variables, control structures (if, while, for), and common commands.
Example:
if [[ $1 == "start" ]]; then
systemctl start httpd
elif [[ $1 == "stop" ]]; then
systemctl stop httpd
else
echo "Usage: $0 start|stop"
exit 1
fiPractice Exercise
Write a script that backs up a directory specified as the first argument. Use the current date as part of the backup filename. Exclude any subdirectories named 'temp' or files ending in '.tmp'. Compress the backup using tar. Hint: Use parameter expansions for the date and script name.
Show Solution
#!/bin/bash
backup_dir=$1
backup_file="${backup_dir}_$(date +%F).tar.gz"
tar czf $backup_file --exclude='temp' --exclude='*.tmp' $backup_dir
echo "Backup created: $backup_file"
```
This script does:
1. Takes the backup directory as the first command line argument
2. Generates the backup filename using the directory name and current date (`date +%F` formats as YYYY-MM-DD)
3. Uses `tar` to create a gzipped archive of the directory, excluding 'temp' subdirectories and '.tmp' files
4. Outputs the name of the created backup file
The `${backup_dir}` and `$(date +%F)` are parameter expansions that substitute in the values of those variables and commands.Step 6:
Practice Exercise
Show Solution
Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.