📋 Table of Contents
📝 Nano Text Editor
Nano is a user-friendly, text-based editor designed specifically for editing configuration files, scripts, and text documents directly in the terminal. It comes pre-installed on virtually all Linux distributions, making it the go-to choice for server administrators.
Essential Nano Shortcuts
Nano uses two types of shortcuts: Caret shortcuts (Control key) and Meta shortcuts (Alt key). The most commonly used shortcuts are displayed at the bottom of the Nano interface.
| Shortcut | Description | Example Usage |
|---|---|---|
| Ctrl + X | Exit Nano | Close the editor (prompts to save changes) |
| Ctrl + O | Save (Write Out) | Save changes without closing |
| Ctrl + W | Search | Find text within the file |
| Alt + W | Find Next | Jump to next occurrence of search term |
| Ctrl + \ | Search and Replace | Find and replace text |
| Ctrl + C | Cancel | Cancel the current operation |
Opening Files with Nano
There are two approaches to opening files in Nano: using absolute paths or relative paths.
Example 1: Using Absolute Path
nano /etc/nginx/nginx.conf
This command opens the nginx.conf file using its full path from the root directory.
Example 2: Using Relative Path (Recommended)
cd /etc/nginx
nano nginx.conf
First navigate to the directory, then open the file. This method is preferred as it provides practice with directory navigation.
Nano Workflow Diagram
cd /etc/nginxnano nginx.confCtrl + O
Ctrl + X
⚙️ Configuration Files
Configuration files are plain text files containing directives that control the behavior of services and applications running on your server. Understanding how to read and modify these files is fundamental to server administration.
Understanding Comments in Configuration Files
Most configuration files use the hash symbol (#) to indicate comments. Lines beginning with # are ignored by the system and serve as documentation or disabled directives.
Example Configuration File
# This is a comment - ignored by the system
# server_name example.com; ← This directive is disabled
server_name mysite.com; ← This directive is active
# Some files use semicolons for comments
; database_host = localhost
- Always create a backup before making changes
- Reload or restart the service for changes to take effect
- Test the configuration to ensure it works correctly
# Example: Backing up and restarting a service
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
sudo nano /etc/nginx/nginx.conf
# Make your changes
sudo systemctl restart nginx
📦 Package Management with APT
Package management is the process of installing, updating, and removing software on your Ubuntu server. The Advanced Package Tool (APT) handles all aspects of software management, from resolving dependencies to ensuring proper configuration.
Key Terminology
| Term | Definition |
|---|---|
| Package | Software bundled with all necessary files, configurations, and dependencies |
| Package Dependency | Additional software required for a package to function properly |
| Repository (Repo) | Remote server storing organized, categorized software packages |
| Package Manager | Tool that automates software installation, updates, and removal (APT for Ubuntu) |
Essential APT Commands
| Command | Purpose | Example |
|---|---|---|
| sudo apt update | Refresh package list from repositories | Run before installing or upgrading packages |
| sudo apt upgrade | Install updates for all installed packages | Keep your system secure and up-to-date |
| sudo apt install [package] | Install new software | sudo apt install nginx |
| sudo apt remove [package] | Remove software (keeps configuration files) | sudo apt remove nginx |
| sudo apt purge [package] | Remove software and configuration files | sudo apt purge nginx |
| sudo apt autoremove | Remove unused dependencies | Clean up after uninstalling software |
| apt-cache search [term] | Search for packages in repositories | apt-cache search nginx |
Package Installation Workflow
Complete Installation Example
# Update package list
sudo apt update
# Install nginx web server
sudo apt install nginx
# Verify installation
nginx -v
# Check service status
sudo systemctl status nginx
🔧 Service Management with Systemctl
Systemctl is the command-line interface for managing services on Ubuntu servers that use systemd as their initialization system. It provides comprehensive control over system services, allowing you to start, stop, restart, and monitor them.
Understanding System Components
| Component | Description |
|---|---|
| Systemd | The initialization system and service manager (Process ID: 1) |
| Systemctl | Command-line tool for controlling systemd and services |
| Service | Background process or daemon (e.g., nginx, MySQL, SSH) |
Common Systemctl Commands
| Command | Action | Use Case |
|---|---|---|
| sudo systemctl start [service] | Start a service | Begin running a stopped service |
| sudo systemctl stop [service] | Stop a service | Gracefully shutdown a running service |
| sudo systemctl restart [service] | Restart a service | Apply configuration changes |
| sudo systemctl reload [service] | Reload configuration | Update settings without full restart |
| sudo systemctl enable [service] | Enable auto-start | Start service automatically at boot |
| sudo systemctl disable [service] | Disable auto-start | Prevent automatic startup at boot |
| sudo systemctl status [service] | Check service status | View current state and recent logs |
Example: Managing Nginx Service
# Check if nginx is running
sudo systemctl status nginx
# Start nginx
sudo systemctl start nginx
# Enable nginx to start at boot
sudo systemctl enable nginx
# Restart nginx after configuration changes
sudo systemctl restart nginx
# Reload nginx configuration without downtime
sudo systemctl reload nginx
🔐 SSH Authentication
SSH (Secure Shell) provides secure remote access to your server. Understanding server fingerprints and key-based authentication is essential for maintaining server security.
Server Fingerprints
A server fingerprint is a unique cryptographic identifier that verifies you're connecting to the correct server, preventing man-in-the-middle attacks.
First-Time Connection Process
- Server operating system was reinstalled (expected)
- Potential security threat or man-in-the-middle attack (investigate immediately)
SSH Key Authentication
Key-based authentication is significantly more secure than password authentication. It uses a cryptographic key pair: a public key (stored on the server) and a private key (stored securely on your local machine).
Advantages of Key Authentication
- Enhanced Security: Cryptographic keys are virtually impossible to brute-force
- Convenience: No need to type passwords for each login
- Automation: Enables automated processes and scripts
- Accountability: Each key can be tracked to a specific user
SSH Key Authentication Process
- Private Key: Remains on your local computer (~/.ssh/id_rsa)
- Public Key: Copied to server (~/.ssh/authorized_keys)
- Known Hosts: Server fingerprints stored locally (~/.ssh/known_hosts)
📜 Bash Scripts
Bash scripts are plain text files containing sequences of commands that automate repetitive tasks. They're essential tools for system administrators who need to perform complex operations efficiently.
Anatomy of a Bash Script
#!/bin/bash
# This is the shebang line - tells the system to use bash
# This is a comment - not executed
# Comments explain what the script does
# Actual commands begin here
sudo chown www-data:www-data /var/www/html
echo "Permissions updated successfully"
| Element | Syntax | Purpose |
|---|---|---|
| Shebang | #!/bin/bash |
Specifies the interpreter (must be first line) |
| Comments | # Comment text |
Documentation and notes (ignored during execution) |
| Commands | Standard Linux commands | The actual operations performed by the script |
Example: WordPress Backup Script
#!/bin/bash
# WordPress Backup Script
# Created: 2025-01-15
# Variables
BACKUP_DIR="/var/backups/wordpress"
DATE=$(date +%Y%m%d)
# Create backup directory
mkdir -p $BACKUP_DIR
# Backup files
tar -czf $BACKUP_DIR/wp-files-$DATE.tar.gz /var/www/html
# Backup database
mysqldump -u root wordpress > $BACKUP_DIR/wp-db-$DATE.sql
# Confirmation message
echo "Backup completed: $DATE"
- Make the script executable:
chmod +x script.sh - Run with:
./script.shorbash script.sh - Test scripts thoroughly before using in production
⏰ Cron Jobs - Automated Task Scheduling
Cron is Ubuntu's built-in task scheduler that allows you to automate repetitive tasks such as backups, updates, and system maintenance. Understanding cron syntax is essential for effective server automation.
Cron Management Commands
| Command | Purpose | Example |
|---|---|---|
| crontab -l | List current user's cron jobs | View your scheduled tasks |
| crontab -e | Edit current user's cron jobs | Add or modify scheduled tasks |
| sudo crontab -l | List root user's cron jobs | View system-level tasks |
| sudo crontab -e | Edit root user's cron jobs | Manage system automation |
Cron Syntax Structure
# ┌───────────── minute (0-59)
# │ ┌───────────── hour (0-23)
# │ │ ┌───────────── day of month (1-31)
# │ │ │ ┌───────────── month (1-12)
# │ │ │ │ ┌───────────── day of week (0-6, Sunday=0)
# │ │ │ │ │
# * * * * * command to execute
| Field | Allowed Values | Description |
|---|---|---|
| Minute | 0-59 | Which minute of the hour |
| Hour | 0-23 | Which hour of the day (0 = midnight) |
| Day of Month | 1-31 | Which day of the month |
| Month | 1-12 | Which month (1 = January) |
| Day of Week | 0-6 | Which day of week (0 = Sunday, 6 = Saturday) |
Practical Cron Examples
Example 1: Daily Backup at 2 AM
00 02 * * * /usr/local/bin/backup.sh > /dev/null 2>&1
Explanation: Runs backup script every day at 2:00 AM, sending output to /dev/null
Example 2: Saturday Morning Report at 9:30 AM
30 09 * * 6 /usr/local/bin/weekly-report.sh > /dev/null 2>&1
Explanation: Runs weekly report every Saturday at 9:30 AM
Example 3: Multiple Times Per Day (9 AM, 3 PM, 9 PM)
00 09,15,21 * * * /usr/local/bin/check-system.sh > /dev/null 2>&1
Explanation: Runs system check three times daily at specified hours
Example 4: Twice Monthly (5th and 25th) at 1:15 AM
15 01 5,25 * * /usr/local/bin/maintenance.sh > /dev/null 2>&1
Explanation: Runs maintenance on the 5th and 25th of every month
Example 5: Every 15 Minutes
*/15 * * * * /usr/local/bin/monitor.sh > /dev/null 2>&1
Explanation: Runs monitoring script every 15 minutes
Example 6: Every 2 Hours
00 */2 * * * /usr/local/bin/cleanup.sh > /dev/null 2>&1
Explanation: Runs cleanup script every 2 hours on the hour
Understanding Output Redirection
> /dev/null 2>&1
This redirects all output (both standard output and errors) to /dev/null, preventing cron from sending email notifications. While convenient, it means you won't be notified of failures, so periodic manual verification is recommended.
- Always use absolute paths in cron commands
- Test scripts manually before scheduling them
- Periodically verify that cron jobs are running successfully
- Document complex cron schedules with comments
- Consider logging script output for troubleshooting
@reboot |
Run once at server startup |
@daily |
Run once per day (midnight) |
@weekly |
Run once per week (Sunday midnight) |
@monthly |
Run once per month (1st midnight) |
🎯 Key Takeaways and Next Steps
Mastering Ubuntu server administration is a journey that builds on foundational knowledge. The concepts covered in this guide form the essential toolkit for effective server management.
- Server administration is accessible: With proper knowledge and practice, managing a server becomes straightforward
- Stability is achievable: Servers can run for years without issues when properly maintained
- Updates are critical: Keep both the operating system and applications (like WordPress) regularly updated
- Never experiment on production: Always test changes in development environments first, especially for income-generating sites
- Documentation is your friend: Keep notes on configurations and procedures