AB
Complete your Linux mastery with advanced topics covering networking, DNS, security, file permissions, user management, and firewall configuration.
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.
Linux provides numerous tools for networking operations and diagnostics:
# Display hostname
hostname
# Display IP address
hostname -I
# Display fully qualified domain name (FQDN)
hostname -f
# 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
# 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
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 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.
When encountering network problems, follow this systematic approach:
Check interface status:
ip link show eth0
Verify hostname resolution:
nslookup hostname
Test connectivity:
ping ip_address
Trace the network path:
traceroute ip_address
Check for running services:
netstat -an | grep 80 | grep -i LISTEN
“DNS_PROBE_FINISHED_NXDOMAIN” error indicates a DNS lookup failure:
ipconfig /flushdns
on Windows)“Connection timeout” error occurs when a device fails to establish a connection:
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.
A domain name consists of multiple parts:
When you enter a URL in your browser, the following steps occur:
DNS records contain different types of information:
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:
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 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.
# 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
# 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
A switch is a networking device that connects multiple devices within a local area network (LAN) and forwards data based on MAC addresses:
A router is a device that forwards data packets between computer networks:
A gateway is a network node that serves as an access point to another network:
Linux is a multi-user operating system with robust account management capabilities:
Linux stores user information in several important files:
/etc/passwd:
Contains essential user account information:
Format: username:x:UID:GID:comment:home_directory:shell
username
: Login namex
: Placeholder for password (stored in /etc/shadow)UID
: User ID numberGID
: Primary group IDcomment
: User information (e.g., full name)home_directory
: User’s home directoryshell
: User’s login shell/etc/shadow:
Contains secure password information:
Format: username:encrypted_password:lastchange:min:max:warn:inactive:expire:reserved
username
: Login nameencrypted_password
: Hashed passwordlastchange
: Days since Jan 1, 1970 that password was last changedmin
: Minimum days before password can be changedmax
: Maximum days after which password must be changedwarn
: Days before password expires to warn userinactive
: Days after password expires until account is disabledexpire
: Days since Jan 1, 1970 that account is disabledreserved
: Reserved field/etc/group:
Contains group information:
Format: groupname:x:GID:user_list
groupname
: Group namex
: Placeholder for group password (rarely used)GID
: Group ID numberuser_list
: Comma-separated list of users in the group# 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
# 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
# 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
# Switch to another user
su - username
# Switch to root
sudo -i
# or
su -
Linux uses a permission system to control access to files and directories. Understanding this system is crucial for maintaining security.
Each file and directory has three permission sets (for owner, group, and others) with three permission types:
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:
-
for regular file, d
for directory)rw-
) show the owner’s permissionsr--
) show the group’s permissionsr--
) show permissions for othersThe chmod
command changes file permissions:
Using symbolic notation:
# 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:
# Set permissions to rwxr-xr-- (owner:rwx, group:r-x, others:r--)
chmod 754 file.txt
Octal values:
These values are added together for each category (e.g., 7 = 4+2+1 = read+write+execute).
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
SSH is a protocol for secure remote access and file transfers.
This method uses a username and password for authentication. It’s simple but less secure than key-based authentication.
This method uses cryptographic key pairs (private and public keys) for authentication. It’s more secure and can be automated.
# 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
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
iptables is a powerful firewall management tool in Linux that filters network packets based on defined rules.
A firewall is an essential security component that controls incoming and outgoing network traffic based on predetermined rules.
iptables organizes firewall rules into chains:
# 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
# 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
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
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.