Skip to content

Network Linux commands

Show network interfaces:

ip link show

Enable or disable an interface:

ip link set eth0 down
ip link set eth0 up

Manually change the interface name or address:

ip link set eth0 down
ip link set eth0 address 00:11:22:33:44:55
ip link set eth0 name lan
ip link set eth0 up

Show physical interface options:

ethtool eth0

Show interface speed:

ethtool eth0 |grep Speed

Show the kernel module used by the interface:

ethtool -i eth0

Show statistics, error packets, and collisions:

ethtool -S eth0

VLANs

Add a virtual interface with tagged VLAN 101 to eth0:

ip link add link eth0 name vlan101 type vlan id 101
ip link set vlan101 up

Bonding

ip link set eth1 down
ip link set eth2 down


modprobe -r bonding
modprobe bonding mode=4

# default xmit_hash_policy is 0 (hash with mac adress)
# modprobe bonding mode=4 xmit_hash_policy=2

ip link set eth1 master bond0
ip link set eth2 master bond0

ip link set eth1 up 
ip link set eth2 up

ip link set bond0 up

cat /proc/net/bonding/bond0

Test network speed with iperf

Use iperf for traditional tests. For speeds greater than 1 Gbps, use iperf3, which offers more options.

#Server
iperf -s 
#Client
iperf -c SERVER_IP

Some useful options:

#Server
iperf -s -i 2 -p 5002
#Client
iperf -p 5002 -c SERVER_IP
  • -t: Increase the duration of the test.
#Server
iperf -s -i 2
#Client
iperf -t 30 -c SERVER_IP

Test network speed with netperf

Run a realistic TCP throughput test in both directions.

  • https://github.com/HewlettPackard/netperf

  • Start netserver on one host.

  • Execute the script on the client:
vi netperf-test.sh
 for i in 1
     do
      netperf -H 198.51.100.102 -t TCP_STREAM -B "outbound" -i 10 -P 0 -v 0 \
        -- -s 256K -S 256K &
      netperf -H 198.51.100.102 -t TCP_MAERTS -B "inbound"  -i 10 -P 0 -v 0 \
        -- -s 256K -S 256K &
     done

The test runs in the background and prints the results to the console when it finishes.

You can monitor live network throughput during the test using iftop:

iftop -i eth0

Bridges

  1. Create a bridge:
brctl addif br0 usb0
  1. Assign interfaces to the bridge:
brctl addif br0 eth0
brctl addif br0 usb0
brctl addif br0 usb1
brctl delif br0 usb1
  1. Display the bridge members, ports, and MAC addresses:
brctl show
brctl show br0
brctl showmacs br0

Capture packets

tcpdump: A classic tool for packet captures.

tcpdump -w 08232010.pcap -i eth0

