Network Linux commands¶
Link layer¶
Show network interfaces:
Enable or disable an interface:
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:
Show interface speed:
Show the kernel module used by the interface:
Show statistics, error packets, and collisions:
VLANs¶
Add a virtual interface with tagged VLAN 101 to eth0:
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.
Some useful options:
-t: Increase the duration of the test.
Test network speed with netperf¶
Run a realistic TCP throughput test in both directions.
-
https://github.com/HewlettPackard/netperf
-
Start
netserveron one host. - Execute the script on the client:
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:
Bridges¶
- Create a bridge:
- Assign interfaces to the bridge:
- Display the bridge members, ports, and MAC addresses:
Capture packets¶
tcpdump: A classic tool for packet captures.
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):
Network Layer¶
Show IP addresses. This is the short command for ip address show:
Show statistics (tx, rx, error, dropped):
Fixed IP address¶
- Remove all IP addresses from an interface:
- Assign an IP address to an interface. Include the subnet mask:
- 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:
This command releases the current IP and terminates the daemon. If you cannot request a new IP, kill the process using:
To request a new IP on any interface:
To request a new IP on a specific interface:
To view more details about IP assignment:
ARP Table¶
The traditional arp command is now centralized in the ip utility with the ip neigh option.
To view the current ARP table:
To clear the ARP table:
To add a permanent ARP entry:
To view only reachable entries:
Routing¶
View the current routes with ip route show:
Delete all routes with ip route flush:
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:
Add a default gateway:
Delete the default gateway:
Add a static route:
Enable the forwarding bit to act as a router:
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:
To resolve names using DNS servers, check the contents of the /etc/resolv.conf file:
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:
IP masquerading¶
Masquerade all outbound internet traffic via eth0.
Check iptables:
Connectivity tests¶
Check ARP protocol responses:
Ping indefinitely (press Ctrl + C to stop):
Limit the number of pings:
fping: Ping multiple devices on a network:
nmap
Ping scan:
Port 22 scanTraceroute and IP geolocation¶
traceroute: A historical tool for discovering network routes.
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:
Scan a specific port:
Scan a range of ports:
Scan ports even if the host does not respond to ping by adding the -PN option:
Scan UDP ports:
netstat¶
Display the number of listening TCP and UDP ports, along with the associated program:
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: