Skip to content

🐧 Linux Command-Line Tutorial

📚 Table of Contents

  1. Introduction
  2. Basic System Commands
  3. File and Directory Management
  4. File Viewing and Editing
  5. Process Management
  6. User and Group Management
  7. Networking Commands
  8. Disk and Filesystem Management
  9. Package Management
  10. Scripting and Automation
  11. Security Commands
  12. System Information and Monitoring
  13. Miscellaneous Commands
  14. Appendix: Complete Command Reference

📝 1. Introduction

Welcome to the Linux Command-Line Tutorial! This guide is designed to help you navigate and master essential Linux commands, whether you're a beginner or preparing for system administration tasks. Each section covers a specific category of commands with descriptions, usage examples, and helpful emojis to make learning fun and effective.


🛠ïļ 2. Basic System Commands

These commands are fundamental for interacting with the Linux system, managing aliases, checking system architecture, and more.

Command Description Example Usage ðŸ’ŧ
alias 📛 Create shortcuts for longer commands. Example: alias ll='ls -la'. bash<br>alias gs='git status' # Create a shortcut for git status
arch ðŸ–Ĩïļ Show the system architecture (e.g., 32-bit or 64-bit). bash<br>arch
bash 🐚 Start a new shell session using the Bourne Again SHell. bash<br>bash
clear ðŸ§đ Clear the terminal screen. bash<br>clear
echo ðŸ“Ē Display a line of text/string on the screen. bash<br>echo "Hello, World!"
exit 🚊 Exit the terminal or shell session. bash<br>exit
history 📜 Show the history of executed commands. bash<br>history
pwd 📁 Print the current working directory. bash<br>pwd
uname ðŸ–ēïļ Display system information. bash<br>uname -a # Display all system information

🔍 Examples and Scripts

📛 alias

Description: Create shortcuts for longer commands to save time.

Example:

# Create a shortcut `ll` for `ls -la`
alias ll='ls -la'

# Use the alias
ll

ðŸ“Ē echo

Description: Display a line of text/string on the screen.

Example:

# Display a welcome message
echo "Welcome to the Linux Command-Line Tutorial! 🚀"

📂 3. File and Directory Management

Manage files and directories efficiently using these essential commands.

Command Description Example Usage ðŸ’ŧ
cd 📂 Change the current directory. bash<br>cd /home/user/Documents
cp 📋 Copy files or directories. bash<br>cp source.txt destination.txt
mv 🚚 Move or rename files or directories. bash<br>mv oldname.txt newname.txt
rm 🗑ïļ Remove files or directories. bash<br>rm file.txt<br>rm -r directory/
mkdir 🏗ïļ Create new directories. bash<br>mkdir new_folder
rmdir ❌ Remove empty directories. bash<br>rmdir empty_folder
ln 🔗 Create hard and symbolic links. bash<br>ln -s /path/to/original /path/to/link
touch 🕒 Create an empty file or update file timestamps. bash<br>touch newfile.txt

🔍 Examples and Scripts

📂 cd

Description: Change the current working directory.

Example:

# Navigate to the Documents directory
cd /home/user/Documents

# Navigate back to the previous directory
cd -

🗑ïļ rm

Description: Remove files or directories.

Example:

# Remove a single file
rm unwanted_file.txt

# Remove a directory and its contents recursively
rm -r old_project/

📖 4. File Viewing and Editing

Commands for viewing, searching, and editing file contents.

Command Description Example Usage ðŸ’ŧ
cat 📄 Read and concatenate files. bash<br>cat file.txt
less 📚 View file contents one page at a time. bash<br>less largefile.log
more ➕ View file contents one page at a time (simpler than less). bash<br>more file.txt
head 🏁 Display the first few lines of a file. bash<br>head -n 10 file.txt
tail ðŸū Display the last few lines of a file. bash<br>tail -n 10 file.txt
grep 🔍 Search for patterns within files. bash<br>grep "error" logfile.log
sed ✂ïļ Stream editor for filtering and transforming text. bash<br>sed 's/old/new/g' file.txt
awk 🛠ïļ Pattern scanning and processing language. bash<br>awk '{print $1}' file.txt
vi 🖋ïļ Open the Vi editor for editing files. bash<br>vi script.sh
nano 📝 Open the Nano editor for editing files. bash<br>nano notes.txt

🔍 Examples and Scripts

📄 cat

Description: Read and concatenate files.

Example:

# Display the contents of file.txt
cat file.txt

# Concatenate two files into a new file
cat file1.txt file2.txt > combined.txt

🔍 grep

Description: Search for specific patterns within files.

Example:

# Search for the word "error" in logfile.log
grep "error" logfile.log

# Case-insensitive search for "warning"
grep -i "warning" logfile.log

🖋ïļ vi

Description: Open the Vi editor to edit files.

Example:

# Open script.sh in Vi editor
vi script.sh

# Insert mode commands:
# Press 'i' to enter insert mode
# Type your changes
# Press 'Esc' to exit insert mode
# Type ':wq' to save and quit

Certainly! Let's continue building your Linux Command-Line Tutorial by completing the Process Management section and moving forward with the remaining sections. We'll maintain the use of emojis and practical scripts to enhance engagement and understanding.


🔄 5. Process Management

Manage and monitor system processes effectively.

