Understanding Linux Commands: Beginner to Advance

Abang LazAbang Laz
34 min read

Stage 1: Beginner Level

  1. Introduction to Linux:

    • What is Linux and how it works.

    • Common Linux distributions.

  2. Basic Linux Commands:

    • Navigating the filesystem.

    • Managing files and directories.

    • Viewing and editing files.

Stage 2: Intermediate Level

  1. Users, Groups, and Permissions:

    • User management.

    • File permissions and ownership.

  2. Process Management:

    • Understanding system processes.

    • Managing and monitoring processes.

Stage 3: Advanced Level

  1. Networking:

    • Networking basics and commands.

    • Configuring network interfaces and services.

  2. Shell Scripting:

    • Automating tasks with Bash scripts.
  3. System Administration:

    • Installing and managing software.

    • Disk management and backups.

Let’s get started with Stage 1.

1. Introduction to Linux

  • What is Linux? Linux is an open-source operating system that manages hardware resources and provides services for applications. It's used widely in servers, embedded devices, and desktops.

  • Distributions: Linux comes in many flavors called distributions or "distros." Popular ones include:

    • Ubuntu (beginner-friendly)

    • Fedora (for developers)

    • CentOS (for servers)

    • Kali Linux (for security professionals)

Which Linux distribution are you using, or would you like help choosing one?

2. Basic Linux Commands

Let's dive into some essential commands. Open your terminal and try these:

  • pwd: Prints the current working directory.

  • ls: Lists files and directories.

  • cd [directory]: Changes to the specified directory.

  • cp [source] [destination]: Copies files or directories.

  • mv [source] [destination]: Moves or renames files.

  • rm [file]: Deletes a file.

  • man [command]: Shows the manual for a command (e.g., man ls).

Task:

Run the following commands to get familiar with navigating the file system:

pwd
ls
cd /home
ls
cd ~

3. Users, Groups, and Permissions

Understanding Permissions

In Linux, every file and directory has associated permissions that define who can read, write, or execute it. Permissions are set for three categories:

  • Owner: The person who owns the file.

  • Group: A set of users who share access rights to the file.

  • Others: Everyone else.

Run this command to see the permissions of files in your current directory:

ls -l

The output looks something like this:

-rwxr-xr--

Here's what each part means:

  • -rwxr-xr-- is broken down as:

    • -: The first character indicates the type (- for file, d for directory).

    • rwx: Permissions for the owner (read, write, execute).

    • r-x: Permissions for the group (read, no write, execute).

    • r--: Permissions for others (read only).

Changing Permissions

You can modify file permissions using the chmod command:

chmod [permissions] [filename]

For example, to give the owner full permissions and everyone else read-only permissions:

chmod 744 filename.txt

This command sets:

  • 7 (owner) = read, write, and execute.

  • 4 (group) = read-only.

  • 4 (others) = read-only.

Changing Ownership

The chown command is used to change the owner of a file:

sudo chown [new_owner]:[new_group] [filename]

For example, to change the owner to user1 and group to staff:

sudo chown user1:staff file.txt

Task:

  • Try creating a new file:

      touch myfile.txt
    
  • Change the permissions to make it readable and writable only by the owner:

      chmod 600 myfile.txt
    

    4. Process Management in Linux

    A process is essentially a running instance of a program. Linux provides various tools to manage these processes, including viewing, stopping, and prioritizing them.

    Key Commands for Process Management

    1. Viewing Processes:

      • To view a list of running processes, use the ps command:

          ps aux
        
        • a: Shows processes for all users.

        • u: Shows detailed information (user, memory usage, etc.).

        • x: Shows processes not associated with a terminal.

      • Another useful command for real-time process monitoring is top:

          top
        

        This shows CPU and memory usage of running processes and is refreshed in real-time.

      • htop (an improved version of top): If it's installed on your system, you can run htop for an interactive process viewer (if it's not installed, you can install it using sudo apt install htop).

    2. Killing Processes: If a process is unresponsive, you can terminate it with the kill command by providing its process ID (PID).

      • First, find the PID of the process using ps aux or top.

      • Then, use the kill command:

          kill [PID]
        
      • To forcefully kill a process:

          kill -9 [PID]
        
    3. Starting Processes in the Background: You can run processes in the background so that the terminal remains free for other commands.

      • To run a command in the background, use &:

          some-command &
        
      • To see jobs running in the background:

          jobs
        
      • To bring a background process to the foreground:

          fg [job_number]
        
    4. Nice and Renice: You can prioritize processes by adjusting their "niceness" (how nicely they use system resources).

      • Start a process with a specific nice value (default is 0):

          nice -n [nice_value] command
        
        • Lower values give higher priority (-20 is the highest priority, +19 is the lowest).
      • Adjust the niceness of a running process using renice:

          sudo renice [new_nice_value] -p [PID]
        

Task:

  1. Run top and observe which processes are using the most CPU and memory.

  2. Find a process using ps aux, and terminate it using kill [PID].

Example:

    ps aux | grep some-process
    kill 1234

5. Networking Basics in Linux

Networking is a critical part of any Linux system, especially if you are working with servers, managing remote systems, or running web applications.

