🐧 Linux System Administration Guide

Automated Updates & External Storage Management

🔄 System Update Automation

Automate system updates using a bash script and cron jobs. This ensures your system stays up-to-date without manual intervention.

Step 1: Create the Update Script

#!/bin/bash # System Update Automation Script # Save as: /usr/local/bin/system-update.sh # Log file location LOGFILE="/var/log/system-update.log" # Function to log messages log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOGFILE" } log_message "Starting system update" # Detect the distribution if [ -f /etc/debian_version ]; then # Debian/Ubuntu log_message "Detected Debian/Ubuntu system" apt-get update >> "$LOGFILE" 2>&1 DEBIAN_FRONTEND=noninteractive apt-get upgrade -y >> "$LOGFILE" 2>&1 apt-get autoremove -y >> "$LOGFILE" 2>&1 apt-get autoclean >> "$LOGFILE" 2>&1 elif [ -f /etc/redhat-release ]; then # RHEL/CentOS/Fedora log_message "Detected RHEL/CentOS/Fedora system" yum update -y >> "$LOGFILE" 2>&1 || dnf upgrade -y >> "$LOGFILE" 2>&1 elif [ -f /etc/arch-release ]; then # Arch Linux log_message "Detected Arch Linux system" pacman -Syu --noconfirm >> "$LOGFILE" 2>&1 else log_message "Unknown distribution" exit 1 fi log_message "System update completed"

Step 2: Make the Script Executable

Command:
sudo nano /usr/local/bin/system-update.sh
sudo chmod +x /usr/local/bin/system-update.sh

Step 3: Set Up Cron Job

Edit crontab:
sudo crontab -e
Add one of these schedules:
# Run every day at 2 AM 0 2 * * * /usr/local/bin/system-update.sh # Run every Sunday at 3 AM 0 3 * * 0 /usr/local/bin/system-update.sh # Run every day at 2 AM and reboot if needed (Debian/Ubuntu) 0 2 * * * /usr/local/bin/system-update.sh && [ -f /var/run/reboot-required ] && /sbin/reboot
ℹ️ Important Notes:
  • The script runs as root (via sudo crontab)
  • Updates are logged to /var/log/system-update.log
  • Test manually first: sudo /usr/local/bin/system-update.sh

💾 External Storage Detection

Learn how to detect and identify external storage devices like SD cards connected to your Linux system.

Quick Check - List All Block Devices

lsblk
Example Output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS loop0 7:0 0 2G 0 loop sda 8:0 1 116.6G 0 disk ├─sda1 8:1 1 512M 0 part /boot/firmware └─sda2 8:2 1 116.1G 0 part / mmcblk0 179:0 0 29.7G 0 disk ├─mmcblk0p1 179:1 0 255M 0 part └─mmcblk0p2 179:2 0 29.5G 0 part

Other Detection Methods

Check kernel messages:
dmesg | tail -20 dmesg | grep -i mmc
List disk devices:
sudo fdisk -l
Check USB devices (if using card reader):
lsusb
Check mounted filesystems:
df -h
🔍 Device Naming:
  • mmcblk0 - SD card (built-in reader)
  • sdb, sdc - USB card reader
  • mmcblk0p1, sdb1 - Partitions on the device

🔧 Formatting Storage for Backup

Format an external storage device (SD card) for use as a Linux backup drive.

Step 1: Unmount Existing Partitions

sudo umount /dev/mmcblk0p1 sudo umount /dev/mmcblk0p2

Step 2: Create New Partition Table

sudo fdisk /dev/mmcblk0
Inside fdisk:
  • Type o (create new DOS partition table) and press Enter
  • Type n (new partition) and press Enter
  • Press Enter (primary partition)
  • Press Enter (partition number 1)
  • Press Enter (first sector, default)
  • Press Enter (last sector, use full disk)
  • Type w (write changes) and press Enter

Step 3: Format the Partition

For ext4 (Linux-only, recommended for backups):
sudo mkfs.ext4 -L BACKUP /dev/mmcblk0p1
For exFAT (Windows/Mac compatibility):
sudo apt-get install exfat-fuse exfat-utils sudo mkfs.exfat -n BACKUP /dev/mmcblk0p1

Step 4: Mount the Drive

sudo mkdir -p /mnt/backup sudo mount /dev/mmcblk0p1 /mnt/backup

Step 5: Set Proper Permissions (ext4 only)

sudo chown -R $USER:$USER /mnt/backup
⚠️ Note: If using exFAT, you cannot use chown. Instead, mount with permissions:
sudo mount -o uid=$(id -u),gid=$(id -g),umask=0022 /dev/mmcblk0p1 /mnt/backup

Step 6: Verify

df -h | grep backup lsblk touch /mnt/backup/test.txt ls -l /mnt/backup/

⚙️ Auto-mounting Configuration

Configure your backup drive to automatically mount on system boot using /etc/fstab.

Step 1: Get the PARTUUID

sudo blkid /dev/mmcblk0p1
Example Output:
/dev/mmcblk0p1: LABEL="BACKUP" UUID="a1b2c3d4-..." TYPE="ext4" PARTUUID="12345678-01"

Step 2: Edit /etc/fstab

sudo nano /etc/fstab

Step 3: Add Entry to fstab

Using device name (simpler):
/dev/mmcblk0p1 /mnt/backup ext4 defaults 0 2
Using PARTUUID (more reliable):
PARTUUID=12345678-01 /mnt/backup ext4 defaults 0 2
Example complete /etc/fstab:
proc /proc proc defaults 0 0 PARTUUID=4c5e577c-01 /boot/firmware vfat defaults 0 2 PARTUUID=4c5e577c-02 / ext4 defaults,noatime 0 1 PARTUUID=12345678-01 /mnt/backup ext4 defaults 0 2

Step 4: Test the Configuration

sudo umount /mnt/backup sudo mount -a df -h | grep backup
✅ Success! Your backup drive will now automatically mount on every boot at /mnt/backup.
⚠️ Common Issues:
  • Operation not permitted when using chown - You're using exFAT. Use mount options instead or reformat to ext4.
  • Device not found - Check device name with lsblk
  • Wrong filesystem type - Verify with sudo blkid

🎯 Quick Reference Commands

Check available storage:
lsblk df -h sudo fdisk -l
Mount/Unmount:
sudo mount /dev/mmcblk0p1 /mnt/backup sudo umount /mnt/backup
Format drives:
sudo mkfs.ext4 -L LABEL /dev/mmcblk0p1 sudo mkfs.exfat -n LABEL /dev/mmcblk0p1
Check device info:
sudo blkid sudo parted -l