# Essential Linux Commands for System Administrators

Linux is a powerful operating system that is widely used in servers, desktops, and embedded systems. Mastering basic commands is crucial for any system administrator. This guide covers fundamental operations, user management, package management, networking, firewall management, and backup strategies.

## 1\. Basic File and Directory Operations

### Create/Change/Remove Directories and Files

* **Create a Directory**: `mkdir directoryname`
    
* **Change Directory**: `cd myfolder/`
    
* **Copy a File**: `cp filename destination`
    
* **Move/Rename a File**: `mv source destination`
    
* **Remove a File/Directory**:
    
    * File: `rm filename`
        
    * Directory: `rm -r directoryname/`
        

### View and Search File Contents

* **Display File Content**: `cat filename`
    
* **Search for Text within a File**: `grep pattern filename`
    
* **View Last 10 Lines of a File**: `tail -n 10 filename`
    
* **Print Specific Columns**: `awk '{ print $1 }' filename`
    

### Find and Edit

* **Find a Directory**: `sudo find / -type d -name directoryname`
    
* **Find a File**: `sudo find / -type f -name filename`
    
* **Edit File Text**: `sed -i 's/old_text/new_text/g' filename`
    

## 2\. File Permissions and Ownership

### File Permissions

* **Types**: Read (r), Write (w), Execute (x)
    
* **Modify User Permissions**: `sudo chmod u+rwx filename`
    
* **Set Permissions Numerically**: `sudo chmod 777 filename`
    

### Ownership Management

* **Change File Owner/Group**: `sudo chown user:group filename`
    
* **Change File Group**: `sudo chgrp group filename`
    

## 3\. User & Group Management

### User Management

* **Create User**: `sudo useradd -m -s /bin/bash username`
    
* **Set User Password**: `sudo passwd username`
    
* **Delete a User**: `sudo userdel username`
    
* **Add User to Group**: `sudo usermod -aG groupname username`
    
* **Set Account Expiration**: `sudo chage -E YYYY-MM-DD username`
    

### Group Management

* **Create New Group**: `sudo groupadd groupname`
    
* **Delete Group**: `sudo groupdel groupname`
    

## 4\. Package Management (Ubuntu/Debian)

### Updating and Installing Packages

* **Update Package List**: `sudo apt update`
    
* **Upgrade All Installed Packages**: `sudo apt upgrade`
    
* **Install Package**: `sudo apt install package-name`
    
* **Remove Package**: `sudo apt remove package-name`
    
* **Purge Package**: `sudo apt purge package-name`
    

## 5\. Service Management

### Service Commands

* **Check Service Status**: `sudo systemctl status servicename`
    
* **Manage Service Lifecycle**:
    
    * Start: `sudo systemctl start servicename`
        
    * Stop: `sudo systemctl stop servicename`
        
    * Restart: `sudo systemctl restart servicename`
        
    * Enable: `sudo systemctl enable servicename`
        
    * Disable: `sudo systemctl disable servicename`
        
* **View Service Logs**: `sudo journalctl -u servicename`
    

## 6\. Process Management

### Process Viewing and Monitoring

* **List Processes**: `ps aux`
    
* **Process Viewer**: `top` or `htop`
    
* **I/O Stats**: `iostat`
    

### Process Killing

* **Kill Process**:
    
    * By PID: `kill PID`
        
    * By Name: `pkill process_name`
        
    * Force Kill: `kill -9 PID`
        

## 7\. Networking Management

### Network Configuration

* **View Network Interfaces**: `ifconfig` or `ip addr`
    
* **View Routing Table**: `ip route`
    
* **Ping a Host**: `ping hostname`
    
* **Trace Route to a Host**: `traceroute hostname`
    

### Network Diagnostics

* **DNS Lookup**: `dig domain` or `nslookup domain`
    
* **List Active Connections**: `sudo netstat -tuln` or `ss -tuln`
    
* **Scan Open Ports**: `sudo nmap -sT IP`
    

## 8\. Firewall Management

### UFW (Uncomplicated Firewall)

* **Manage Firewall**: `sudo ufw status`, `enable`, `disable`
    
* **Allow Ports**: `sudo ufw allow port/protocol`
    
* **Deny Ports**: `sudo ufw deny port/protocol`
    
* **Allow Services**: `sudo ufw allow http`
    

### Firewalld (Alternative)

* **Check Firewall Status**: `sudo firewall-cmd --state`
    
* **Add Service to Zone**: `sudo firewall-cmd --zone=public --add-service=http --permanent`
    
* **Add Port to Zone**: `sudo firewall-cmd --zone=public --add-port=80 --permanent`
    
* **Apply Changes**: `sudo firewall-cmd --reload`
    

## 9\. Disk Management

### Disk Usage

* **Check Disk Space**: `df -h`
    
* **Disk Usage per Directory**: `du -h`
    
* **List Block Devices**: `lsblk`
    

### Partition and LVM (Logical Volume Management)

* **Create Partitions**: `sudo fdisk /dev/sdX`
    
* **Format Partition**: `sudo mkfs.ext4 /dev/sdX1`
    
* **Mount Partition**: `sudo mount /dev/sdX1 /mnt/newdisk`
    

### LVM Commands

* **Create Physical Volume**: `sudo pvcreate /dev/sdX`
    
* **Create Volume Group**: `sudo vgcreate volume_group /dev/sdX`
    
* **Create Logical Volume**: `sudo lvcreate -L 10G -n logical_volume volume_group`
    
* **Mount Logical Volume**: `sudo mount /dev/volume_group/logical_volume /mnt/my_lvm`
    
* **Extend Volume:** `sudo lvextend -L +5G /dev/<volume_group>/<logical_volume>`
    
* **Resize Volume:** `sudo resize2fs /dev/<volume_group>/<logical_volume>`
    

## 10\. Backup and Restore with tar and rsync

### Backup and Extract with tar

* **Create Compressed Backup**: `tar -cvzf backup.tar.gz /path/to/backup`
    
* **Extract Backup**: `tar -xvzf backup.tar.gz -C /path/to/restore`
    

### Remote Backup with rsync

* **Configure SSH**:
    
    * Generate SSH Key: `ssh-keygen`
        
    * Copy SSH Key to Remote Server: `ssh-copy-id user@remote_ip`
        
* **Backup with rsync**: `rsync -avz source user@remote_ip:/destination`