Key Commands for Networking

  1. Checking Network Configuration:

    • Use the ifconfig or ip command to check your network interfaces and their configurations:

        ifconfig
      

      or

        ip addr
      
      • This will show details such as IP address, netmask, broadcast address, etc.
  2. Pinging a Host: The ping command is used to check connectivity to another system:

     ping google.com
    
    • This sends ICMP echo requests and checks if you can reach the host. It also measures the round-trip time (latency).
  3. Checking Network Ports:

    • Use the netstat or ss command to check which ports are in use and which services are listening for connections.

        netstat -tuln
      

      or

        ss -tuln
      
      • -t: Show TCP ports.

      • -u: Show UDP ports.

      • -l: Show listening ports.

      • -n: Show numeric addresses and port numbers.

  4. Testing Connectivity with traceroute: The traceroute command shows the path that packets take to reach a remote host.

     traceroute google.com
    
  5. File Transfers with scp: The scp command (secure copy) is used to copy files between hosts using SSH.

     scp file.txt username@remote_host:/path/to/destination
    
    • To copy a file from a remote server to your local machine:

        scp username@remote_host:/path/to/file.txt /local/path/
      
  6. Managing Network Interfaces: You can bring network interfaces up or down manually:

    • To bring an interface up:

        sudo ifconfig eth0 up
      
    • To bring an interface down:

        sudo ifconfig eth0 down
      
  7. Configuring Static IP Addresses:

    • You can set a static IP for your network interface using the ifconfig command:

        sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
      
    • For persistent configurations, you would edit network configuration files (e.g., /etc/network/interfaces for Ubuntu or /etc/sysconfig/network-scripts/ for CentOS).

Task:

  1. Use the ifconfig or ip addr command to view your network interface details.

  2. Try pinging a website (e.g., ping google.com) to test network connectivity.

  3. Run netstat -tuln or ss -tuln to see which ports are open on your system.

Example:

    ping google.com
    ifconfig
    netstat -tuln

6. Shell Scripting in Linux

Shell scripting allows you to write a series of commands in a script file and execute them all at once. This can help automate repetitive tasks and improve your productivity as a system administrator.

Writing a Basic Shell Script

  1. Create a Script File: Shell scripts typically have a .sh extension, but that's not required.

    • Create a script using any text editor like vim, nano, or gedit. For example:

        nano myscript.sh
      
  2. Add a Shebang: The first line in a shell script is usually the shebang. It tells the system which interpreter to use to run the script. For bash scripts, use:

     #!/bin/bash
    
  3. Add Commands: You can add regular shell commands to your script. For example:

     #!/bin/bash
     echo "Hello, World!"
     ls -l
    
  4. Make the Script Executable: Before you can run the script, you need to give it execute permissions:

     chmod +x myscript.sh
    
  5. Run the Script: You can execute the script by specifying the file path:

     ./myscript.sh
    

Variables in Shell Scripts:

You can store data in variables and use them in your scripts.

  • Assigning a value:

      NAME="Alice"
    
  • Using a variable:

      echo "Hello, $NAME!"
    

Conditional Statements:

You can use if statements to add conditional logic to your scripts:

    #!/bin/bash
    if [ $1 -gt 10 ]
    then
        echo "The number is greater than 10."
    else
        echo "The number is less than or equal to 10."
    fi

Here, $1 refers to the first argument passed to the script.

Loops:

Loops allow you to repeat tasks. For example, a for loop:

    #!/bin/bash
    for i in 1 2 3 4 5
    do
       echo "Welcome $i times"
    done

Task:

  1. Create a shell script that prints "Hello, Linux!" and lists the contents of your home directory.

  2. Make the script executable and run it.

Example:

    #!/bin/bash
    echo "Hello, Linux!"
    ls ~

6.1 Advanced Shell Scripting

1. Functions in Shell Scripts

Functions allow you to group commands into a single reusable block. You can define a function and call it multiple times within a script.

  • Defining a Function:

      my_function() {
        echo "This is my function."
      }
    
  • Calling the Function:

      my_function
    
  • Example:

      #!/bin/bash
      greeting() {
        echo "Hello, $1!"
      }
    
      greeting "World"
      greeting "Alice"
    

In this example, $1 is a positional parameter, which means the function can take arguments.

2. Working with Files and Directories

Shell scripts are frequently used to automate file handling tasks, like copying, moving, or deleting files.

  • Reading a File Line by Line:

      while IFS= read -r line
      do
        echo "$line"
      done < myfile.txt
    
  • Example - Backup Script: Here’s a script that creates a backup of a directory:

      #!/bin/bash
    
      src="/path/to/source"
      dest="/path/to/backup"
    
      echo "Backing up $src to $dest"
      cp -r $src $dest
      echo "Backup complete!"
    

3. Input/Output Redirection and Pipes

Redirection and pipes allow you to control where input and output go.

  • Redirect Output:

    • Write output to a file:

        ls > output.txt
      
    • Append output to a file:

        echo "new line" >> output.txt
      
  • Piping Output: Piping takes the output of one command and uses it as input for another.

      ls | grep "txt"
    

4. Error Handling

Shell scripts should handle errors gracefully to avoid unexpected behavior.

  • Using || for Error Handling: You can use || to run a command if the previous one fails:

      mkdir /backup || echo "Failed to create backup directory"
    
  • Exit Status: Every command in Linux returns an exit status. A status of 0 means success, while any other number means an error occurred.

    • Check the exit status of the last command:

        echo $?
      
  • Using set -e: This makes the script exit immediately if a command fails:

      set -e
    

5. Scheduling Tasks with cron

You can automate your shell scripts using cron. cron allows you to schedule scripts to run at specific intervals (e.g., daily, hourly).

  • Editing Crontab:

      crontab -e
    

    Add a new cron job in the following format:

      * * * * * /path/to/script.sh
    

    This format consists of five time fields (minute, hour, day of month, month, day of week) followed by the command to run.

    Example: To run a backup script every day at 3 AM:

      0 3 * * * /path/to/backup_script.sh
    

Task:

  1. Write a script that creates a backup of your home directory and logs the result to a file.

  2. Add error handling in the script to check if the backup was successful.

  3. Schedule the script to run daily using cron.

