
AI Computing
Lesson Content
Linux Command Line Fundamentals
Introduction
Linux command line proficiency is essential for network administrators. This guide covers essential commands for system and network management.
Basic Navigation
File System Navigation
code# Print working directory pwd # List files and directories ls ls -la # Detailed list with hidden files ls -lh # Human-readable sizes # Change directory cd /etc/network cd .. # Go up one level cd ~ # Go to home directory cd - # Go to previous directory # Create directory mkdir mydir mkdir -p parent/child/grandchild # Create nested directories
File Operations
code# Create empty file touch file.txt # Copy files cp source.txt destination.txt cp -r /source/dir /dest/dir # Copy directory recursively # Move/rename files mv oldname.txt newname.txt mv file.txt /new/location/ # Remove files rm file.txt rm -rf directory/ # Remove directory and contents # View file contents cat file.txt # Display entire file more file.txt # Page through file less file.txt # Better paging (can scroll back) head -n 20 file.txt # First 20 lines tail -n 50 file.txt # Last 50 lines tail -f /var/log/syslog # Follow file in real-time
Network Commands
Network Configuration
code# View network interfaces ifconfig ip addr show ip a # Short form # Configure IP address sudo ip addr add 192.168.1.100/24 dev eth0 sudo ip addr del 192.168.1.100/24 dev eth0 # Enable/disable interface sudo ip link set eth0 up sudo ip link set eth0 down # View routing table route -n ip route show netstat -rn # Add route sudo ip route add 10.0.0.0/24 via 192.168.1.1 # Set default gateway sudo ip route add default via 192.168.1.1
Network Diagnostics
code# Ping ping 8.8.8.8 ping -c 5 google.com # Send 5 packets only # Traceroute traceroute google.com tracepath google.com # Alternative without root # DNS lookup nslookup google.com dig google.com dig +short google.com # Brief output host google.com # Network statistics netstat -tuln # Show listening ports netstat -an # Show all connections ss -tuln # Modern alternative to netstat ss -s # Summary statistics # Port scanning nmap 192.168.1.1 nmap -p 1-1000 192.168.1.1 # Scan ports 1-1000 # Test port connectivity telnet 192.168.1.1 80 nc -zv 192.168.1.1 22 # Test SSH port # Download files wget http://example.com/file.txt curl -O http://example.com/file.txt # Transfer files scp file.txt user@192.168.1.1:/remote/path/ rsync -avz /local/dir/ user@remote:/remote/dir/
Firewall Management
code# UFW (Uncomplicated Firewall) sudo ufw status sudo ufw enable sudo ufw disable sudo ufw allow 22/tcp sudo ufw deny 23/tcp sudo ufw delete allow 80/tcp sudo ufw allow from 192.168.1.0/24 # iptables sudo iptables -L # List rules sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -j DROP # Drop all other incoming sudo iptables-save > /etc/iptables/rules.v4
System Administration
Process Management
code# View processes ps aux # All processes ps aux | grep nginx # Find specific process top # Real-time process monitoring htop # Better interactive top # Kill processes kill 1234 # Send SIGTERM to PID 1234 kill -9 1234 # Force kill (SIGKILL) killall nginx # Kill all nginx processes pkill -f "python script.py" # Kill by pattern # Background processes command & # Run in background jobs # List background jobs fg %1 # Bring job 1 to foreground bg %1 # Resume job 1 in background # Run command with priority nice -n 10 command # Lower priority renice -n -5 -p 1234 # Change priority of PID 1234
User Management
code# User information whoami # Current user id # User ID and groups w # Who is logged in last # Login history # Add user sudo useradd -m -s /bin/bash username sudo passwd username # Modify user sudo usermod -aG sudo username # Add to sudo group sudo usermod -L username # Lock account sudo usermod -U username # Unlock account # Delete user sudo userdel username sudo userdel -r username # Delete with home directory # Groups sudo groupadd groupname sudo groupdel groupname sudo gpasswd -a username groupname # Add user to group
Permissions
code# View permissions ls -l file.txt # Change permissions (symbolic) chmod u+x file.sh # Add execute for user chmod go-w file.txt # Remove write for group/others chmod a+r file.txt # Add read for all # Change permissions (numeric) chmod 755 file.sh # rwxr-xr-x chmod 644 file.txt # rw-r--r-- chmod 600 private.key # rw------- # Change ownership sudo chown user:group file.txt sudo chown -R user:group directory/ # Special permissions chmod +t /tmp # Sticky bit chmod u+s program # SUID bit chmod g+s directory # SGID bit
Package Management
Debian/Ubuntu (APT)
code# Update package list sudo apt update # Upgrade packages sudo apt upgrade sudo apt full-upgrade # Install package sudo apt install nginx sudo apt install package1 package2 # Remove package sudo apt remove nginx sudo apt purge nginx # Remove with config files sudo apt autoremove # Remove unused dependencies # Search packages apt search keyword apt show package-name # List installed packages apt list --installed dpkg -l
Red Hat/CentOS (YUM/DNF)
code# Update package list sudo yum check-update # Install package sudo yum install nginx sudo dnf install nginx # Fedora/RHEL 8+ # Remove package sudo yum remove nginx # Search packages yum search keyword # List installed yum list installed
System Monitoring
Resource Usage
code# Disk usage df -h # Disk space du -sh /var/log # Directory size du -h --max-depth=1 /var # Subdirectory sizes # Memory usage free -h vmstat 5 # Updates every 5 seconds # CPU information lscpu cat /proc/cpuinfo # System load uptime w
Logs
code# View system logs sudo journalctl sudo journalctl -u nginx.service # Service logs sudo journalctl -f # Follow logs sudo journalctl --since "1 hour ago" # Traditional log files sudo tail -f /var/log/syslog sudo tail -f /var/log/auth.log sudo less /var/log/nginx/access.log # Search logs sudo grep "error" /var/log/syslog sudo grep -r "Failed password" /var/log/
Text Processing
Grep (Search)
code# Search in file grep "pattern" file.txt grep -i "pattern" file.txt # Case insensitive grep -r "pattern" /path/ # Recursive search grep -v "pattern" file.txt # Invert match (exclude) grep -n "pattern" file.txt # Show line numbers grep -c "pattern" file.txt # Count matches
Sed (Stream Editor)
code# Replace text sed 's/old/new/' file.txt sed 's/old/new/g' file.txt # Replace all occurrences sed -i 's/old/new/g' file.txt # Edit file in-place # Delete lines sed '/pattern/d' file.txt sed '1,5d' file.txt # Delete lines 1-5
Awk (Text Processing)
code# Print specific columns awk '{print $1}' file.txt # First column awk '{print $1,$3}' file.txt # Columns 1 and 3 # Filter and print awk '$3 > 100' file.txt # Lines where column 3 > 100 netstat -an | awk '/ESTABLISHED/ {print $5}'
Scripting Basics
Simple Bash Script
code#!/bin/bash # Variables NAME="John" echo "Hello, $NAME" # User input read -p "Enter your name: " USERNAME echo "Welcome, $USERNAME" # Conditionals if [ -f "/etc/passwd" ]; then echo "File exists" fi # Loops for i in {1..5}; do echo "Number: $i" done for file in *.txt; do echo "Processing $file" done # Functions greet() { echo "Hello, $1" } greet "World"
Best Practices
- Use Tab Completion: Press Tab to auto-complete
- Command History: Use Up/Down arrows or code
history - Reverse Search: Ctrl+R to search command history
- Clear Screen: Ctrl+L or commandcode
clear - Cancel Command: Ctrl+C
- Logout: Ctrl+D or code
exit - Use Carefully: Understand what commands docode
sudo - Regular Backups: Before making system changes
- Check Before Deleting: Be careful with code
rm -rf - Read Man Pages: for documentationcode
man command