Command Description Example Usage ðŸ’ŧ
ps 🧎 List currently running processes. bash<br>ps aux
top 📈 Display real-time system processes and resource usage. bash<br>top
htop ðŸ–Ĩïļ Interactive process viewer (requires installation). bash<br>htop
bg 🌙 Move a process to the background. bash<br>bg %1
fg ☀ïļ Bring a background process to the foreground. bash<br>fg %1
kill ⚰ïļ Terminate processes by PID. bash<br>kill 1234
pkill ðŸŽŊ Terminate processes by name. bash<br>pkill firefox
nice 🎚ïļ Set the priority of a process. bash<br>nice -n 10 ./long_running_script.sh
nohup ðŸšŦ🔄 Run a command immune to hangups. bash<br>nohup ./script.sh &

🔍 Examples and Scripts

🧎 ps

Description: List currently running processes.

Example:

# Display all running processes with detailed information
ps aux

# Filter processes by user
ps -u username

📈 top

Description: Display real-time system processes and resource usage.

Example:

# Start the top command to monitor processes
top

# Press 'q' to exit the top interface

⚰ïļ kill

Description: Terminate processes by Process ID (PID).

Example:

# Find the PID of a process (e.g., firefox)
ps aux | grep firefox

# Terminate the process with PID 1234
kill 1234

# Force kill the process if it doesn't terminate gracefully
kill -9 1234

ðŸ‘Ĩ 6. User and Group Management

Commands for managing users and groups on the system.

Command Description Example Usage ðŸ’ŧ
useradd ðŸ‘Ī Create a new user. bash<br>sudo useradd -m newuser
userdel ❌ðŸ‘Ī Delete a user. bash<br>sudo userdel -r olduser
usermod ✏ïļðŸ‘Ī Modify a user's attributes. bash<br>sudo usermod -aG sudo username
groupadd ➕ðŸ‘Ĩ Create a new group. bash<br>sudo groupadd developers
groupdel ❌ðŸ‘Ĩ Delete a group. bash<br>sudo groupdel developers
passwd 🔒 Change a user's password. bash<br>sudo passwd username
chage 📅 Change user password expiry information. bash<br>sudo chage -E 2025-12-31 username
id 🆔 Display user identity information. bash<br>id username
who 👀 Show who is logged on. bash<br>who
whoami 🧑‍ðŸ’ŧ Display the current user ID. bash<br>whoami

🔍 Examples and Scripts

ðŸ‘Ī useradd

Description: Create a new user.

Example:

# Create a new user 'john' with a home directory
sudo useradd -m john

# Set a password for the new user
sudo passwd john

# Add the new user to the 'sudo' group for administrative privileges
sudo usermod -aG sudo john

🔒 passwd

Description: Change a user's password.

Example:

# Change the password for user 'john'
sudo passwd john

# You will be prompted to enter and confirm the new password

🆔 id

Description: Display user identity information.

Example:

# Display information for user 'john'
id john

# Output includes UID, GID, and group memberships

🌐 7. Networking Commands

Manage and troubleshoot network configurations and connections.

Command Description Example Usage ðŸ’ŧ
ifconfig ðŸ•ļïļ Display or configure network interfaces (deprecated, use ip). bash<br>ifconfig
ip ðŸ“Ą Show or manipulate routing, devices, policy routing. bash<br>ip addr show
ping 🏓 Check the network connectivity to a host. bash<br>ping google.com
netstat 📊 Display network connections, routing tables, interface stats. bash<br>netstat -tuln
ss 🖧 Utility to investigate sockets. bash<br>ss -tuln
curl 🌐 Transfer data from or to a server. bash<br>curl -O http://example.com/file.txt
wget 🚀 Non-interactive network downloader. bash<br>wget http://example.com/file.txt
ftp 📁 Transfer files using the File Transfer Protocol. bash<br>ftp ftp.example.com
scp 🔒 Securely copy files between hosts. bash<br>scp file.txt user@remote:/path
traceroute ðŸ›Īïļ Trace network traffic routes. bash<br>traceroute google.com

🔍 Examples and Scripts

🏓 ping

Description: Check the network connectivity to a host.

Example:

# Ping Google's DNS server to check connectivity
ping 8.8.8.8

# Ping a domain with 4 packets
ping -c 4 google.com

ðŸ“Ą ip

Description: Show or manipulate routing, devices, policy routing.

Example:

# Display all IP addresses assigned to interfaces
ip addr show

# Bring down a network interface
sudo ip link set eth0 down

# Bring up a network interface
sudo ip link set eth0 up

🔒 scp

Description: Securely copy files between hosts.

Example:

# Copy a local file to a remote server
scp /path/to/local/file.txt user@remote:/path/to/destination/

# Copy a file from a remote server to local machine
scp user@remote:/path/to/remote/file.txt /path/to/local/destination/

ðŸ’― 8. Disk and Filesystem Management

Manage disks, partitions, and filesystems efficiently.

Command Description Example Usage ðŸ’ŧ
df ðŸ“Ķ File system disk space usage. bash<br>df -h
du 📁 File space usage. bash<br>du -sh /home/user
fdisk 🗄ïļ Display disk information and manipulate partitions. bash<br>sudo fdisk -l
mkfs 🛠ïļ Create a filesystem. bash<br>sudo mkfs.ext4 /dev/sdb1
mount 🔗 Mount a filesystem. bash<br>sudo mount /dev/sdb1 /mnt/data
umount 🚊 Unmount a filesystem. bash<br>sudo umount /mnt/data
fsck ðŸĐš Repair filesystems. bash<br>sudo fsck /dev/sdb1
lsblk 📋 List information about all available or the specified block devices. bash<br>lsblk
blkid 🏷ïļ Locate/print block device attributes. bash<br>sudo blkid
parted 🔧 Disk partitioning tool. bash<br>sudo parted /dev/sdb

🔍 Examples and Scripts

ðŸ“Ķ df

Description: File system disk space usage.

Example:

# Display disk space usage in human-readable format
df -h