Example:

    #!/bin/bash
    set -e

    src="/home/user"
    dest="/backup/home_backup"

    echo "Starting backup of $src to $dest" >> /var/log/backup.log
    if cp -r $src $dest; then
        echo "Backup successful" >> /var/log/backup.log
    else
        echo "Backup failed" >> /var/log/backup.log
    fi

7. System Administration in Linux

1. User and Group Management

Managing users and groups is essential for controlling access and security on a Linux system.

  • Creating Users: You can create a new user using the useradd command. For example:

      sudo useradd newuser
    

    Set a password for the user:

      sudo passwd newuser
    
  • Modifying Users: You can modify a user's information, such as their home directory, with the usermod command:

      sudo usermod -d /new/home/directory newuser
    
  • Deleting Users: Remove a user and their home directory using:

      sudo userdel -r newuser
    
  • Group Management:

    • Create a group:

        sudo groupadd newgroup
      
    • Add a user to a group:

        sudo usermod -aG newgroup newuser
      
  • Switch Users: You can switch to another user using su:

      su - newuser
    

2. Permissions and Ownership

File permissions and ownership in Linux are fundamental for ensuring security and proper access.

  • Viewing Permissions: Use ls -l to view file permissions:

      ls -l /path/to/file
    

    The output will look like:

      -rwxr-xr--
    

    This represents:

    • r: Read

    • w: Write

    • x: Execute

    • The first three characters are for the owner, the next three for the group, and the last three for others.

  • Changing Permissions: You can change file permissions with chmod. For example:

      chmod 755 /path/to/file
    

    This makes the file readable and executable by everyone, but only writable by the owner.

  • Changing Ownership: To change the owner or group of a file, use the chown command:

      sudo chown user:group /path/to/file
    

3. Package Management

Every Linux distribution has a package manager that allows you to install, update, and remove software.

  • Debian-based Systems (e.g., Ubuntu): Use apt for package management:

      sudo apt update       # Updates the package list
      sudo apt upgrade      # Upgrades all installed packages
      sudo apt install package-name
      sudo apt remove package-name
    
  • Red Hat-based Systems (e.g., CentOS, Fedora): Use yum or dnf for package management:

      sudo yum update
      sudo yum install package-name
      sudo yum remove package-name
    
  • Finding Packages: You can search for packages with:

      apt search package-name
    

4. Service Management

Services (or daemons) are background processes that provide functionality like networking, logging, or web services.

  • Starting, Stopping, and Checking Status: Use systemctl to manage services:

      sudo systemctl start service-name
      sudo systemctl stop service-name
      sudo systemctl restart service-name
      sudo systemctl status service-name
    
  • Enabling/Disabling Services: To ensure a service starts automatically on boot:

      sudo systemctl enable service-name
      sudo systemctl disable service-name
    

5. Disk Management

Managing disk space and partitions is another critical task for system administrators.

  • Checking Disk Usage: Use df to see how much space is being used on each mounted file system:

      df -h
    
  • Checking Disk I/O: You can use iostat (from the sysstat package) to monitor disk I/O performance:

      sudo apt install sysstat
      iostat
    
  • Partition Management: Tools like fdisk or parted allow you to create, modify, and delete partitions:

      sudo fdisk /dev/sda
    

6. System Performance Monitoring

Monitoring the performance of your system is important to ensure it runs smoothly.

  • Monitoring CPU and Memory Usage: Use top or htop to monitor CPU, memory usage, and running processes in real-time:

      top
      htop
    
  • Checking System Logs: System logs are essential for troubleshooting. The most common log files are stored in /var/log/. To view logs:

      tail -f /var/log/syslog
    
  • Scheduling Tasks with Cron: As mentioned in the shell scripting section, cron can be used to schedule system tasks like backups or updates.

Task:

  1. Create a new user with useradd, assign them a password, and add them to a group.

  2. Use chmod to change the permissions of a file to make it readable and executable by everyone, but only writable by the owner.

  3. Install a package using apt and check its service status using systemctl.

Example:

    sudo useradd john
    sudo passwd john
    sudo usermod -aG sudo john

    chmod 755 /path/to/file

    sudo apt install apache2
    sudo systemctl start apache2
    sudo systemctl status apache2

8. System Security and Firewall Management

1. User and Group Security

As a system admin, you need to ensure that users and groups have the appropriate level of access.

  • Limit Root Access: The root account has unlimited access to the system. Limiting root access is critical for security.

    • Use sudo for administrative tasks instead of logging in as root. You can limit which users have sudo access by modifying /etc/sudoers:

        sudo visudo
      
    • Add specific commands users can run with sudo to restrict their privileges.

  • Password Policies: Enforce strong password policies to enhance security.

    • Set password expiration with the chage command:

        sudo chage -M 90 username    # Set password to expire after 90 days
      
  • Locking Accounts: Lock inactive or compromised user accounts:

      sudo usermod -L username  # Lock user
      sudo usermod -U username  # Unlock user
    

2. SSH Security

SSH is commonly used for remote login. Ensuring it's secure is critical.

  • Disable Root Login via SSH: You should disable remote root login to prevent brute-force attacks on the root account.

    • Edit the SSH configuration file:

        sudo nano /etc/ssh/sshd_config
      
    • Look for the line PermitRootLogin and change it to no:

        PermitRootLogin no
      
    • Restart SSH for the changes to take effect:

        sudo systemctl restart sshd
      
  • Use SSH Keys Instead of Passwords: SSH keys provide stronger authentication than passwords.

    • Generate SSH keys:

        ssh-keygen -t rsa -b 4096
      
    • Copy the public key to the server:

        ssh-copy-id user@hostname
      
    • Disable password authentication in the SSH configuration:

        PasswordAuthentication no
      

3. Firewall Management with ufw (Uncomplicated Firewall)

