Linux Essentials: A Comprehensive Guide - Part 3 - Networking

Complete your Linux mastery with advanced topics covering networking, DNS, security, file permissions, user management, and firewall configuration.

Linux Essentials: A Comprehensive Guide - Part 3 - Networking

Table of Contents

Linux Networking Fundamentals

Networking is a fundamental aspect of Linux systems administration and is essential for system-to-system communication, internet access, and services deployment. This section explores essential networking concepts and commands in Linux.

Basic Networking Commands

Linux provides numerous tools for networking operations and diagnostics:

Hostname and IP Configuration

# Display hostname
hostname

# Display IP address
hostname -I

# Display fully qualified domain name (FQDN)
hostname -f

Network Interface Management

# List all network interfaces
ip link

# Show IP addresses for all interfaces
ip addr

# Bring an interface up
sudo ip link set dev eth0 up

# Bring an interface down
sudo ip link set dev eth0 down

# Add an IP address to an interface
sudo ip addr add 192.168.1.10/24 dev eth0

Routing Configuration

# Show routing table
ip route

# Add a default gateway
sudo ip route add default via 192.168.1.1

# Delete a default route
sudo ip route del default

Network Connectivity Testing

Ping

The ping command is used to test network connectivity between your computer and another device on a network:

# Basic ping to test connectivity
ping example.com

# Limit the number of packets
ping -c 4 example.com

# Ping with a specific packet size
ping -s 1500 example.com

Ping sends ICMP Echo Request packets to the target host and waits for Echo Reply packets. This helps diagnose network issues and measure round-trip time for messages.

Telnet

Telnet is a network protocol for remotely accessing and managing devices:

# Connect to a remote host on the default port (23)
telnet hostname

# Connect to a specific port (e.g., to test if port 80 is open)
telnet hostname 80

Important Note: Telnet transmits data in plain text, making it insecure for sensitive operations. For secure remote access, use SSH instead.

Troubleshooting Network Issues

When encountering network problems, follow this systematic approach:

  1. Check interface status:

    ip link show eth0
    
  2. Verify hostname resolution:

    nslookup hostname
    
  3. Test connectivity:

    ping ip_address
    
  4. Trace the network path:

    traceroute ip_address
    
  5. Check for running services:

    netstat -an | grep 80 | grep -i LISTEN
    

Common Network Errors

“DNS_PROBE_FINISHED_NXDOMAIN” error indicates a DNS lookup failure:

  • The domain name doesn’t exist or can’t be found
  • Possible causes: incorrect domain name, DNS server issues, network configuration problems
  • Troubleshooting steps:
    • Verify the domain name
    • Use alternative DNS servers (e.g., Google’s 8.8.8.8)
    • Flush DNS cache (ipconfig /flushdns on Windows)
    • Check network settings

“Connection timeout” error occurs when a device fails to establish a connection:

  • Possible causes: network congestion, server overload, network outages
  • Troubleshooting steps follow the systematic approach described above

DNS and Name Resolution

What is DNS?

The Domain Name System (DNS) is a hierarchical naming system that translates human-readable domain names (like example.com) into IP addresses (like 192.0.2.1) that computers use to identify each other on a network.

Domain Name Structure

A domain name consists of multiple parts:

Domain Name Structure

  • Top-Level Domain (TLD): The last part (e.g., .com, .org, .net)
  • Second-Level Domain (SLD): The part before the TLD (e.g., example in example.com)
  • Subdomain: Optional part before the SLD (e.g., blog in blog.example.com)

How DNS Resolution Works

When you enter a URL in your browser, the following steps occur:

DNS Resolution Steps

  1. Your browser checks its cache for the domain name
  2. If not found, it queries the operating system’s cache
  3. If still not found, it queries the configured DNS server
  4. The DNS server may query other DNS servers in a hierarchical manner
  5. Once resolved, the IP address is returned to your browser

DNS Record Types

DNS records contain different types of information:

DNS Record Types

  • A Record: Maps a domain name to an IPv4 address
  • AAAA Record: Maps a domain name to an IPv6 address
  • CNAME Record: Creates an alias for another domain name
  • MX Record: Specifies mail servers for the domain
  • TXT Record: Stores text information (often used for verification)
  • NS Record: Specifies authoritative DNS servers for the domain

Host Files and DNS Configuration

The /etc/hosts File

The /etc/hosts file maps hostnames to IP addresses locally:

# Example /etc/hosts file
127.0.0.1       localhost
192.168.1.10    myserver.local

This file is useful for:

  • Local development environments
  • Overriding DNS entries
  • Static hostname resolution

The /etc/resolv.conf File

The /etc/resolv.conf file specifies DNS servers:

# Example /etc/resolv.conf file
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com

The /etc/nsswitch.conf File

The /etc/nsswitch.conf file specifies the order of name resolution:

# Example from /etc/nsswitch.conf
hosts: files dns

This line specifies that the system should check the /etc/hosts file first, then use DNS if the hostname is not found locally.

DNS Lookup Tools

nslookup

# Basic lookup
nslookup example.com

# Specify record type
nslookup -type=MX example.com

# Use a specific DNS server
nslookup example.com 8.8.8.8

dig (Domain Information Groper)

# Basic lookup
dig example.com

# Specify record type
dig example.com MX

# Perform a reverse lookup
dig -x 192.0.2.1

# Use a specific DNS server
dig @8.8.8.8 example.com

Network Hardware Concepts

Switches

A switch is a networking device that connects multiple devices within a local area network (LAN) and forwards data based on MAC addresses:

  • Layer 2 Switches: Operate at the Data Link layer, forwarding based on MAC addresses
  • Layer 3 Switches: Can also perform routing functions at the Network layer
  • Functions: Forwards data only to the intended recipient, learns device locations, prevents loops
  • Benefits: Improved performance, scalability, security through VLANs

Routers

A router is a device that forwards data packets between computer networks:

  • Functions: Determines best path for data, connects different networks, translates addresses (NAT)
  • Types: Home routers, enterprise routers, core routers, edge routers
  • Benefits: Efficient routing, network segmentation, security features

Gateways

A gateway is a network node that serves as an access point to another network:

  • Functions: Protocol conversion, data routing, security, network translation
  • Types: Network gateways, application gateways, VoIP gateways, cloud gateways
  • Default Gateway: The router that a device uses to access networks beyond the local network

Linux Security and Access Control

User Accounts in Linux

Linux is a multi-user operating system with robust account management capabilities:

Types of Accounts

  1. Regular User Accounts: Standard users with limited permissions
  2. System Accounts: Used by system services (UIDs < 1000)
  3. Service Accounts: Used by services and daemons
  4. Root Account: Superuser with full administrative privileges (UID 0)
  5. Guest Accounts: For temporary users

Account Information Files

Linux stores user information in several important files:

/etc/passwd:

Contains essential user account information:

/etc/passwd File

Format: username:x:UID:GID:comment:home_directory:shell

  • username: Login name
  • x: Placeholder for password (stored in /etc/shadow)
  • UID: User ID number
  • GID: Primary group ID
  • comment: User information (e.g., full name)
  • home_directory: User’s home directory
  • shell: User’s login shell

/etc/shadow:

Contains secure password information:

/etc/shadow File

Format: username:encrypted_password:lastchange:min:max:warn:inactive:expire:reserved

  • username: Login name
  • encrypted_password: Hashed password
  • lastchange: Days since Jan 1, 1970 that password was last changed
  • min: Minimum days before password can be changed
  • max: Maximum days after which password must be changed
  • warn: Days before password expires to warn user
  • inactive: Days after password expires until account is disabled
  • expire: Days since Jan 1, 1970 that account is disabled
  • reserved: Reserved field

/etc/group:

Contains group information:

Format: groupname:x:GID:user_list

  • groupname: Group name
  • x: Placeholder for group password (rarely used)
  • GID: Group ID number
  • user_list: Comma-separated list of users in the group

User and Group Management

User Account Management Commands

User Management Commands

# Add a new user
sudo useradd username

# Add a user with specific options
sudo useradd -m -d /home/username -s /bin/bash username

# Set or change a user's password
sudo passwd username

# Delete a user
sudo userdel username

# Delete a user and their home directory
sudo userdel -r username

# Modify a user account
sudo usermod -s /bin/bash username

# Add a user to a group
sudo usermod -aG groupname username

# Lock a user account
sudo passwd -l username

# Unlock a user account
sudo passwd -u username

Group Management Commands

# Create a new group
sudo groupadd groupname

# Create a group with a specific GID
sudo groupadd -g 1010 groupname

# Delete a group
sudo groupdel groupname

# Modify a group
sudo groupmod -n newname oldname

Viewing User and Group Information

# Display current user information
id

# Display information for a specific user
id username

# Show who is logged in
who

# Show login history
last

# List all users
cat /etc/passwd

# List all groups
cat /etc/group

Switching Users

# Switch to another user
su - username

# Switch to root
sudo -i
# or
su -

File Permissions and Access Control

Linux uses a permission system to control access to files and directories. Understanding this system is crucial for maintaining security.

Basic File Permissions

Each file and directory has three permission sets (for owner, group, and others) with three permission types:

Linux File Permissions

  • Read (r): View file contents or list directory contents
  • Write (w): Modify file contents or create/delete files in a directory
  • Execute (x): Run a file as a program or access files in a directory

File permissions are displayed in the first column of ls -l output:

$ ls -l file.txt
-rw-r--r-- 1 user group 1234 Jan 20 12:34 file.txt

