Linux:Network

From Cheatsheet
Jump to navigationJump to search


Checks

Common

# List route table
route -n

# Display network connections and current states
netstat

# Check listening ports, connected remote IPs, processes, states and more
netstat -taupen

# Check listening ports and IPs of the local server
netstat -tulpn

# List the routing table
netstat -r

# List verbose common TCP and ICMP information
netstat -s

# List iptable rules (Nftables)
iptables -nvL

# List iptable rules (Legacy iptables)
iptables-legacy -nvL

# Test specific IP and port combination for connectivity
telnet 172.16.2.1 22

# Wireshark on a specific interface to a file, listening on a local port and for a remote IP
tshark -p -i bond0 -w file.pcap -f "port 443 and host 172.16.16.25"

# List available routers
ip netns

# Show interfaces with an IPv4 address
ip -4 a

# Show interfaces with an IPv6 address
ip -6 a

Network

NetworkManager

nmtui is a GUI-tool for managing NetworkManager connections.

Checks

# Show all active network connections
nmcli connection show

# Show connection information for interface ens5
nmcli connection show ens5

# Show active and unactive network connections
nmcli dev status

nmcli

# Bring logical interface ens6 up
nmcli device up ens6

# Turn off DHCP
nmcli con mod ens6 ipv4.method manual
nmcli con mod ens6 connection.autoconnect yes

# Add an IP-address to interface ens6
nmcli connection modify ens6 ipv4.address "192.168.0.10/24"

# Add DNS-servers to interface ens6
nmcli connection modify ens6 ipv4.dns "8.8.8.8,1.1.1.1,196.168.0.1"

# Add a gateway to interface ens6
nmcli con mod ens6 ipv4.gateway "192.168.0.1"

# Add a default route to interface ens160
nmcli connection modify ens160 +ipv4.routes "0.0.0.0/0 192.168.3.100"

# Remove an IP-address from interface ens6
nmcli con mod ens6 -ipv4.addresses 192.168.0.11/24

# Apply changes to interface ens
nmcli device reapply ens6

RHEL

Generic Interface

BOOTPROTO=static for static address
BOOTPROTO=dhcp for DHCP

# /etc/sysconfig/network-scripts/ifcfg-ens128
DEVICE=ens128
NAME=ens128
HWADDR=ab:cd:ef:gh:ij:kl
UUID=0a8d3485-d512-46da-8225-19f4721813c1
BOOTPROTO=static
STARTMODE=auto
ONBOOT=yes
IPADDR=192.168.10.2
NETMASK=255.255.255.0
GATEWAY=192.168.10.1

Generic VLAN Interface

# /etc/sysconfig/network-scripts/ifcfg-eno2.100
VLAN=yes
TYPE=Vlan
PHYSDEV=eno2
VLAN_ID=100
NAME=eno2.100
BOOTPROTO=static
HWADDR=ab:cd:ef:gh:ij:kl
IPADDR=192.168.100.217
NETMASK=255.255.255.0
STARTMODE=auto
UUID=689cff6f-c750-4db7-936c-234fb80b6018
GATEWAY=192.168.100.1

VLAN Bond interface configuration