The firewall helps filter incoming and outgoing traffic, allowing you to control access to your system.

  • Installing ufw: On Ubuntu or Debian systems, ufw is often pre-installed, but you can install it if needed:

      sudo apt install ufw
    
  • Basic Firewall Rules: Allow traffic on specific ports. For example, to allow SSH (port 22):

      sudo ufw allow 22/tcp
    

    If you're running a web server (e.g., Apache or Nginx), allow HTTP and HTTPS traffic:

      sudo ufw allow 80/tcp  # HTTP
      sudo ufw allow 443/tcp # HTTPS
    
  • Enable the Firewall: Once you've added the necessary rules, enable the firewall:

      sudo ufw enable
    
  • Check Firewall Status: You can check the current status of the firewall:

      sudo ufw status
    
  • Deny All Incoming Traffic by Default: For enhanced security, deny all incoming traffic unless explicitly allowed:

      sudo ufw default deny incoming
      sudo ufw default allow outgoing
    

4. Installing and Configuring Fail2Ban

Fail2Ban helps prevent brute-force attacks by banning IP addresses that exhibit suspicious behavior (e.g., too many failed login attempts).

  • Installing Fail2Ban:

      sudo apt install fail2ban
    
  • Configuring Fail2Ban: Edit the default configuration:

      sudo nano /etc/fail2ban/jail.local
    

    You can configure settings like the maximum number of failed login attempts before banning an IP:

      [sshd]
      enabled = true
      maxretry = 3
      bantime = 3600  # Ban IP for 1 hour after 3 failed attempts
    
  • Starting Fail2Ban:

      sudo systemctl start fail2ban
      sudo systemctl enable fail2ban
    

5. Auditing and Monitoring Logs

Monitoring system logs can help you detect unusual behavior or breaches.

  • Using logwatch for Automated Log Monitoring: logwatch analyzes your system logs and sends summaries to your email.

    • Install it:

        sudo apt install logwatch
      
    • Run logwatch manually:

        sudo logwatch --detail Low --mailto your-email@example.com --service all --range today
      
  • Checking Login Attempts: Review login attempts in the auth.log file:

      cat /var/log/auth.log | grep "Failed password"
    

6. AppArmor and SELinux

Both AppArmor and SELinux are Linux security modules that enforce security policies to restrict applications' access to resources.

  • AppArmor: AppArmor uses security profiles to restrict programs.

    • Check if it's enabled:

        sudo aa-status
      
    • You can enforce or put a program in complain mode:

        sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
        sudo aa-complain /etc/apparmor.d/usr.sbin.nginx
      
  • SELinux: On Red Hat-based systems, SELinux provides Mandatory Access Control (MAC).

    • Check if it's enabled:

        sestatus
      
    • Set the enforcement mode:

        sudo setenforce 1  # Enforce
        sudo setenforce 0  # Permissive
      

Task:

  1. Secure your SSH by disabling root login and enabling key-based authentication.

  2. Configure a firewall using ufw to allow only SSH, HTTP, and HTTPS.

  3. Install and configure Fail2Ban to protect against brute-force attacks.

  4. Set up automated log monitoring with logwatch and review the logs for any failed login attempts.

Example:

    # 1. Disable root login and enable SSH key-based authentication
    sudo nano /etc/ssh/sshd_config
    # Set: PermitRootLogin no
    # Set: PasswordAuthentication no
    sudo systemctl restart sshd

    # 2. Configure the firewall
    sudo ufw allow 22/tcp
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    sudo ufw enable

    # 3. Install and configure Fail2Ban
    sudo apt install fail2ban
    sudo nano /etc/fail2ban/jail.local
    # Enable [sshd] and set maxretry = 3, bantime = 3600
    sudo systemctl start fail2ban

    # 4. Install logwatch for log monitoring
    sudo apt install logwatch
    sudo logwatch --detail Low --mailto you@example.com --service all --range today

Great! Now let's jump into Linux Networking. This is a critical area for managing and troubleshooting network connections, configuring services, and understanding how data moves through the network.

10. Networking in Linux

1. Checking Network Interfaces

Network interfaces are the hardware or virtual devices that connect your system to the network. You can list the network interfaces using various commands.

  • List network interfaces with ip:

      ip addr show
    

    This shows all active and inactive network interfaces and their IP addresses.

  • Check network interface details with ifconfig (older tool):

      ifconfig
    

2. Configuring Network Interfaces

You can manually configure network interfaces to set IP addresses, gateways, and other networking options.

  • Assign a Static IP: To assign an IP address manually to a specific interface (e.g., eth0):

      sudo ip addr add 192.168.1.100/24 dev eth0
    
  • Bring an Interface Up or Down:

    • Bring an interface up:

        sudo ip link set eth0 up
      
    • Bring an interface down:

        sudo ip link set eth0 down
      
  • Check Routing Table: View the system's routing table, which shows where traffic will be routed:

      ip route show
    

3. Testing Connectivity

Testing connectivity helps diagnose network issues.

  • Ping: The ping command sends ICMP echo requests to a remote host and checks if it responds. It's commonly used to test connectivity.

      ping google.com
    

    To stop ping, press Ctrl + C.

  • Traceroute: traceroute shows the path packets take to reach a destination, useful for diagnosing routing problems.

      traceroute google.com
    

    If traceroute is not installed, install it with:

      sudo apt install traceroute
    
  • Netcat (nc): netcat is a versatile networking tool that can be used for checking open ports or sending data.

    • To check if port 80 is open on a server:

        nc -zv google.com 80
      
  • DNS Lookup: Use nslookup or dig to query DNS servers and check how a domain resolves.

      nslookup google.com
    

    For dig:

      dig google.com
    

4. Configuring DNS