In this example:

  • The first character indicates the file type (- for regular file, d for directory)
  • The next three characters (rw-) show the owner’s permissions
  • The next three (r--) show the group’s permissions
  • The last three (r--) show permissions for others

Linux Permissions Explanation

Changing File Permissions

The chmod command changes file permissions:

Using symbolic notation:

chmod with Symbols

# Give owner read, write, and execute permissions
chmod u+rwx file.txt

# Remove write permission from group and others
chmod go-w file.txt

# Set specific permissions for all categories
chmod u=rwx,g=rx,o=r file.txt

Using numeric (octal) notation:

chmod with Numbers

# Set permissions to rwxr-xr-- (owner:rwx, group:r-x, others:r--)
chmod 754 file.txt

Octal values:

  • 4 = read
  • 2 = write
  • 1 = execute
  • 0 = no permission

These values are added together for each category (e.g., 7 = 4+2+1 = read+write+execute).

Changing Ownership

The chown command changes file ownership:

# Change owner
sudo chown username file.txt

# Change owner and group
sudo chown username:groupname file.txt

# Change recursively for a directory
sudo chown -R username:groupname directory/

The chgrp command changes only the group:

sudo chgrp groupname file.txt

Secure Shell (SSH)

SSH is a protocol for secure remote access and file transfers.

SSH Authentication Methods

  1. Password Authentication:

SSH using Password

This method uses a username and password for authentication. It’s simple but less secure than key-based authentication.

  1. Key-Based Authentication:

SSH using Key

This method uses cryptographic key pairs (private and public keys) for authentication. It’s more secure and can be automated.

Creating and Managing SSH Keys

# Generate an SSH key pair
ssh-keygen -t rsa -b 4096 -C "[email protected]"

# Copy your public key to a remote server
ssh-copy-id username@remote_host

# View authorized keys on your system
cat ~/.ssh/authorized_keys

# Connect to a remote server
ssh username@remote_host

# Connect using a specific key
ssh -i ~/.ssh/id_rsa username@remote_host

# Connect using a non-standard port
ssh -p 2222 username@remote_host

Secure File Transfer with SCP

SCP (Secure Copy Protocol) uses SSH for secure file transfers:

# Copy a local file to a remote server
scp /path/to/local/file username@remote_host:/path/to/remote/directory

# Copy a remote file to the local system
scp username@remote_host:/path/to/remote/file /path/to/local/directory

# Copy a directory recursively
scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory

Firewall Management with iptables

iptables is a powerful firewall management tool in Linux that filters network packets based on defined rules.

Network Security Concepts

Network Security

A firewall is an essential security component that controls incoming and outgoing network traffic based on predetermined rules.

iptables Concepts

iptables organizes firewall rules into chains:

iptables Chains

  • INPUT: Controls incoming packets destined for the local system
  • FORWARD: Controls packets being routed through the system
  • OUTPUT: Controls outgoing packets originating from the local system

Basic iptables Commands

# List all rules
sudo iptables -L

# List rules with line numbers and packet counts
sudo iptables -L -v --line-numbers

# Append a rule to the INPUT chain
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Insert a rule at position 1 in the INPUT chain
sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT

# Delete a rule by line number
sudo iptables -D INPUT 2

Common iptables Rules

Adding iptables Rules

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

# Allow incoming HTTP connections
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# Allow incoming HTTPS connections
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# Allow traffic from a specific IP address
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT

# Block traffic from a specific IP address
sudo iptables -A INPUT -s 192.168.1.101 -j DROP

# Allow established and related connections
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Set default policy to DROP for INPUT chain
sudo iptables -P INPUT DROP

# Allow all outgoing traffic
sudo iptables -P OUTPUT ACCEPT

Understanding iptables Options

  • -A: Append a rule
  • -I: Insert a rule (with optional position number)
  • -D: Delete a rule
  • -p: Specify protocol (tcp, udp, icmp)
  • -s: Specify source address
  • -d: Specify destination address
  • –dport: Specify destination port
  • –sport: Specify source port
  • -j: Jump to target (ACCEPT, DROP, REJECT)
  • -P: Set default policy for a chain

Saving iptables Rules

iptables rules are not persistent by default. To save them:

On Debian/Ubuntu:

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

On CentOS/RHEL:

sudo service iptables save

Conclusion

This third part of our Linux Essentials guide has covered crucial aspects of Linux networking, security, and access control. By understanding these concepts and mastering the associated commands, you’ll be well-equipped to manage Linux systems securely and efficiently in networked environments.

Linux’s robust networking capabilities and security features make it an excellent choice for servers, network devices, and mission-critical systems. As you continue your Linux journey, remember that security is an ongoing process that requires regular updates, monitoring, and maintenance.

Practice implementing the concepts covered in this guide in a test environment before applying them to production systems. This approach will help you gain confidence and avoid potential issues while strengthening your Linux administration skills.

Table of Contents