Introduction to Linux Server Administration: Essential Commands and Concepts
In this tutorial, we will cover the essential concepts and commands that are crucial for Linux server administration. Linux servers are the backbone of many web applications, data centers, and cloud environments, so understanding these basic to advanced commands will help you manage and maintain your Linux systems effectively.
Prerequisites
- A Linux based OS installed on dedicated server or KVM VPS.
- A root user access or normal user with administrative privileges.
Linux Server Administration Essential Commands
1. Understanding the Linux File System
The Linux file system is hierarchical, starting from the root directory /. Below are some important directories and their purpose:
- /bin - Contains essential command binaries (e.g., ls, cp).
- /etc - Configuration files for the system.
- /home - Home directories for users.
- /var - Variable data files such as logs.
- /usr - User programs and libraries.
Useful Commands:
pwd: Displays the current working directory.
pwd
ls: Lists files and directories.
ls
cd: Changes the directory.
cd /etc
2. Basic Linux Commands
Here are a few essential commands that every Linux admin must know:
cat: Display file contents.
cat /etc/passwd
cp: Copy files.
cp file1.txt /home/user/file2.txt
mv: Move or rename files.
mv file1.txt /home/user/file1-renamed.txt
rm: Remove files or directories.
rm file1.txt
mkdir: Create a new directory.
mkdir /home/user/newdir
3. Managing Users and Groups
Managing users and groups is a key part of server administration to maintain access control.
Create a new user:
sudo adduser username
Delete a user:
sudo deluser username
Create a new group:
sudo groupadd newgroup
Add a user to a group:
sudo usermod -aG groupname username
Check current users:
who
4. File Permissions and Ownership
Linux files and directories have specific permissions for owners, groups, and others.
Format:
- r = read
- w = write
- x = execute
To see file permissions:
ls -l
To change file permissions:
chmod 755 filename
To change file ownership:
sudo chown newowner:newgroup filename
5. Process Management
Understanding and controlling processes is critical for performance and troubleshooting.
View running processes:
ps aux
Monitor real-time processes:
top
Kill a process:
kill <PID>
Background a process:
<command> &
6. System Monitoring
Monitoring system performance is essential for server administration.
Check disk usage:
df -h
Check memory usage:
free -m
View system logs:
tail -f /var/log/syslog
7. Networking Commands
Linux servers require efficient network configuration and troubleshooting. Here are some basic networking commands:
Check network interface configuration:
ip a
Ping a remote server:
ping google.com
Display network routing table:
route -n
Check open ports:
netstat -tuln
Download a file using wget:
wget http://example.com/file.tar.gz
8. Package Management
Linux distributions use different package managers to install, update, and remove software.
For Debian-based systems (e.g., Ubuntu):
Update package lists:
sudo apt update
Install a package:
sudo apt install package_name
Remove a package:
sudo apt remove package_name
For Red Hat-based systems (e.g., CentOS, AlmaLinux):
Install a package:
sudo yum install package_name
Remove a package:
sudo yum remove package_name
9. Automating Tasks with Cron
Cron jobs are scheduled tasks that automate scripts and commands.
Edit the cron table:
crontab -e
Cron job format:
* * * * * /path/to/command
(The five stars represent minute, hour, day, month, and weekday)
Example: Run a backup every day at 2 AM:
0 2 * * * /home/user/backup.sh
To list scheduled cron jobs:
crontab -l
10. Firewall and Security Essentials
Basic security measures for Linux server administration include configuring firewalls, disabling unused services, and keeping the system up to date.
View firewall status:
sudo ufw status
Allow a port (e.g., SSH):
sudo ufw allow 22
Enable firewall:
sudo ufw enable
To keep your server secure:
Regularly update packages:
sudo apt upgrade # Debian/Ubuntu-based
sudo yum update # Red Hat/CentOS-based
12. grep: Search Through Files
The grep command searches through files or output based on a pattern. This is invaluable when looking for specific information in logs or configuration files.
Search for a specific string in a file:
grep "error" /var/log/syslog
Search recursively in directories:
grep -r "keyword" /etc/
Search case-insensitively:
grep -i "ERROR" /var/log/syslog
Useful options:
- -v (invert the match) to find lines that don’t match the pattern.
- -A or -B to display lines before or after the match.
12. find: Search for Files Based on Criteria
find is a versatile tool to locate files and directories based on name, type, size, date modified, and more.
Find files by name:
find / -name "file.txt"
Find files modified in the last 7 days:
find /var/log -mtime -7
Find and delete files larger than 100MB:
find /home -size +100M -exec rm {} \;
13. rsync: Sync Files and Directories
rsync is a fast and versatile tool for copying and syncing files between directories or servers. It's widely used for backups and data transfer.
Sync local directories:
rsync -avh /source/dir/ /destination/dir/
Sync files to a remote server:
rsync -avz /local/dir/ user@remote:/remote/dir/
-a for archive mode, -v for verbose, -h for human-readable output, and -z for compression.
Only copy updated files:
rsync -av --update /source/ /destination/
14. sed: Stream Editor for Text Manipulation
sed is used to find and replace text in files, making batch edits extremely efficient.
Replace text in a file:
sed -i 's/oldtext/newtext/g' filename
This replaces all occurrences of "oldtext
" with "newtext
" in the file.
Delete lines containing a string:
sed '/pattern/d' filename
Replace only the first occurrence in each line:
sed 's/old/new/' filename
15. awk: Text Processing
awk is a powerful programming language and command for processing and analyzing text data.
Print a specific column from a file:
awk '{print $2}' file.txt
This prints the second column from each line of file.txt
.
Sum up the values in a column:
awk '{s+=$2} END {print s}' file.txt
Filter and print specific rows:
awk '$3 > 100' file.txt
16. tmux: Terminal Multiplexer
tmux allows you to run multiple terminal sessions within a single window and switch between them. It’s very helpful for remote server administration.
Start a new session:
tmux new -s mysession
Detach from a session (without killing it):
Ctrl+b, then d
List all sessions:
tmux ls
Reattach to a session:
tmux attach -t mysession
17. tcpdump: Network Packet Analyzer
tcpdump is a powerful command-line packet analyzer. It allows you to capture and analyze network traffic.
Capture all network traffic:
sudo tcpdump
Capture traffic on a specific interface:
sudo tcpdump -i eth0
Capture traffic on port 80
(HTTP):
sudo tcpdump 'port 80'
Save output to a file:
sudo tcpdump -w capture.pcap
18. lsof: List Open Files
lsof is used to list open files and the processes that opened them. This is useful for diagnosing issues with file usage, networking, and more.
List all open files:
lsof
Find files opened by a specific process:
lsof -p PID
See which process is using a port (e.g., port 80
):
sudo lsof -i :80
19. htop: Interactive Process Viewer
htop is a more user-friendly version of top with a graphical display of system resources and processes.
Install htop (if not installed):
sudo apt install htop # Debian/Ubuntu
sudo yum install htop # CentOS/Red Hat
Run htop:
htop
Use keyboard shortcuts to manage processes, sort by CPU or memory, and kill processes directly.
20. iptables: Manage Firewall Rules
iptables is a powerful tool for configuring the Linux firewall.
List current rules:
sudo iptables -L
Block an IP address:
sudo iptables -A INPUT -s 192.168.1.100 -j DROP
Allow traffic on a specific port (e.g., port 22 for SSH):
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
21. xargs: Execute Commands from Standard Input
xargs is used to build and execute commands using input from other commands.
Delete all files with a certain extension:
find /path -name "*.log" | xargs rm
Pass arguments from standard input:
echo "file1 file2 file3" | xargs touch
22. tar: Archiving Files
tar is the most common utility used to create and extract archive files (usually .tar or .tar.gz).
Create a .tar.gz
archive:
tar -czvf archive.tar.gz /path/to/files
Extract an archive:
tar -xzvf archive.tar.gz
- -c: create archive.
- -x: extract files.
- -v: verbose output.
- -f: file to process.
- -z: compress using gzip.
13. chroot: Change Root Directory
chroot is used to change the root directory for the current process, which is useful for testing or recovery scenarios.
Enter a different root directory:
sudo chroot /path/to/new/root
This confines the user within a new root directory for the system.
24. systemctl: Manage System Services
systemctl is used to control system services (starting, stopping, enabling, or disabling services).
Start a service:
sudo systemctl start nginx
Stop a service:
sudo systemctl stop nginx
Enable a service at boot:
sudo systemctl enable nginx
Check the status of a service:
sudo systemctl status nginx
25. journalctl: View System Logs
journalctl helps you view and manage systemd logs.
View all logs:
sudo journalctl
View logs for a specific service:
sudo journalctl -u nginx.service
View logs since boot:
sudo journalctl -b
Conclusion
This tutorial introduces the Linux Server Administration essential commands. Understanding these essentials will help you efficiently manage your server, troubleshoot issues, and maintain security. As you continue to explore Linux, you'll discover even more tools and techniques to refine your administrative skills.
Advanced Linux commands allow you to have greater control over your system, automate tasks, and ensure optimal performance. Mastering them will elevate your Linux server administration skills and make you more efficient in troubleshooting, managing processes, and ensuring system security.