The Domain Name System (DNS) is used to resolve domain names to IP addresses.

  • Change DNS Servers: To change the system’s DNS servers, edit the /etc/resolv.conf file:

      sudo nano /etc/resolv.conf
    

    Add the following lines to use Google’s public DNS servers:

      nameserver 8.8.8.8
      nameserver 8.8.4.4
    

5. Managing Services

Network services such as web servers, SSH, and DNS are essential to system operation.

  • Restarting Network Services: Use systemctl to restart network services (such as network-manager on Ubuntu):

      sudo systemctl restart NetworkManager
    
  • Check Service Status: You can check the status of any network service:

      sudo systemctl status ssh
    

6. Using netstat to Monitor Network Connections

netstat is a useful tool to monitor active network connections and listening ports.

  • View Open Ports: Check which ports are open and what services are listening:

      sudo netstat -tuln
    
  • Monitor Active Connections: To view all active connections and their status:

      netstat -ant
    

7. Using nmap for Network Scanning

nmap is a powerful tool for network discovery and security auditing.

  • Installing nmap: On Ubuntu or Debian-based systems, install it with:

      sudo apt install nmap
    
  • Scan a Network: To scan for devices on your local network:

      nmap -sn 192.168.1.0/24
    
  • Scan Open Ports: To scan open ports on a target host:

      nmap -p 1-1000 example.com
    

8. Network File Sharing

Linux supports several methods for file sharing across a network, including SSH, NFS, and Samba.

  • SSH File Transfer: You can transfer files between machines using scp (secure copy).

    • Example:

        scp /path/to/file user@remote:/path/to/destination/
      
  • NFS (Network File System): NFS allows you to share directories between Linux systems.

    • Install NFS:

        sudo apt install nfs-kernel-server
      
    • Configure NFS Exports: Edit /etc/exports and add the directories to share:

        /path/to/share 192.168.1.0/24(rw,sync,no_subtree_check)
      
    • Start NFS:

        sudo systemctl start nfs-kernel-server
      
  • Samba (for Windows): Samba allows file sharing between Linux and Windows.

    • Install Samba:

        sudo apt install samba
      
    • Configure Samba: Edit /etc/samba/smb.conf to define shares.

9. Firewall Rules and Port Forwarding

Configuring firewalls and controlling network access is a crucial part of networking.

  • Allow Traffic to Specific Ports: Using ufw, allow traffic to a specific port (e.g., port 80 for HTTP):

      sudo ufw allow 80/tcp
    
  • Forwarding Ports: Port forwarding allows external traffic to be redirected to internal services. Edit the firewall or router settings to forward ports.

Task:

  1. Check your network interfaces using ip and assign a static IP address.

  2. Test connectivity using ping and traceroute.

  3. Use rsync to transfer files between two machines over SSH.

  4. Set up netstat to monitor network connections and view open ports.

  5. Use nmap to scan for open ports on a remote server.

Example:

    # 1. Assign a static IP to an interface
    sudo ip addr add 192.168.1.100/24 dev eth0

    # 2. Test connectivity with ping and traceroute
    ping google.com
    traceroute google.com

    # 3. Transfer files using scp (SSH)
    scp /path/to/file user@remote:/path/to/destination/

    # 4. Monitor active connections with netstat
    sudo netstat -tuln

    # 5. Scan a network with nmap
    nmap -sn 192.168.1.0/24

11. Network Troubleshooting in Linux

1. Checking Network Configuration

Before troubleshooting, always verify your network configuration.

  • Check IP Address and Network Interface Status:

      ip addr show
    
  • Verify Routing Table: Ensure the default gateway is set correctly.

      ip route show
    

2. Using ping for Basic Connectivity Tests

The ping command helps check if a host is reachable.

  • Ping Localhost: Check if the local network stack is working.

      ping 127.0.0.1
    
  • Ping an External Host: Test connectivity to an external server (like Google).

      ping google.com
    
  • Analyze Results:

    • Success: If you receive replies, your connection is good.

    • Timeouts: If you see timeouts, there may be a problem.

3. Using traceroute to Identify Hops

The traceroute command shows the path packets take to reach a destination.

  • Run traceroute:

      traceroute google.com
    
  • Analyze the Output: Each line shows a hop along the route. If a hop times out, it may indicate where the problem lies.

4. Using netstat and ss to Monitor Connections

Monitor active connections and identify issues.

  • View All Connections:

      netstat -tuln
    
  • Check Established Connections:

      netstat -ant | grep ESTABLISHED
    
  • Use ss (more modern tool):

      ss -tuln
    

5. Checking Firewall Rules

Firewalls can block traffic, so ensure they are correctly configured.

  • Check UFW Status:

      sudo ufw status verbose
    
  • Allow a Specific Port: If a service (like SSH) isn’t reachable, ensure the port is open:

      sudo ufw allow 22/tcp
    

6. Analyzing Log Files

Log files can provide insight into what’s happening on your system.

  • System Logs: Check the syslog for network-related messages:

      tail -f /var/log/syslog
    
  • Specific Service Logs: For example, check SSH logs:

      tail -f /var/log/auth.log
    

7. Checking DNS Resolution

If you can’t reach a host by name, DNS may be the issue.

  • Use nslookup or dig:

      nslookup google.com
    
      dig google.com
    
  • Verify /etc/resolv.conf: Check that DNS servers are correctly listed in /etc/resolv.conf.

8. Using tcpdump for Packet Analysis

tcpdump allows you to capture and analyze network packets.

  • Capture Traffic on an Interface:

      sudo tcpdump -i eth0
    
  • Filter for Specific Traffic: To capture only HTTP traffic:

      sudo tcpdump -i eth0 port 80
    

9. Resolving Common Issues

  • No Connectivity:

    • Check cables and connections.

    • Ensure the interface is up.

    • Verify IP configuration.

  • Slow Connection:

    • Use ping and traceroute to diagnose where delays occur.

    • Check for high network traffic using netstat or ss.

  • DNS Issues:

    • Check /etc/resolv.conf.

    • Use nslookup or dig to test DNS resolution.