# Show disk space usage for a specific directory
df -h /home/user

🛠ïļ mkfs

Description: Create a filesystem.

Example:

# Create an ext4 filesystem on /dev/sdb1
sudo mkfs.ext4 /dev/sdb1

# Create an XFS filesystem on /dev/sdb1
sudo mkfs.xfs /dev/sdb1

🔗 mount

Description: Mount a filesystem.

Example:

# Create a mount point directory
sudo mkdir /mnt/data

# Mount /dev/sdb1 to /mnt/data
sudo mount /dev/sdb1 /mnt/data

# Verify the mount
df -h | grep /mnt/data

ðŸ“Ķ 9. Package Management

Manage software packages on your system.

Command Description Example Usage ðŸ’ŧ
apt 🛒 Advanced Package Tool for Debian-based systems. bash<br>sudo apt update && sudo apt upgrade
yum ðŸ“Ķ Package manager for RPM-based systems (e.g., CentOS). bash<br>sudo yum install package-name
dnf 🐉 Next-generation package manager for RPM-based systems. bash<br>sudo dnf install package-name
rpm 📄 Manage RPM packages. bash<br>rpm -ivh package.rpm
dpkg ðŸ“Ķ Manage Debian packages. bash<br>sudo dpkg -i package.deb
snap ðŸ“Ķ🔗 Install and manage snap packages. bash<br>sudo snap install package-name
flatpak ðŸ“ĶðŸ–Ĩïļ Install and manage Flatpak packages. bash<br>sudo flatpak install flathub package-name
apt-get 🛒🔧 APT package handling utility (older commands). bash<br>sudo apt-get install package-name
apt-cache 🔍 Query the APT cache. bash<br>apt-cache search package-name
pip 🐍 Python package installer. bash<br>pip install package-name

🔍 Examples and Scripts

🛒 apt

Description: Advanced Package Tool for Debian-based systems.

Example:

# Update package lists
sudo apt update

# Upgrade all installed packages to their latest versions
sudo apt upgrade

# Install a new package (e.g., git)
sudo apt install git

# Remove a package
sudo apt remove git

ðŸ“Ķ yum

Description: Package manager for RPM-based systems (e.g., CentOS).

Example:

# Install a package (e.g., vim)
sudo yum install vim

# Remove a package
sudo yum remove vim

# Update all packages
sudo yum update

🐍 pip

Description: Python package installer.

Example:

# Install a Python package (e.g., requests)
pip install requests

# Upgrade a Python package
pip install --upgrade requests

# Uninstall a Python package
pip uninstall requests

Certainly! Let's continue building your Linux Command-Line Tutorial starting from 📜 10. Scripting and Automation. We'll maintain the use of emojis, structured tables, and practical script examples to enhance engagement and understanding.


📜 10. Scripting and Automation

Automating tasks with scripts can significantly enhance productivity and efficiency. This section covers essential commands and scripting techniques to help you create powerful automation scripts.

Command Description Example Usage ðŸ’ŧ
for 🔄 Loop through a list of items in scripts. bash<br>for i in {1..5}; do echo "Number $i"; done
while âģ Execute commands repeatedly based on a condition. bash<br>while [ $count -le 5 ]; do<br> echo "Count $count"<br> count=$((count + 1))<br>done
if ❓ Conditional statement to execute commands based on criteria. bash<br>if [ -f "file.txt" ]; then<br> echo "File exists"<br>fi
else ➡ïļ Specify alternative commands in conditional statements. bash<br>if [ -f "file.txt" ]; then<br> echo "File exists"<br>else<br> echo "File does not exist"<br>fi
elif 🔀 Else-if statement for multiple conditions. bash<br>if [ $age -lt 18 ]; then<br> echo "Minor"<br>elif [ $age -lt 65 ]; then<br> echo "Adult"<br>else<br> echo "Senior"<br>fi
case 🗂ïļ Execute commands based on pattern matching. bash<br>case "$option" in<br> 1) echo "Option 1 selected";;<br> 2) echo "Option 2 selected";;<br> *) echo "Invalid option";;<br>esac
function 🛠ïļ Define reusable functions in scripts. bash<br>function greet {<br> echo "Hello, $1!"<br>}<br>greet "Alice"
export ðŸ“Ī Set environment variables for child processes. bash<br>export PATH=$PATH:/new/path
cron ⏰ Schedule automated tasks using crontab. bash<br>crontab -e<br># Add the following line to run script.sh daily at midnight<br>0 0 * * * /path/to/script.sh
at 📅 Schedule one-time tasks to run at a specific time. bash<br>echo "/path/to/script.sh" \| at 02:00 PM tomorrow
&& 🔗 Execute the next command only if the previous one succeeds. bash<br>mkdir new_directory && cd new_directory
\|\| ❌ Execute the next command only if the previous one fails. bash<br>mkdir existing_directory \| echo "Directory already exists"
; ➕ Separate multiple commands on the same line. bash<br>cd /var/log; ls -l; pwd

🔍 Examples and Scripts

🔄 for Loop

Description: Iterate over a list of items and execute commands for each item.

Example:

#!/bin/bash
# Script to print numbers from 1 to 5

for i in {1..5}; do
  echo "Number $i"
done

Output:

Number 1
Number 2
Number 3
Number 4
Number 5

âģ while Loop

Description: Execute commands repeatedly as long as a condition is true.

Example:

#!/bin/bash
# Script to count from 1 to 5

count=1
while [ $count -le 5 ]; do
  echo "Count $count"
  count=$((count + 1))
done

Output:

Count 1
Count 2
Count 3
Count 4
Count 5

❓ if Statement

