Skip to content

Linux Firewalls and Networking Commands

Overview

Linux provides various tools for managing network configurations and firewalls. This document covers basic and commonly used commands for network management and firewall configuration.

Networking Commands

ifconfig

Displays or configures network interfaces.

ifconfig [interface]

Example

ifconfig eth0
# Displays the configuration for the 'eth0' network interface

Example

ifconfig eth0 up
# Brings up the 'eth0' network interface

ip

A more modern tool for managing network interfaces, routes, and addresses.

Show Network Interfaces

ip addr show

Example

ip addr show
# Displays detailed information about all network interfaces

Assign an IP Address

sudo ip addr add <IP_address>/<netmask> dev <interface>

Example

sudo ip addr add 192.168.1.100/24 dev eth0
# Assigns the IP address 192.168.1.100 to the 'eth0' interface

Bring Up/Down an Interface

sudo ip link set <interface> up
sudo ip link set <interface> down

Example

sudo ip link set eth0 up
# Brings up the 'eth0' interface

netstat

Displays network connections, routing tables, interface statistics, and more.

netstat [options]

Example

netstat -tuln
# Displays listening TCP and UDP ports with numeric addresses

ss

A utility to investigate sockets and network connections, a modern replacement for netstat.

ss [options]

Example

ss -tuln
# Displays listening TCP and UDP sockets with numeric addresses

ping

Sends ICMP ECHO_REQUEST packets to network hosts.

ping [options] <host>

Example

ping google.com
# Pings 'google.com' to check network connectivity

traceroute

Displays the route packets take to a network host.

traceroute <host>

Example

traceroute google.com
# Shows the path taken to reach 'google.com'

nslookup

Queries DNS to obtain domain name or IP address mapping.

nslookup <domain>

Example

nslookup google.com
# Queries DNS for information about 'google.com'

route

Displays or modifies the IP routing table.

route [options]

Example

route -n
# Displays the IP routing table with numeric addresses

ip route

Displays and manages the routing table.

ip route [options]

Example

ip route show
# Displays the current routing table

Firewall Commands

iptables

Configures and manages firewall rules.

List Rules

sudo iptables -L

Example

sudo iptables -L
# Lists all current firewall rules

Add Rule

sudo iptables -A <chain> -p <protocol> --dport <port> -j <target>

Example

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allows incoming TCP connections on port 22 (SSH)

Delete Rule

sudo iptables -D <chain> -p <protocol> --dport <port> -j <target>

Example

sudo iptables -D INPUT -p tcp --dport 22 -j ACCEPT
# Deletes the rule allowing incoming TCP connections on port 22

ufw

Uncomplicated Firewall (UFW) is a user-friendly front-end for managing firewall rules.

Enable UFW

sudo ufw enable

Example

sudo ufw enable
# Enables the UFW firewall

Allow a Port

sudo ufw allow <port>

Example

sudo ufw allow 22
# Allows incoming connections on port 22 (SSH)

Deny a Port

sudo ufw deny <port>

Example

sudo ufw deny 80
# Denies incoming connections on port 80 (HTTP)

Check UFW Status

sudo ufw status

Example

sudo ufw status
# Displays the current status and rules of UFW

Summary

Linux provides a robust set of tools for managing network configurations and firewall rules. Mastering these commands helps in monitoring and securing network traffic, configuring network interfaces, and troubleshooting network issues. For more detailed information on each command, refer to the Linux manual pages.