Task:

  1. Test connectivity to your local gateway and an external website.

  2. Use traceroute to analyze the path to an external host.

  3. Check your firewall rules to ensure necessary ports are open.

  4. Monitor active connections with netstat or ss.

  5. Use tcpdump to capture traffic on your primary network interface.

Example:

    # 1. Test connectivity to the local gateway
    ping 192.168.1.1

    # 2. Trace route to an external host
    traceroute google.com

    # 3. Check firewall status
    sudo ufw status verbose

    # 4. Monitor active connections
    sudo netstat -tuln

    # 5. Capture traffic on eth0
    sudo tcpdump -i eth0

Great! Let’s move on to Advanced Networking. This section will cover more complex networking concepts and configurations that are vital for system administration and network management.

12. Advanced Networking in Linux

1. Understanding VLANs (Virtual Local Area Networks)

VLANs allow you to segment networks logically without needing additional hardware.

  • Creating a VLAN:

    • Ensure the vconfig utility is installed (may vary by distribution).
        sudo apt install vlan
  • Create a VLAN (e.g., VLAN ID 10 on eth0):
        sudo vconfig add eth0 10
        sudo ifconfig eth0.10 up
  • Assign an IP Address to a VLAN:

      sudo ip addr add 192.168.10.1/24 dev eth0.10
    

2. Setting Up a VPN (Virtual Private Network)

A VPN creates a secure connection over the internet, allowing remote access to resources.

  • OpenVPN Installation:

      sudo apt install openvpn
    
  • Configuring OpenVPN: You’ll need to create or obtain a configuration file (usually .ovpn). Start the VPN with:

      sudo openvpn --config /path/to/config.ovpn
    

3. Advanced Routing

Routing determines how packets travel through networks. Linux can function as a router.

  • Enabling IP Forwarding: Edit /etc/sysctl.conf to enable IP forwarding:

      net.ipv4.ip_forward=1
    

    Apply changes:

      sudo sysctl -p
    
  • Configuring Static Routes: To add a static route:

      sudo ip route add 192.168.2.0/24 via 192.168.1.1
    

4. Firewall Configuration with iptables

iptables is a powerful tool for configuring firewalls on Linux.

  • Basic Commands:

    • View Current Rules:

        sudo iptables -L -v
      
    • Allow Incoming SSH Traffic:

        sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
      
    • Drop All Incoming Traffic (default policy):

        sudo iptables -P INPUT DROP
      
    • Save Rules: Depending on your distribution, save rules to persist after reboot:

        sudo iptables-save > /etc/iptables/rules.v4
      

5. Setting Up a Web Server

A web server allows you to host websites and services.

  • Installing Apache:

      sudo apt install apache2
    
  • Starting and Enabling Apache:

      sudo systemctl start apache2
      sudo systemctl enable apache2
    
  • Accessing the Web Server: Open a web browser and navigate to http://your-server-ip to see the default Apache page.

6. Configuring DNS with BIND

BIND (Berkeley Internet Name Domain) is a widely used DNS server software.

  • Install BIND:

      sudo apt install bind9
    
  • Configure BIND: Edit the configuration file /etc/bind/named.conf.local to define zones.

  • Testing Configuration: After configuring, check for syntax errors:

      sudo named-checkconf
    
  • Restart BIND:

      sudo systemctl restart bind9
    

7. Network Performance Tuning

Improve network performance with various tuning techniques.

  • Adjust TCP Settings: Modify /etc/sysctl.conf to tune TCP settings, such as buffer sizes:

      net.core.rmem_max = 16777216
      net.core.wmem_max = 16777216
    
  • Apply Changes:

      sudo sysctl -p
    

Task:

  1. Create a VLAN and assign an IP address.

  2. Set up a simple OpenVPN server using a configuration file.

  3. Enable IP forwarding and add a static route.

  4. Configure iptables to allow and deny specific traffic.

  5. Install and configure Apache as a web server.

Example:

    # 1. Create VLAN 10 on eth0
    sudo vconfig add eth0 10
    sudo ifconfig eth0.10 up
    sudo ip addr add 192.168.10.1/24 dev eth0.10

    # 2. Start OpenVPN with configuration
    sudo openvpn --config /path/to/config.ovpn

    # 3. Enable IP forwarding
    echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
    sudo sysctl -p

    # 4. Allow SSH through iptables
    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

    # 5. Install Apache
    sudo apt install apache2
    sudo systemctl start apache2
    sudo systemctl enable apache2

14. Server Management in Linux

1. Managing Web Servers

Web servers serve web content to users over HTTP/HTTPS.

  • Apache Web Server:

    • Install Apache:

        sudo apt install apache2
      
    • Start, Stop, and Restart Apache:

        sudo systemctl start apache2
        sudo systemctl stop apache2
        sudo systemctl restart apache2
      
    • Check Apache Status:

        sudo systemctl status apache2
      
  • Nginx Web Server:

    • Install Nginx:

        sudo apt install nginx
      
    • Start, Stop, and Restart Nginx:

        sudo systemctl start nginx
        sudo systemctl stop nginx
        sudo systemctl restart nginx
      

2. Managing Database Servers

Database servers store and manage data for applications.

  • MySQL Database Server:

    • Install MySQL:

        sudo apt install mysql-server
      
    • Start, Stop, and Restart MySQL:

        sudo systemctl start mysql
        sudo systemctl stop mysql
        sudo systemctl restart mysql
      
    • Secure MySQL Installation:

        sudo mysql_secure_installation
      
  • PostgreSQL Database Server:

    • Install PostgreSQL:

        sudo apt install postgresql postgresql-contrib
      
    • Start, Stop, and Restart PostgreSQL:

        sudo systemctl start postgresql
        sudo systemctl stop postgresql
        sudo systemctl restart postgresql
      