Description: Execute commands based on whether a condition is true.

Example:

#!/bin/bash
# Script to check if a file exists

filename="file.txt"

if [ -f "$filename" ]; then
  echo "File '$filename' exists."
fi

Output (if file exists):

File 'file.txt' exists.

➡ïļ else Statement

Description: Provide alternative commands if the if condition is false.

Example:

#!/bin/bash
# Script to check if a file exists

filename="file.txt"

if [ -f "$filename" ]; then
  echo "File '$filename' exists."
else
  echo "File '$filename' does not exist."
fi

Output (if file does not exist):

File 'file.txt' does not exist.

🗂ïļ case Statement

Description: Execute commands based on pattern matching, useful for handling multiple conditions.

Example:

#!/bin/bash
# Script to select an option

echo "Select an option:"
echo "1) Start"
echo "2) Stop"
echo "3) Restart"
read -p "Enter choice: " choice

case "$choice" in
  1)
    echo "Starting service..."
    ;;
  2)
    echo "Stopping service..."
    ;;
  3)
    echo "Restarting service..."
    ;;
  *)
    echo "Invalid option."
    ;;
esac

Output (if user selects 2):

Stopping service...

🛠ïļ function Definition

Description: Define reusable blocks of code within scripts.

Example:

#!/bin/bash
# Script with a function to greet users

function greet {
  echo "Hello, $1! Welcome to the Linux Command-Line Tutorial. 🚀"
}

greet "Alice"
greet "Bob"

Output:

Hello, Alice! Welcome to the Linux Command-Line Tutorial. 🚀
Hello, Bob! Welcome to the Linux Command-Line Tutorial. 🚀

ðŸ“Ī export Environment Variable

Description: Set environment variables that are available to child processes.

Example:

#!/bin/bash
# Script to set and use an environment variable

export PROJECT_DIR="/home/user/projects"

echo "Project directory is set to $PROJECT_DIR"

# Using the environment variable in a command
cd "$PROJECT_DIR" && ls -l

Output:

Project directory is set to /home/user/projects
(total list of files and directories in /home/user/projects)

⏰ cron Scheduling with crontab

Description: Schedule scripts or commands to run automatically at specified times.

Example:

# Open the crontab editor
crontab -e

# Add the following line to run backup.sh every day at midnight
0 0 * * * /home/user/scripts/backup.sh

Explanation:

  • 0 0 * * * specifies the time (midnight every day).
  • /home/user/scripts/backup.sh is the script to be executed.

📅 at One-Time Task Scheduling

Description: Schedule a command or script to run once at a specific time.

Example:

# Schedule a script to run at 2:00 PM tomorrow
echo "/home/user/scripts/report.sh" | at 2:00 PM tomorrow

Explanation:

  • echo "/home/user/scripts/report.sh" sends the command to at.
  • at 2:00 PM tomorrow schedules the command for the specified time.

🔗 && Logical AND Operator

Description: Execute the next command only if the previous command succeeds.

Example:

#!/bin/bash
# Script to create a directory and navigate into it

mkdir new_project && cd new_project
echo "Successfully created and moved into new_project directory. 📁"

Output:

Successfully created and moved into new_project directory. 📁

❌ || Logical OR Operator

Description: Execute the next command only if the previous command fails.

Example:

#!/bin/bash
# Script to create a directory or notify if it already exists

mkdir existing_directory || echo "Directory already exists. ❗"

Output (if directory exists):

Directory already exists. ❗

➕ ; Command Separator

Description: Execute multiple commands sequentially, regardless of the success of previous commands.

Example:

#!/bin/bash
# Script to update the system and clean up

sudo apt update; sudo apt upgrade -y; sudo apt autoremove -y
echo "System updated and cleaned up successfully! 🛠ïļ"

Output:

System updated and cleaned up successfully! 🛠ïļ

📝 Best Practices for Scripting

  1. Use Comments 📝: Always comment your scripts to explain what each part does.
# This script backs up the home directory
tar -czf backup.tar.gz /home/user/
  1. Set Executable Permissions 🔒: Ensure your script has the right permissions.
chmod +x backup.sh
  1. Error Handling ⚠ïļ: Handle potential errors gracefully.
if cp source.txt destination.txt; then
  echo "Copy succeeded. ✅"
else
  echo "Copy failed. ❌"
fi
  1. Use Functions for Reusability 🔄: Break your script into functions for better organization.
function backup {
  tar -czf backup.tar.gz /home/user/
}

backup
  1. Validate Inputs 🧐: Check user inputs or script arguments.
    if [ -z "$1" ]; then
      echo "Usage: $0 filename"
      exit 1
    fi
    

ðŸ›Ąïļ 11. Security Commands

Security is paramount in system administration. These commands help you manage permissions, firewall settings, and system security.

Command Description Example Usage ðŸ’ŧ
chmod 🔒 Change file permissions. bash<br>chmod 755 script.sh
chown 👑 Change file ownership. bash<br>chown user:group file.txt
chgrp ðŸ‘Ĩ Change group ownership of a file. bash<br>chgrp developers project/
sudo ðŸĶļ Execute a command as another user (usually root). bash<br>sudo apt update
ufw ðŸ›Ąïļ Uncomplicated Firewall - manage firewall rules. bash<br>sudo ufw enable
firewall-cmd ðŸ”Ĩ Command line interface for firewalld. bash<br>sudo firewall-cmd --add-port=80/tcp --permanent
selinux ðŸ›Ąïļ Manage SELinux policies and status. bash<br>sestatus
fail2ban ðŸšŦ Protect against brute-force attacks. bash<br>sudo fail2ban-client status
iptables ðŸŠĪ Configure network packet filtering and NAT. bash<br>sudo iptables -L
ssh 🔑 Secure Shell for secure remote access. bash<br>ssh user@remote_host
openssl 🔐 Toolkit for SSL/TLS and cryptographic operations. bash<br>openssl req -new -x509 -days 365 -nodes -out server.crt -keyout server.key
gpg 🔑 GNU Privacy Guard for encryption and signing. bash<br>gpg --gen-key

