Essential Linux System Administration Commands
System Operations
Help Commands
1. The man Command
The man (manual) command displays comprehensive documentation for system utilities and configuration files.
- To view the manual for the
lscommand:man ls. - Manual pages are categorized into sections. Use
man manto see information about the manual itself.
2. The help Command
Commands are categorized as internal (built into the shell) or external (seperate programs).
- For internal commands like
cd, use:help cd. - For external commands like
ls, use:ls --help.
3. The info Command
The info system provides detailed, often more extensive, documentation than man. Use info ls for information on the ls command.
File Deletion Commands
rm(Remove): Deletes files or directories.rm -f file.tar.gz: Forcefully deletes the specified file.rm -rf nginx-1.7.8: Recursively and forcefully deletes the specified directory and all its contents.-ror-R: Operate recursively.-f: Force removal without confirmation.
Disk Management Commands
1. Viewing Disk Space
df -h: Displays disk usage for mounted filesystems in a human-readable format.
2. Assessing File and Directory Size
ll -h: Lists files and directories with human-readable sizes.du -h: Summarizes disk usage of files and directories in human-readable format. For example:du -h /data.
3. Analyzing Disk I/O
iostat -x: Displays extended I/O statistics.- Key CPU metrics:
%iowait: CPU idle time waiting for I/O.%idle: CPU idle time.
- Key disk metrics:
%util: Percentage of CPU time spent on I/O requests.await: Average I/O request wait time.svctm: Average I/O request service time.avgqu-sz: Average I/O queue length.
- Key CPU metrics:
Memory Management Commands
1. System Memory Overview
free -h: Shows memory usage.total: Total physical RAM.used: Memory used (includes caches and buffers).free: Unused memory.buff/cache: Memory used for kernel buffers and page cache.- The
-/+ buffers/cacheline shows memory used by applications (used - buff/cache) and memory available to applications (free + buff/cache).
2. Process Memory Details
pswith memory flags:ps -o vsz,rss,sz,size,pid -p [PID]VSZ: Virtual memory size.RSS: Resident Set Size, the non-swapped physical memory used.
CPU Information Commands
lscpu: Provides detailed CPU architecture information, including:- Number of CPU cores and threads.
- CPU model name and frequency.
- Cache sizes.
- Virtualization support.
Package Management
YUM (CentOS/RHEL) and APT (Ubuntu/Debian)
- Check for installed package manager:
which yumorwhich apt-get. - Search for a package:
yum search jdkorapt-cache search jdk. - Install a package:
yum install package_nameorapt-get install package_name. - Remove a package:
yum erase package_nameorapt-get purge package_name.
Homebrew (macOS)
brewis a package manager for macOS, similar toyum/apt-get.
Installation Methods
- Using Package Files:
rpm -i package.rpm(CentOS) ordpkg -i package.deb(Ubuntu). - Using Archive Files: Download a
.tar.gzarchive withwget [URL], then extract withtar -xzf archive.tar.gz.
Locating Installed Software
- Use
whichto find the executable path:which java.
Process Execution
- Interactive Execution: Run with
./program_nameor, if inPATH, justprogram_name. - Background Execution: Use
nohupto keep a process running after terminal logout:nohup command > output.log 2>&1 &. - Service Management: Use system service managers (
systemctl,service) for daemons.
Terminating Processes
Find and terminate a process by keyword:
ps -ef | grep keyword | awk '{print $2}' | xargs kill -9
Or terminate by Process ID (PID): kill -9 [PID].
File Operations
Editing Files with Vim
Vim operates in three modes:
- Command Mode: Default mode for navigation and commands (e.g.,
xto delete,ito insert). - Insert Mode: For text entry (entered by pressing
i). - Command-Line Mode: For save/quit commands (entered by pressing
:).
Basic Vim Workflow
- Open a file:
vim filename.txt. - Press
ito enter Insert mode and edit. - Press
Escto return to Command mode. - Type
:wqand pressEnterto save and quit.
Transferring Files
scp(Secure Copy):scp -r local_directory user@remote_host:/remote/path/.
Viewing File Contents
cat: Outputs entire file.head -n 5 file.log: Shows first 5 lines.tail: Shows last lines of a file.
Listing Files and Permissions
ls -l: Detailed list with permissions.- The first character indicates file type (
-for regular file,dfor directory). ls -la: Lists all files, including hidden ones.
Permission Escalation
su: Switch user (e.g.,su root).sudo -i: Start a shell with root privileges.
Searching for Installed Files
- Use pipes (
|) andgrep:rpm -qa | grep jdkordpkg -l | grep jdk.
Basic File Operations
- Create a directory:
mkdir new_folder. - Remove a directory:
rm -r folder_name.
Network and Process Management
Network Commands
netstat: Displays network connections, routing tables, interface statistics.- Example:
netstat -na | grep 3306to check for MySQL connections.
Process Management
- Viewing Processes:
ps -ef | grep java. - Terminating Processes:
kill [PID]: Sends SIGTERM (graceful termination).kill -9 [PID]: Sends SIGKILL (forceful termination).
System Resource Monitoring
top: Dynamic, real-time view of system processes and resource usage (CPU, memory).- Key columns include
PID,USER,%CPU,%MEM,COMMAND. - To monitor a specific process:
top -p [PID].