3. Managing File Servers

File servers provide shared access to files over a network.

  • Samba for File Sharing:

    • Install Samba:

        sudo apt install samba
      
    • Configure Samba: Edit /etc/samba/smb.conf to set up shared directories.

    • Create a Shared Directory:

        sudo mkdir /srv/samba/share
        sudo chown nobody:nogroup /srv/samba/share
        sudo chmod 0777 /srv/samba/share
      
  • NFS (Network File System):

    • Install NFS Server:

        sudo apt install nfs-kernel-server
      
    • Configure NFS Exports: Edit /etc/exports to define shared directories.

    • Start NFS Server:

        sudo systemctl start nfs-kernel-server
      

4. Monitoring Server Performance

Monitoring helps ensure servers run efficiently.

  • Using top and htop:

    • Top Command:

        top
      
    • Htop (more user-friendly):

        sudo apt install htop
        htop
      
  • Using vmstat and iostat:

    • VMStat (memory and process statistics):

        vmstat 1
      
    • IOStat (disk I/O statistics):

        sudo apt install sysstat
        iostat 1
      

5. Backup and Restore

Regular backups are critical for data safety.

  • Using rsync:

    • Backup Files:

        rsync -av --progress /source/directory /backup/directory
      
  • Using tar:

    • Create a Backup Archive:

        tar -cvf backup.tar /path/to/directory
      
    • Extract Backup Archive:

        tar -xvf backup.tar
      

6. Scheduling Tasks with Cron

Cron jobs allow you to automate tasks on a scheduled basis.

  • Editing Crontab:

      crontab -e
    
  • Schedule a Job: Example to run a script daily at 2 AM:

      0 2 * * * /path/to/script.sh
    

Task:

  1. Install and configure Apache or Nginx web server.

  2. Install and secure MySQL or PostgreSQL database server.

  3. Set up a Samba file share or NFS server.

  4. Monitor system performance using htop.

  5. Create a backup of a directory using rsync or tar.

Example:

    # 1. Install and start Apache
    sudo apt install apache2
    sudo systemctl start apache2

    # 2. Install MySQL and secure it
    sudo apt install mysql-server
    sudo mysql_secure_installation

    # 3. Install Samba and create a shared directory
    sudo apt install samba
    sudo mkdir /srv/samba/share
    sudo chown nobody:nogroup /srv/samba/share
    sudo chmod 0777 /srv/samba/share

    # 4. Monitor performance with htop
    sudo apt install htop
    htop

    # 5. Create a backup using rsync
    rsync -av --progress /source/directory /backup/directory

16. Networking and Troubleshooting in Linux

1. Basic Networking Concepts

Understanding how networking works is essential for effective troubleshooting and management.

  • IP Address: Unique identifier for a device on a network.

  • Subnet Mask: Defines the network and host portions of an IP address.

  • Default Gateway: The router that connects your device to other networks.

  • DNS (Domain Name System): Translates domain names to IP addresses.

2. Network Configuration

You can view and configure network settings using various commands.

  • Viewing Network Interfaces:

      ip addr show
    
  • Bringing Up/Down an Interface:

      sudo ip link set dev eth0 up
      sudo ip link set dev eth0 down
    
  • Assigning a Static IP Address: Edit the configuration file for your network interface (e.g., /etc/network/interfaces or /etc/netplan/ for Ubuntu):

      auto eth0
      iface eth0 inet static
          address 192.168.1.100
          netmask 255.255.255.0
          gateway 192.168.1.1
    

3. Network Troubleshooting Tools

Linux provides several tools to diagnose and troubleshoot network issues.

  • Ping: Check connectivity to another host.

      ping google.com
    
  • Traceroute: Trace the path packets take to a network host.

      traceroute google.com
    
  • Netstat: View active connections and listening ports.

      netstat -tuln
    
  • Curl: Make HTTP requests and check responses.

      curl -I http://example.com
    
  • Nslookup: Query DNS records for a domain.

      nslookup example.com
    

4. Firewalls and Security Groups

Managing firewalls is crucial for network security.

  • Using UFW:

    • Allow/deny specific ports:

        sudo ufw allow 80/tcp   # Allow HTTP
        sudo ufw deny 23/tcp    # Deny Telnet
      
  • Check UFW Status:

      sudo ufw status
    

5. Remote Access and SSH

SSH is commonly used for secure remote access to systems.

  • Connecting to a Remote Host:

      ssh username@remote_ip
    
  • Using SSH Key Authentication: Generate an SSH key:

      ssh-keygen
    

    Copy the public key to the remote server:

      ssh-copy-id username@remote_ip
    

6. Network Monitoring

Monitoring network traffic is essential for performance and security.

  • Using iftop: Install iftop to monitor bandwidth usage:

      sudo apt install iftop
      sudo iftop
    
  • Using nmap: Install nmap to scan for open ports:

      sudo apt install nmap
      nmap -sP 192.168.1.0/24  # Ping scan for active devices
    

Task:

  1. Check your IP address and network configuration.

  2. Use ping to test connectivity to a website.

  3. Use traceroute to see the path to a remote host.

  4. Set up a UFW firewall rule to allow SSH.

  5. Use iftop to monitor network traffic.

Example Commands:

    # Check IP address
    ip addr show

    # Test connectivity
    ping google.com

    # Trace route to a host
    traceroute google.com

    # Allow SSH through UFW
    sudo ufw allow ssh

    # Monitor network traffic
    sudo apt install iftop
    sudo iftop

1. Advanced Command-Line Usage