🔍 Examples and Scripts

🔒 chmod - Change File Permissions

Description: Modify the read, write, and execute permissions of files and directories.

Example:

#!/bin/bash
# Script to set executable permissions for a script

chmod 755 deploy.sh
echo "Set execute permissions for deploy.sh ✅"

Explanation:

  • 755 sets read, write, and execute permissions for the owner, and read and execute permissions for the group and others.

👑 chown - Change File Ownership

Description: Change the owner and group of a file or directory.

Example:

#!/bin/bash
# Script to change ownership of a directory

chown alice:developers /home/alice/project/
echo "Changed ownership of /home/alice/project/ to alice:developers ðŸ‘Ĩ"

Explanation:

  • Changes the owner to alice and the group to developers.

ðŸĶļ sudo - Execute Commands with Elevated Privileges

Description: Run commands with the security privileges of another user, typically the superuser.

Example:

#!/bin/bash
# Script to update and upgrade the system

sudo apt update && sudo apt upgrade -y
echo "System updated and upgraded successfully! 🚀"

Explanation:

  • sudo apt update updates the package list.
  • sudo apt upgrade -y upgrades all installed packages without prompting for confirmation.

ðŸ›Ąïļ ufw - Uncomplicated Firewall

Description: Manage firewall rules easily.

Example:

#!/bin/bash
# Script to enable UFW and allow SSH

sudo ufw enable
sudo ufw allow ssh
sudo ufw status
echo "Firewall enabled and SSH allowed. 🔒"

Output:

Firewall is active and enabled on system startup
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Firewall enabled and SSH allowed. 🔒

🔑 ssh - Secure Shell

Description: Connect securely to a remote server.

Example:

#!/bin/bash
# Script to connect to a remote server

echo "Connecting to remote server..."
ssh user@192.168.1.100

Usage:

./connect.sh

Output:

Connecting to remote server...
user@192.168.1.100's password:

🔐 openssl - SSL/TLS Toolkit

Description: Generate SSL certificates and manage cryptographic operations.

Example:

#!/bin/bash
# Script to generate a self-signed SSL certificate

openssl req -new -x509 -days 365 -nodes -out server.crt -keyout server.key
echo "Self-signed SSL certificate generated. 🔐"

Explanation:

  • Generates a new self-signed certificate valid for 365 days.

ðŸŠĪ iptables - Network Packet Filtering

Description: Configure the Linux kernel firewall.

Example:

#!/bin/bash
# Script to allow HTTP and HTTPS traffic

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -L
echo "Allowed HTTP and HTTPS traffic through iptables. ðŸŠĪ"

Output:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
...
Allowed HTTP and HTTPS traffic through iptables. ðŸŠĪ

Certainly! Let's continue building your Linux Command-Line Tutorial starting from 📊 12. System Information and Monitoring. We'll maintain the use of emojis, structured tables, and practical script examples to enhance engagement and understanding.


📊 12. System Information and Monitoring

Monitoring your system's performance and obtaining detailed information is crucial for maintaining a healthy and efficient Linux environment. This section covers essential commands that provide insights into system resources, hardware details, and overall performance.

Command Description Example Usage ðŸ’ŧ
top 📈 Display real-time system processes and resource usage. bash<br>top
htop ðŸ–Ĩïļ Interactive process viewer (requires installation). bash<br>htop
vmstat 📊 Report virtual memory statistics. bash<br>vmstat 5
iostat ðŸ§Ū Display CPU and I/O statistics for devices and partitions. bash<br>iostat -xz 1
free ðŸ’ū Show memory and swap usage. bash<br>free -h
df ðŸ“Ķ Report file system disk space usage. bash<br>df -h
du 📂 Estimate file and directory space usage. bash<br>du -sh /home/user
uname ðŸ–ēïļ Display system information. bash<br>uname -a
lscpu 🧠 Display CPU architecture information. bash<br>lscpu
lsblk 📀 List information about block devices. bash<br>lsblk
lshw 🛠ïļ List detailed hardware configuration. bash<br>sudo lshw
dmidecode 🔍 Dump system's DMI (hardware) table contents. bash<br>sudo dmidecode
dmesg 🌀 Print kernel ring buffer messages (system messages). bash<br>dmesg \| less
free ðŸ’ū Display memory usage. bash<br>free -m
uptime ⏰ Show how long the system has been running and load averages. bash<br>uptime
sar 📈 Collect, report, or save system activity information. bash<br>sar -u 1 3
mpstat 📊 Report processors related statistics. bash<br>mpstat -P ALL 1
netstat 🌐 Display network connections, routing tables, interface stats. bash<br>netstat -tuln
ss ⚡ Utility to investigate sockets. bash<br>ss -tuln
watch 👀 Execute a program periodically, showing output fullscreen. bash<br>watch -n 2 df -h
perf 🚀 Performance analysis tools for Linux. bash<br>perf top
systemctl 🔧 Control the systemd system and service manager. bash<br>systemctl status sshd

🔍 Examples and Scripts

📈 top

Description: Display real-time system processes and resource usage.

Example:

# Start the top command to monitor processes
top

# Within top, you can:
# - Press 'q' to exit
# - Press 'M' to sort by memory usage
# - Press 'P' to sort by CPU usage