Virtual Bond Master
BONDING_OPTS="mode=802.3ad miimon=100"
TYPE=Bond
BONDING_MASTER=yes
PROXY_METHOD=none
BROWSER_ONLY=no
IPV6INIT=no
NAME=bond0
UUID=7bb91614-6ffe-4bdc-9b37-c6e9d37f6987
DEVICE=bond0
ONBOOT=yes
AUTOCONNECT_PRIORITY=9
AUTOCONNECT_RETRIES=0
AUTOCONNECT_SLAVES=yes
MTU=1500
Physical bond Slaves
# /etc/sysconfig/network-scripts/ifcfg-ens1
TYPE=Ethernet
NAME=ens1
UUID=c6a4da43-b84a-44f4-b49f-4bdc717d4238
DEVICE=ens1
ONBOOT=yes
AUTOCONNECT_PRIORITY=9
AUTOCONNECT_RETRIES=0
MASTER_UUID=7bb91614-6ffe-4bdc-9b37-c6e9d37f6987
MASTER=bond0
SLAVE=yes
# /etc/sysconfig/network-scripts/ifcfg-ens2
TYPE=Ethernet
NAME=ens2
UUID=ca09a126-a082-4620-a920-be45269e5d8a
DEVICE=ens2
ONBOOT=yes
AUTOCONNECT_PRIORITY=9
AUTOCONNECT_RETRIES=0
MASTER_UUID=7bb91614-6ffe-4bdc-9b37-c6e9d37f6987
MASTER=bond0
SLAVE=yes
VLAN 100 Interface
# /etc/sysconfig/network-scripts/ifcfg-vlan-bond0.100
VLAN=yes
TYPE=Vlan
PHYSDEV=bond0
VLAN_ID=100
REORDER_HDR=yes
GVRP=no
MVRP=no
HWADDR=
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.100.10
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=vlan-bond0.100
UUID=83b0e31c-9a9f-47da-9dc6-645796bc47aa
ONBOOT=yes
AUTOCONNECT_PRIORITY=9
AUTOCONNECT_RETRIES=0
GATEWAY=192.168.100.1

Ubuntu/Debian

Netplan

# Apply the configuration, but if the dialogue is left unconfirmed, the configuration will be reverted.
netplan try

# Apply the configuration
netplan apply
Generic DCHP interface
network:
    version: 2
    ethernets:
        ens4:
            # Some info about the Interface/why does it exist
            dhcp4: true
            match:
                macaddress: fa:16:3e:aa:bb:cc
            set-name: ens4

Generic DHCP Interfaces, but while ignoring the routes for an Interface and disabling DHCP on the other.

network:
    version: 2
    ethernets:
        ens4:
            # Some info about the Interface/why does it exist
            dhcp4: true
            dhcp4-overrides:
              use-routes: false
            match:
                macaddress: fa:16:3e:aa:bb:cc
            set-name: ens4
        ens5:
            # Some info about the Interface/why does it exist
            dhcp4: no
            match:
                macaddress: fa:16:cc:dd:ee
            set-name: ens5
Generic static interface

You may have to disable automatic network-configuration:

sudo bash -c 'echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg'
network:
    version: 2
    ethernets:
        ens7:
           addresses:
              - 192.168.0.23/24
           match:
               macaddress: ab:cd:ef:gh:ij:kl
           mtu: 1500
           set-name: ens7
           nameservers:
               addresses: [1.1.1.1, 8.8.8.8]
           routes:
              - to: default
                via: 192.168.0.1
VLAN Interface
network:
    version: 2
    ethernets:
        eno1: {}
    vlans:
        eno1.10:
            id: 10
            link: eno1
            addresses: [192.168.1.1/24]
        eno1.20:
            id: 20
            link: eno1
            addresses: [192.168.2.1/24]
            nameservers:
              addresses:
                - 1.1.1.1
                - 8.8.8.8
              search: []
            routes:
              - to: default
                via: 192.168.2.1
        eno1.30:
            id: 30
            link: eno1
            addresses: [192.168.3.1/24]
Empty Interface
network:
  version: 2
  ethernets:
    eno2:
      dhcp4: false
      dhcp6: false

Interface files

Classic /etc/network/interfaces.d files i.e. /etc/network/interfaces.d/ens200.conf Otherwise use /etc/network/interfaces

Generic IPv4
# /etc/network/interfaces.d/ens160.conf
auto ens160
iface ens160 inet static
    address 192.168.23.7
    netmask 255.255.255.0
    gateway 192.168.23.1
Generic IPv6
# /etc/network/interfaces.d/ens3.conf
iface ens3 inet6 static
    address abcd:defg:0:1234:5123:abcd:abcd:1234
    netmask 48
    gateway abcd:defg::1
Bond
auto eno1
iface eno1 inet manual

auto eno2
iface eno2 inet manual

auto bond0
iface bond0 inet static
        address 192.168.39.245
        gateway 192.168.39.254
        network 255.255.255.0
        bond-slaves eno1 eno2
        bond-miimon 100
        bond-mode 802.3ad
        bond-xmit-hash-policy layer2+3