Understanding advanced command-line features can enhance your efficiency.

  • Pipelines: Combine multiple commands using the pipe (|) operator.

      ls -l | grep ".txt"
    
  • Redirection: Redirect output to files using > and >>.

      echo "Hello" > file.txt   # Overwrite
      echo "World" >> file.txt   # Append
    
  • Command Substitution: Use the output of one command as an argument for another.

      FILE_COUNT=$(ls | wc -l)
      echo "Number of files: $FILE_COUNT"
    

2. Process Management

Managing processes efficiently is crucial for system performance.

  • Viewing Processes:

      ps aux | less
    
  • Killing Processes:

      kill <PID>            # Terminate by PID
      killall <process_name>  # Terminate all processes by name
    
  • Using htop: A more user-friendly process viewer.

      htop
    

3. System Performance Tuning

Optimizing system performance involves monitoring and adjusting settings.

  • Memory Usage: Monitor memory usage with free:

      free -h
    
  • Swap Management: Check swap usage:

      swapon --show
    
  • Tune Swappiness: Adjust the swappiness value (default is 60):

      sudo sysctl vm.swappiness=10
    

4. Managing Services

Understanding how to manage system services is key to maintaining a server.

  • Using systemctl:

    • Start, Stop, Restart a Service:

        sudo systemctl start <service_name>
        sudo systemctl stop <service_name>
        sudo systemctl restart <service_name>
      
    • Enable/Disable Services at Boot:

        sudo systemctl enable <service_name>
        sudo systemctl disable <service_name>
      
    • Check Status of a Service:

        sudo systemctl status <service_name>
      

5. Kernel Management

Managing the Linux kernel is crucial for performance and compatibility.

  • Check Kernel Version:

      uname -r
    
  • Update Kernel: For Debian/Ubuntu systems:

      sudo apt update
      sudo apt upgrade
    

6. Backup and Restore Systems

Implementing backup strategies is essential for data protection.

  • Creating System Snapshots with timeshift: Install and configure timeshift for creating system snapshots:

      sudo apt install timeshift
      sudo timeshift --create
    
  • Restoring from a Snapshot: Use the Timeshift GUI or command line to restore:

      sudo timeshift --restore
    

Task:

  1. Use advanced command-line techniques, such as pipelines and redirection.

  2. Monitor and manage processes using htop or ps.

  3. Adjust system swappiness and monitor memory usage.

  4. Start and enable a service using systemctl.

  5. Create a system snapshot using timeshift.

Example Commands:

    # 1. List files and filter for .txt
    ls -l | grep ".txt"

    # 2. Check running processes
    htop

    # 3. Monitor memory usage
    free -h

    # 4. Start and enable a service
    sudo systemctl start apache2
    sudo systemctl enable apache2

    # 5. Create a system snapshot
    sudo apt install timeshift
    sudo timeshift --create

18. Security and Hardening in Linux

1. User and Group Management

Managing users and groups is fundamental for securing your system.

  • Adding a New User:

      sudo adduser username
    
  • Creating a New Group:

      sudo addgroup groupname
    
  • Adding a User to a Group:

      sudo usermod -aG groupname username
    
  • Removing a User:

      sudo deluser username
    

2. File Permissions and Ownership

Understanding file permissions is crucial for system security.

  • Viewing Permissions:

      ls -l
    
  • Changing Ownership:

      sudo chown username:groupname file.txt
    
  • Changing Permissions:

      sudo chmod 600 file.txt   # Owner can read/write
      sudo chmod 755 script.sh   # Owner can read/write/execute; others can read/execute
    

3. Firewall Configuration

Configuring a firewall helps protect against unauthorized access.

  • Using UFW (Uncomplicated Firewall):

    • Enable UFW:

        sudo ufw enable
      
    • Allow SSH:

        sudo ufw allow ssh
      
    • Check UFW Status:

        sudo ufw status
      

4. Secure SSH Configuration

Securing SSH is vital for remote access.

  • Editing SSH Configuration:

      sudo nano /etc/ssh/sshd_config
    
    • Disable Root Login:

        PermitRootLogin no
      
    • Change Default SSH Port (optional):

        Port 2222
      
  • Restart SSH Service:

      sudo systemctl restart sshd
    

5. Implementing Fail2ban

fail2ban helps protect against brute-force attacks.

  • Installing Fail2ban:

      sudo apt install fail2ban
    
  • Starting and Enabling Fail2ban:

      sudo systemctl start fail2ban
      sudo systemctl enable fail2ban
    
  • Check Fail2ban Status:

      sudo fail2ban-client status
    

6. Keeping the System Updated

Regular updates help patch security vulnerabilities.

  • Updating the System:

      sudo apt update && sudo apt upgrade
    

7. Intrusion Detection Systems (IDS)

Consider using an IDS to monitor for unauthorized access.

  • Installing rkhunter:

      sudo apt install rkhunter
      sudo rkhunter --check
    

Task:

  1. Add a new user and configure their permissions.

  2. Set up a firewall rule using UFW.

  3. Secure your SSH configuration.

  4. Install and configure fail2ban.

  5. Run rkhunter to check for rootkits.

Example Commands:

    # 1. Add a new user
    sudo adduser newuser

    # 2. Enable UFW and allow SSH
    sudo ufw enable
    sudo ufw allow ssh

    # 3. Secure SSH by disabling root login
    sudo nano /etc/ssh/sshd_config
    # Change PermitRootLogin to no
    sudo systemctl restart sshd

    # 4. Install and start fail2ban
    sudo apt install fail2ban
    sudo systemctl start fail2ban
    sudo systemctl enable fail2ban

    # 5. Run rkhunter to check for rootkits
    sudo apt install rkhunter
    sudo rkhunter --check
0
Subscribe to my newsletter

Read articles from Abang Laz directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abang Laz
Abang Laz