ðŸ–Ĩïļ htop

Description: An interactive process viewer with a more user-friendly interface than top. Requires installation.

Installation:

# On Debian/Ubuntu
sudo apt-get install htop

# On CentOS/RHEL
sudo yum install htop

# On Fedora
sudo dnf install htop

Usage:

# Start htop
htop

# Within htop, you can:
# - Use arrow keys to navigate
# - Press 'F6' to sort
# - Press 'F9' to kill a process
# - Press 'F10' to exit

ðŸ’ū free

Description: Display the amount of free and used memory in the system.

Example:

# Display memory usage in human-readable format
free -h

# Sample Output:
              total        used        free      shared  buff/cache   available
Mem:           15Gi       3.2Gi       8.5Gi       1.1Gi       3.8Gi        10Gi
Swap:         2.0Gi          0B       2.0Gi

ðŸ“Ķ df

Description: Report file system disk space usage.

Example:

# Display disk usage in human-readable format
df -h

# Sample Output:
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   20G   28G  42% /
tmpfs           2.0G     0  2.0G   0% /dev/shm

📀 lsblk

Description: List information about block devices.

Example:

# List all block devices in a tree format
lsblk

# Sample Output:
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0    50G  0 disk
├─sda1   8:1    0    50G  0 part /
sdb      8:16   0    10G  0 disk
└─sdb1   8:17   0    10G  0 part /mnt/data

🌀 dmesg

Description: Print the kernel ring buffer messages, useful for diagnosing hardware and driver issues.

Example:

# Display kernel messages with paging
dmesg | less

# Search for specific keywords, e.g., "error"
dmesg | grep -i "error"

⏰ uptime

Description: Show how long the system has been running along with the load averages.

Example:

# Display uptime and load averages
uptime

# Sample Output:
 10:15:30 up 5 days,  4:22,  3 users,  load average: 0.15, 0.20, 0.18

🌐 netstat

Description: Display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

Example:

# Display all listening ports and associated programs
netstat -tuln

# Sample Output:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN

Note: netstat is deprecated in favor of ss.


⚡ ss

Description: Utility to investigate sockets, providing similar functionality to netstat with more features.

Example:

# Display all listening TCP ports
ss -tuln

# Sample Output:
State    Recv-Q    Send-Q        Local Address:Port          Peer Address:Port
LISTEN   0         128                   *:22                       *:*
LISTEN   0         128                   *:80                       *:*

👀 watch

Description: Execute a program periodically, showing output fullscreen. Useful for monitoring changes over time.

Example:

# Watch the disk usage every 2 seconds
watch -n 2 df -h

# Watch the list of running processes
watch -n 5 'ps aux --sort=-%mem | head'

🔧 systemctl

Description: Control the systemd system and service manager. Used to manage system services and the system state.

Example:

# Check the status of the SSH service
systemctl status sshd

# Start a service
sudo systemctl start apache2

# Enable a service to start on boot
sudo systemctl enable apache2

# Restart a service
sudo systemctl restart apache2

# Stop a service
sudo systemctl stop apache2

ðŸ§Ū iostat

Description: Display CPU and I/O statistics for devices and partitions, useful for identifying bottlenecks.

Example:

# Display extended statistics every second
iostat -xz 1

