ð§ Linux Command-Line Tutorial
ð Table of Contents
- Introduction
- Basic System Commands
- File and Directory Management
- File Viewing and Editing
- Process Management
- User and Group Management
- Networking Commands
- Disk and Filesystem Management
- Package Management
- Scripting and Automation
- Security Commands
- System Information and Monitoring
- Miscellaneous Commands
- 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:
ðĒ echo
Description: Display a line of text/string on the screen.
Example:
ð 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:
â°ïļ 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:
ð 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:
Output:
âģ 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:
â 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):
âĄïļ 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):
ðïļ 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):
ð ïļ 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 toat
.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:
â ||
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):
â ;
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:
ð Best Practices for Scripting
- Use Comments ð: Always comment your scripts to explain what each part does.
- Set Executable Permissions ð: Ensure your script has the right permissions.
- Error Handling â ïļ: Handle potential errors gracefully.
- Use Functions for Reusability ð: Break your script into functions for better organization.
- Validate Inputs ð§: Check user inputs or script arguments.
ðĄïļ 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 todevelopers
.
ðĶļ 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:
Output:
ð 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:
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:
Email Alert:
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:
ð ïļ sudo
- Execute Commands with Elevated Privileges
Description: Run commands with the security privileges of another user, typically the superuser (root).
Example:
Usage Scenario:
ðĨ 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:
ð 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:
ð 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:
ðĩïļââïļ 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:
ðĶ 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:
ð 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.