tshark: Wireshark in the command line. For capture filters, see [https://wiki.wireshark.org/CaptureFilters].

  • [ -i ] Input interface
  • [ -w ] Output file
  • [ -r ] Read options from file
  • [ -c ] Limit the number of packets captured
  • [ -a duration:X] Stop the capture after X seconds
  • [ -Y ] Wireshark filters

Examples:

tshark -i enp3s0 -w /tmp/out.pcap -c 10
tshark -i enp3s0 -w /tmp/out.pcap -a duration:4
tshark -r /tmp/out.pcap -w /tmp/web.pcap -Y "http"

tshark -i enp3s0 -w /tmp/ping.pcap "host 8.8.8.8"
tshark -i enp3s0 -w /tmp/web.pcap "port 80"

To display packets on the screen with the extended version (-x):

tshark -r /tmp/web.pcap
tshark -r /tmp/web.pcap -x 
tshark -r /tmp/web.pcap -x -Y "frame.number==10"

Network Layer

Show IP addresses. This is the short command for ip address show:

ip a

Show statistics (tx, rx, error, dropped):

ip -statistics a

Fixed IP address

  1. Remove all IP addresses from an interface:
ip a f dev eth0
  1. Assign an IP address to an interface. Include the subnet mask:
ip a a 192.168.100.10/24 dev eth0
  1. Assign multiple IP addresses to an interface or remove a specific address:
ip a a 192.168.100.10/24 dev eth0
ip a a 192.168.200.11/27 dev eth0

ip a d 192.168.200.11/27 dev eth0

Dynamic IPs

To release the current IP:

dhclient -r

This command releases the current IP and terminates the daemon. If you cannot request a new IP, kill the process using:

killall dhclient

To request a new IP on any interface:

dhclient

To request a new IP on a specific interface:

dhclient eth0

To view more details about IP assignment:

dhclient -v eth0
dhclient -v -lf /tmp/eth0.lease
cat /tmp/eth0.lease

ARP Table

The traditional arp command is now centralized in the ip utility with the ip neigh option.

To view the current ARP table:

ip neigh

To clear the ARP table:

ip neigh flush all

To add a permanent ARP entry:

ip neigh add 192.168.100.1 lladdr 00:11:22:33:44:55 dev enp3s0

To view only reachable entries:

ip neigh show nud reachable

Routing

View the current routes with ip route show:

ip r 

Delete all routes with ip route flush:

ip r f all

When you add an IP address to an interface, a direct route is added. This route indicates that traffic for that IP network goes directly through the interface without passing through a router:

$ ip r f all
$ ip a a 172.16.0.10/16 dev eth0
$ ip r s 
172.16.0.10/16  dev eth0 [...]

Add a default gateway:

ip r a default via 192.168.1.1

Delete the default gateway:

ip r d default

Add a static route:

ip r a 192.168.200.0/24 via 192.168.100.1

Enable the forwarding bit to act as a router:

echo 1 > /proc/sys/net/ipv4/ip_forward

DNS name resolution

Linux first checks the /etc/hosts file for name resolution. This file contains a line for localhost. You can add lines to this file to resolve names without using a DNS server:

$ cat /etc/hosts
127.0.0.1       localhost.localdomain localhost

To resolve names using DNS servers, check the contents of the /etc/resolv.conf file:

$ cat /etc/resolv.conf 
[...]
nameserver 192.168.1.1

Look for the line starting with nameserver, followed by the IP address of the DNS server you want to use. If the DHCP server provides a DNS server when renewing the dynamic IP address, this file is overwritten.

To manually force name resolution through a DNS server:

echo "nameserver 8.8.8.8" > /etc/resolv.conf

IP masquerading

Masquerade all outbound internet traffic via eth0.

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Check iptables:

iptables-save
iptables -t filter -L
iptables -t nat -L

Connectivity tests

Check ARP protocol responses:

arping 192.168.100.1

Ping indefinitely (press Ctrl + C to stop):

ping 8.8.8.8

Limit the number of pings:

ping -c 2 8.8.8.8

fping: Ping multiple devices on a network:

fping -g 192.168.100.100 192.168.100.110 -a -q
fping -g 192.168.100.0/24 -a -q

nmap

Ping scan:

nmap -sP 192.168.100.0/24
Port 22 scan

Traceroute and IP geolocation

traceroute: A historical tool for discovering network routes.

traceroute 8.8.8.8

mtr: Use this tool if you need more information, statistics, and IP geolocation details.

dnf -y install mtr GeoIP GeoIP-GeoLite-data GeoIP-GeoLite-data-extra
mtr -n --report www.example.com
geoiplookup 203.0.113.10

Transport layer

nmap and port scanning

Scan typical TCP ports:

nmap -sS 192.168.100.1

Scan a specific port:

nmap -sS -p 80 192.168.100.1

Scan a range of ports:

nmap -sS -p 8080-8090 192.168.100.1

Scan ports even if the host does not respond to ping by adding the -PN option:

nmap -sS -PN -p 80 192.168.100.1

Scan UDP ports:

nmap -sU -p 53 192.168.100.1

netstat

Display the number of listening TCP and UDP ports, along with the associated program:

netstat -utlnp

You can combine options in any order. The common netstat options are:

  • -u: UDP ports
  • -t: TCP ports
  • -p: The program using the connection and its PID (visible only if you are root)
  • -n: Display the port number
  • -a: All connections
  • -l: Listening ports

All TCP connections:

netstat -tanp 

Test ports with netcat