# Sample Output:
Linux 5.4.0-42-generic (hostname)   04/27/2024  _x86_64_  (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.25    0.00    0.75    0.50    0.00   97.50

Device             r/s     w/s     rkB/s   wkB/s  rrqm/s  wrqm/s  %rrqm  %wrqm  r_await  w_await  aqu-sz  rareq-sz  wareq-sz  svctm  %util
sda               0.50    1.00    10.00    20.00     0.00    0.00    0.00    0.00      2.00      3.00     0.00      20.00      20.00    1.50   0.10

📊 vmstat

Description: Report virtual memory statistics, including processes, memory, paging, block I/O, traps, and CPU activity.

Example:

# Display vmstat output every 5 seconds
vmstat 5

# Sample Output:
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 812356  12345 678901    0    0     1     2    3    4  5  1 94  0  0

📈 sar

Description: Collect, report, or save system activity information. Useful for historical data analysis.

Example:

# Display CPU usage every 1 second for 3 times
sar -u 1 3

# Sample Output:
Linux 5.4.0-42-generic (hostname)    04/27/2024      _x86_64_        (4 CPU)

04:00:01 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:00:02 PM     all      1.25      0.00      0.75        0.50      0.00     97.50
04:00:03 PM     all      1.50      0.00      0.75        0.75      0.00     96.00
04:00:04 PM     all      1.75      0.00      1.00        1.00      0.00     96.25
Average:        all      1.50      0.00      0.83        0.75      0.00     96.92

Note: To enable data collection, ensure that the sysstat package is installed and the sar service is running.


🚀 perf

Description: Performance analysis tools for Linux, used to profile applications and system performance.

Example:

# Install perf (if not already installed)
sudo apt-get install linux-tools-common linux-tools-generic

# Start perf to monitor CPU usage in real-time
perf top

# Sample Output:
# Displays real-time CPU usage by functions

Note: perf requires root privileges for certain operations.


🛠ïļ Practical Script Example: Monitoring Disk Usage and Alerting

Description: This script monitors disk usage and sends an alert if usage exceeds a specified threshold.

Script:

#!/bin/bash
# disk_monitor.sh - Monitor disk usage and send an alert

# Set the threshold percentage (e.g., 80%)
THRESHOLD=80

# Get the current disk usage for root filesystem
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

echo "Current disk usage is ${USAGE}%"

# Check if usage is greater than or equal to threshold
if [ "$USAGE" -ge "$THRESHOLD" ]; then
  echo "Disk usage is above ${THRESHOLD}%. Sending alert..."
  # Example: Send an email (requires mailutils installed and configured)
  echo "Disk usage is at ${USAGE}% on $(hostname)" | mail -s "Disk Usage Alert" admin@example.com
else
  echo "Disk usage is under control."
fi

Usage:

# Make the script executable
chmod +x disk_monitor.sh

# Run the script
./disk_monitor.sh

Scheduling with Cron:

# Open crontab editor
crontab -e

# Add the following line to run the script daily at 8 AM
0 8 * * * /path/to/disk_monitor.sh

Output:

Current disk usage is 85%
Disk usage is above 80%. Sending alert...

Email Alert:

Subject: Disk Usage Alert

Disk usage is at 85% on your-server-name

Certainly! Let's continue building your Linux Command-Line Tutorial starting from ðŸ›Ąïļ 13. Security Commands. We'll maintain the use of emojis, structured tables, and practical script examples to enhance engagement and understanding.


ðŸ›Ąïļ 13. Security Commands

Ensuring the security of your Linux system is paramount. This section covers essential commands for managing permissions, ownership, firewalls, and other security-related tasks.

Command Description Example Usage ðŸ’ŧ
chmod 🔒 Change file or directory permissions. bash<br>chmod 755 script.sh
chown 👑 Change file or directory ownership. bash<br>chown user:group file.txt
chgrp ðŸ‘Ĩ Change the group ownership of a file or directory. bash<br>chgrp developers project/
sudo 🛠ïļ Execute a command as another user, typically root. bash<br>sudo apt-get update
firewall-cmd ðŸ”Ĩ Manage the firewall using firewalld. bash<br>sudo firewall-cmd --add-port=80/tcp --permanent<br>sudo firewall-cmd --reload
firewall-config ðŸ›Ąïļ GUI tool for managing the firewall. bash<br>sudo firewall-config
sestatus 📝 Check the status of SELinux. bash<br>sestatus
semanage 🔧 Configure SELinux policy components. bash<br>sudo semanage port -a -t http_port_t -p tcp 8080
iptables 🛑 Configure IPv4/IPv6 packet filtering and NAT rules. bash<br>sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
fail2ban ðŸšŦ🔒 Protect against brute-force attacks by banning suspicious IPs. bash<br>sudo fail2ban-client status
ssh 🔑 Secure Shell for remote login and command execution. bash<br>ssh user@remote_host
tcpwrappers ðŸ“Ķ Host-based access control lists for network services. bash<br>sudo nano /etc/hosts.allow
gpg 🗝ïļ Encrypt and sign your data and communications. bash<br>gpg --encrypt --recipient user@example.com file.txt
openssl 🛠ïļ Toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. bash<br>openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout key.pem
auditctl ðŸ•ĩïļâ€â™‚ïļ Control the behavior of the audit daemon. bash<br>sudo auditctl -w /etc/passwd -p wa -k passwd_changes
passwd 🔑 Change a user's password. bash<br>passwd username
usermod ðŸ›Ąïļ Modify user account properties, including security settings. bash<br>sudo usermod -L username

🔍 Examples and Scripts

🔒 chmod - Change File Permissions

Description: Modify the read, write, and execute permissions of files and directories to control access.

Example:

# Grant read, write, and execute permissions to the owner,
# and read and execute permissions to the group and others
chmod 755 script.sh

# Explanation:
# 7 (owner) = 4 (read) + 2 (write) + 1 (execute)
# 5 (group) = 4 (read) + 1 (execute)
# 5 (others) = 4 (read) + 1 (execute)

Usage Scenario:

# Make a script executable
chmod +x backup.sh

# Remove write permissions for others
chmod o-w confidential.txt

👑 chown - Change File Ownership

Description: Change the owner and/or group of a file or directory to manage access permissions.

Example:

# Change the owner to 'alice' and the group to 'developers' for project directory
chown alice:developers project/

Usage Scenario:

# Change ownership of a file to root
sudo chown root:root /etc/ssh/sshd_config

🛠ïļ sudo - Execute Commands with Elevated Privileges

Description: Run commands with the security privileges of another user, typically the superuser (root).

Example:

# Update the package list with root privileges
sudo apt-get update

Usage Scenario:

# Install a package using sudo
sudo yum install nginx

ðŸ”Ĩ firewall-cmd - Manage Firewall Rules

Description: Configure the firewall dynamically with firewalld, allowing you to add or remove rules without restarting the firewall.

Example:

# Allow HTTP traffic on port 80 permanently
sudo firewall-cmd --permanent --add-service=http

# Reload the firewall to apply changes
sudo firewall-cmd --reload

Usage Scenario:

# Open port 443 for HTTPS
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload

📝 sestatus - Check SELinux Status

Description: Display the current status of SELinux, including its mode and policies.

Example:

# Check SELinux status
sestatus

# Sample Output:
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing

Usage Scenario:

# Set SELinux to permissive mode temporarily
sudo setenforce 0

# Check the updated status
sestatus

🛑 iptables - Configure Firewall Rules

Description: Set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel.

Example:

# Allow incoming SSH connections on port 22
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Save the iptables rules
sudo iptables-save | sudo tee /etc/iptables/rules.v4

Usage Scenario:

# Block all incoming traffic from a specific IP
sudo iptables -A INPUT -s 192.168.1.100 -j DROP

🔑 ssh - Secure Shell for Remote Access

Description: Securely connect to remote servers and execute commands over an encrypted connection.

Example:

# Connect to a remote server as user 'bob'
ssh bob@remote_server_ip

# Using a specific SSH key for authentication
ssh -i ~/.ssh/id_rsa bob@remote_server_ip

Usage Scenario:

# Execute a remote command without logging in interactively
ssh user@remote_host 'ls -la /var/www'

ðŸ•ĩïļâ€â™‚ïļ auditctl - Control the Audit Daemon

Description: Configure the audit subsystem, allowing you to track security-relevant events.

Example:

# Watch for changes to the /etc/passwd file
sudo auditctl -w /etc/passwd -p wa -k passwd_changes

# View audit logs
sudo ausearch -k passwd_changes

Usage Scenario:

# Monitor all executions of the 'passwd' command
sudo auditctl -a exit,always -F arch=b64 -S execve -F path=/usr/bin/passwd -k passwd_exec

🛠ïļ 14. Miscellaneous Commands

This section covers various commands that don't fit neatly into other categories but are still essential for daily Linux operations.

Command Description Example Usage ðŸ’ŧ
wget 🌐 Non-interactive network downloader. bash<br>wget https://example.com/file.zip
curl ðŸ“Ą Transfer data from or to a server. bash<br>curl -O https://example.com/file.zip
tar ðŸ“Ķ Archive multiple files into a single file. bash<br>tar -czvf archive.tar.gz /path/to/directory
gzip 🗜ïļ Compress files using the GNU zip algorithm. bash<br>gzip file.txt
gunzip 🔓 Decompress files compressed by gzip. bash<br>gunzip file.txt.gz
zip 🛠ïļ Package and compress files. bash<br>zip archive.zip file1 file2
unzip ðŸ“Ĩ Extract compressed ZIP archives. bash<br>unzip archive.zip
find 🔍 Search for files and directories based on criteria. bash<br>find /home/user -name "*.txt"
locate 📍 Quickly find files by name using a prebuilt database. bash<br>locate file.txt
which 📌 Show the full path of (shell) commands. bash<br>which python3
man 📖 Display the manual for each command. bash<br>man ls
whatis ❓ Short description of a command. bash<br>whatis grep
which 🔍 Show the full path of shell commands. bash<br>which bash
history 📜 Show the history of executed commands. bash<br>history
alias 🏷ïļ Create shortcuts for longer commands. bash<br>alias ll='ls -la'

🔍 Examples and Scripts

🌐 wget - Download Files from the Internet

Description: Retrieve files from the web using HTTP, HTTPS, or FTP protocols.

Example:

# Download a single file
wget https://example.com/file.zip

# Download multiple files listed in a text file
wget -i download_list.txt

# Resume a partially downloaded file
wget -c https://example.com/largefile.iso

Usage Scenario:

# Download a website recursively for offline viewing
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent https://example.com

ðŸ“Ą curl - Transfer Data from or to a Server

Description: Perform data transfers with URLs, supporting various protocols and options.

Example:

# Download a file and save it with the same name
curl -O https://example.com/file.zip

# Fetch the headers of a URL
curl -I https://example.com

# Send a POST request with JSON data
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/data

Usage Scenario:

# Upload a file using FTP
curl -T upload.txt ftp://ftp.example.com/ --user username:password

ðŸ“Ķ tar - Archive Files

Description: Combine multiple files into a single archive file, optionally compressing it.

Example:

# Create a compressed archive
tar -czvf archive.tar.gz /path/to/directory

# Extract a compressed archive
tar -xzvf archive.tar.gz

# List contents of an archive
tar -tzvf archive.tar.gz

Usage Scenario:

# Backup home directory excluding the Downloads folder
tar --exclude='/home/user/Downloads' -czvf home_backup.tar.gz /home/user

🔍 find - Search for Files and Directories

Description: Locate files and directories based on name, size, modification date, and other criteria.

Example:

# Find all .log files in /var/log
find /var/log -type f -name "*.log"

# Find directories named 'backup'
find / -type d -name "backup"

# Find files larger than 100MB
find /home/user -type f -size +100M

Usage Scenario:

# Find and delete all .tmp files older than 7 days
find /tmp -type f -name "*.tmp" -mtime +7 -exec rm {} \;

📜 history - Command History

Description: Display or manipulate the history of commands entered in the shell.

Example:

# Show the last 20 commands
history 20

# Re-execute the last command
!!

# Re-execute a specific command from history using its number
!100

Usage Scenario:

# Search the history for commands containing 'ssh'
history | grep ssh

📚 15. Appendix: Complete Command Reference

For a comprehensive list of all Linux commands covered in this tutorial, refer to the appendix below. This section serves as a quick reference guide to help you find commands and their descriptions efficiently.

No. Command Description
1 alias 📛 Create a short name for a long command.
2 arch ðŸ–Ĩïļ Show system architecture (32 or 64-bit).
3 at 📅 Schedule ad-hoc jobs.
4 awk 🛠ïļ Process and analyze text files by fields.
5 bash 🐚 Start a new shell session.
6 bc ➗ Perform arithmetic operations.
7 bg 🌙 Run a process in the background.
8 cal 📆 Display the calendar.
9 case 🗂ïļ Provide options in scripts based on conditions.
10 cat 📄 Read and concatenate files.
11 cd 📂 Change the current directory.
12 chage 📅 Modify user attributes like password expiration.
... ... ...
153 yum 🛠ïļ Download, install, and update packages.

Note: This is a partial list. Refer to the detailed sections above for comprehensive explanations and examples.


🎓 Conclusion

Mastering these Linux commands will empower you to efficiently manage and secure your Linux systems. Practice using these commands regularly, and refer back to this tutorial as needed to reinforce your understanding and skills.