Managing an Ubuntu Server efficiently requires mastering key command-line tools. Whether you’re a seasoned sysadmin or new to Linux, these Ubuntu Server commands are critical for streamlining operations, troubleshooting issues, and maintaining peak performance. In this guide, we’ll break down the top 10 commands you need to know, complete with practical examples and use cases.
Table of Contents
Ubuntu Server Commands
1. apt: Simplify Package Management
The apt command is your gateway to installing, updating, and removing software. Ubuntu’s Advanced Package Tool (APT) ensures seamless dependency resolution.
Key Examples:
Update package lists:
sudo apt update
Install a package (e.g., Nginx):
sudo apt install nginx
Remove unused packages:
sudo apt autoremove
Pro Tip: Always run sudo apt update before installing new software to fetch the latest versions.
2. systemctl: Manage Services Like a Pro
Control system services with systemctl, part of the systemd suite. Start, stop, or check the status of critical services effortlessly.
Key Examples:
Start a service:
sudo systemctl start nginx
Enable a service to launch at boot:
sudo systemctl enable nginx
Check service status:
systemctl status ssh
Use Case: Restart a crashed web server with sudo systemctl restart nginx.
3. ssh: Secure Remote Access
Connect to remote servers securely using SSH. Replace outdated tools like Telnet with this encrypted protocol.
Example:
ssh username@192.168.1.50
Pro Tip: Generate SSH keys for password-less logins:
ssh-keygen -t ed25519
4. ufw: Configure Firewalls in Seconds
Uncomplicated Firewall (ufw) simplifies network security. Create rules to allow or block traffic with minimal effort.
Key Examples:
Allow HTTP traffic:
sudo ufw allow 80/tcp
Deny incoming requests from an IP:
sudo ufw deny from 203.0.113.10
Use Case: Enable UFW and check status:
sudo ufw enable
sudo ufw status verbose
5. grep: Find Text Patterns Fast
Search logs, files, or command outputs instantly with grep. Pair it with regular expressions for advanced filtering.
Example:
Search for “error” in logs:
grep -i "error" /var/log/syslog
Pro Tip: Combine grep with journalctl for service-specific logs:
journalctl -u nginx | grep -i "failed"
6. cron: Automate Repetitive Tasks
Schedule scripts or commands using cron. Edit the crontab file to set up time-based jobs.
Example:
Open the crontab editor:
crontab -e
Add a daily backup job:
0 3 * * * /usr/bin/backup-script.sh
Use Case: List active cron jobs:
crontab -l
7. df and du: Monitor Disk Usage
df shows disk space for mounted filesystems.
du reports directory-level usage.
Examples:
df -h # Human-readable format
du -sh /var/log # Check log directory size
Pro Tip: Use ncdu for an interactive disk usage analyzer.
8. journalctl: Dive into System Logs
Inspect systemd logs with journalctl. Filter by time, service, or priority for targeted troubleshooting.
Examples:
View logs for the last hour:
journalctl --since "1 hour ago"
Track kernel messages:
journalctl -k
9. chmod and chown: Master File Permissions
chmod changes file permissions.
chown modifies file ownership.
Examples:
chmod 755 script.sh # Give execute rights
chown www-data:www-data /var/www # Set ownership for web files
Use Case: Recursively update permissions for a directory:
chmod -R 755 /var/www/html
10. lsof: List Open Files and Ports
Identify processes using files or network ports with lsof. Critical for debugging conflicts.
Examples:
Find processes using port 443:
sudo lsof -i :443
List files opened by a user:
lsof -u username
Boost Your Ubuntu Server Expertise Today
Mastering these Ubuntu Server commands will transform your admin workflow. Bookmark this guide, practice with real-world scenarios, and explore advanced variations of these tools.
Frequently Asked Questions (FAQs)
1. “Why do I get ‘Unable to locate package’ errors with apt?
This occurs when package lists are outdated or third-party repositories aren’t configured. Always run sudo apt update first. If the issue persists, verify the repository URL in /etc/apt/sources.list and ensure the package name is spelled correctly.
2. “My SSH connection keeps timing out – how do I fix this?”
Check if the SSH service is running:
sudo systemctl status ssh
If active, verify firewall rules with sudo ufw status. Allow port 22 temporarily for testing:
sudo ufw allow 22/tcp
Also ensure the client’s IP isn’t blocked in /etc/hosts.deny.
3. “UFW isn’t blocking a port I denied – what’s wrong?”
Firewall rules might be overridden by later configurations. Use:
sudo ufw status numbered
to check rule priority. Delete conflicting rules with sudo ufw delete [RULE_NUMBER]. Always reload rules after changes:
sudo ufw reload
4. “Cron jobs aren’t executing – how do I debug them?”
First check cron logs:
grep CRON /var/log/syslog
Ensure:
- Scripts have executable permissions (chmod +x)
- Absolute paths are used (e.g., /usr/bin/python3 instead of python3)
- Output is redirected to a log file for debugging:
* * * * * /path/script.sh >> /var/log/cron.log 2>&1
5. “I deleted files but disk space wasn’t freed – why?”
This often happens when processes hold open file handles. Use lsof to find affected processes:
sudo lsof +L1
Restart the service using the file or reboot the server. For Docker containers, check orphaned volumes with docker system prune.
6. “How do I check which service is using a specific port?”
Combine lsof and netstat for quick diagnosis:
sudo lsof -i :443
# OR
sudo netstat -tulpn | grep ':443'
This reveals the PID and name of the process bound to the port.