Intro

This post is something like my personal cheatsheet for Linux networking. But maybe usefull also for somebody else…
It is intentionally practical and command-focused, written mainly for people who already understand networking concepts (IP, routing, VLANs, VRFs) and want a quick Linux reference.

Although the main topic is networking, I start with package management…

For almost all commands we will need root rights

System upgrade

Distro command
Alpine apk update && apk upgrade
Ubuntu apt update && apt full-upgrade
Debian apt update && apt full-upgrade
Rocky dnf upgrade --refresh

Install useful networking packages

Distro command
Alpine apk add iproute2 net-tools
Ubuntu apt install net-tools
Debian apt install net-tools
Rocky dnf install net-tools

net-tools (ifconfig, arp, netstat) is deprecated but still useful


Basic networking

Check curent IP addresses on all interfaces

Distro command
All ip a

Check routing table

Distro command
All ip route

Check arp table

Distro command
All arp

Set temporrary IP address

Let’s continue with basic setup.

It is important, that setup will does not survive system reboot .
If given interface already has an IP, new one will be added as secondary.

Distro command
All ip addr add 10.208.116.181/24 dev eth0

Command is the same for all distros, just Name of interfaces will change:

Distro interface
Alpine eth0
Ubuntu ens192
Debian ens192
Rocky ens33

This will apply for all next commands - I’ll use eth0, Do not forget to use correct interface name


Add temporrary IP route

Distro command
All ip route add 10.200.200.0/24 via 10.208.116.10 dev eth0

Delete IP route

Will not survive system reboot.

Distro command
All ip route del 10.200.200.0/24 via 10.208.116.10 dev eth0

Delete all IP addresses on given interface and request new IP over DHCP

Distro command
Alpine ip addr flush dev eth0 && udhcpc -i eth0
Ubuntu ip addr flush dev ens192 && dhcpcd ens192
Debian ip addr flush dev ens192 && /sbin/dhcpcd ens192
Rocky ip addr flush dev ens33 && nmcli device reapply ens33

Shut interface down

Distro command
All ip link set down dev eth0

Bring interface up

Distro command
All ip link set up dev eth0

And now more complicated part - permanent IP setup, which will survive system reboot. Configuration should be writen in specific configuration file. Change can be also enforced instead of reboot after change.

Set permanent IP address and route

Distro config file enforcement command
Alpine /etc/network/interfaces service networking restart
Ubuntu /etc/netplan/01-my_net_config.yaml netplan apply
Debian /etc/network/interfaces systemctl restart networking
Rocky /etc/NetworkManager/system-connections/ens33.nmconnection nmcli connection reload && nmcli connection up ens33

And examples for each distro, Each example contains two interfaces - first for native interface without tagging, second with 802.1q tagging.

Alpine:

# /etc/network/interfaces

auto eth0
iface eth0 inet static
 address 10.208.116.181/24
 gateway 10.208.116.1
 dns-nameservers 8.8.8.8 8.8.4.4
 post-up ip route add 10.200.200.0/24 via 10.208.116.10 dev eth0

auto eth1.116
iface eth1.116 inet static
 address 192.168.2.181/24

Ubuntu:

# /etc/netplan/01-my_net_config.yaml
network:
    ethernets:
        ens192:
            dhcp4: false
            addresses: [10.208.116.182/24]
            routes:
            - to: default
              via: 10.208.116.1
            - to: 10.200.200.0/24
              via: 10.208.116.10
            nameservers:
                addresses: [8.8.8.8,8.8.4.4]
        ens224:
            dhcp4: false

    vlans:
        ens224.116:
            id: 116
            link: ens224
            addresses: [192.168.2.182/24]

Debian:

# /etc/network/interfaces
auto ens192
iface ens192 inet static
 address 10.208.116.183/24
 gateway 10.208.116.1
 post-up ip route add 10.200.200.0/24 via 10.208.116.10 dev ens192

auto ens224.116
iface ens224.116 inet static
 address 192.168.2.183/24

# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

Rocky:
In “Rocky world” it is recommended to use nmcli for creating persistent network config files.

We can delete and recreate native primary interface ens33 with:

nmcli connection delete ens33 && nmcli connection add \
  type ethernet \
  con-name ens33 \
  ifname ens33 \
  ipv4.method manual \
  ipv4.addresses 10.208.116.184/24 \
  ipv4.gateway 10.208.116.1 \
  ipv4.dns "8.8.8.8 8.8.4.4" \
  ipv4.routes "10.200.200.0/24 10.208.116.10"

Or delete/create new subinterface:

nmcli connection delete ens34.116 && nmcli con add type vlan con-name ens34.116 dev ens34 id 116 ip4 192.168.2.184/24

It will efectivelly create new config files:

/etc/NetworkManager/system-connections/ens33.nmconnection /etc/NetworkManager/system-connections/ens34.116.nmconnection


Namespaces

Network namespaces provide full isolation of network stacks:

  • interfaces
  • IP addresses
  • routing tables
  • ARP / neighbor tables
  • firewall rules

Conceptually, a network namespace is very similar to a VRF, but implemented entirely in software.

Creating new namespace is the same for all distros:

ip netns add namespace-1 ip link set lo up

Or delete, if you want to play with it:

ip netns del namespace-1

Interesting thing is, that namespaces cannot be managed through interface’s config files. They should be configured by scripts at boot time.

Please note, that IP addressing in namespace-1 overlaps with addressing of primary native interfaces - and it is ok ;)

Creating new subinterfaces:

Distro command
Alpine ip link add link eth1 name eth1.100 type vlan id 100
Ubuntu ip link add link ens224 name ens224.100 type vlan id 100
Debian ip link add link ens224 name ens224.100 type vlan id 100
Rocky ip link add link ens34 name ens34.100 type vlan id 100

Assign new subinterface to the “VRF”:

Distro command
Alpine ip link set eth1.100 netns namespace-1
Ubuntu ip link set ens224.100 netns namespace-1
Debian ip link set ens224.100 netns namespace-1
Rocky ip link set ens34.100 netns namespace-1

Bring new subinterfaces up in the “VRF”:

Distro command
Alpine ip netns exec namespace-1 ip link set eth1.100 up
Ubuntu ip netns exec namespace-1 ip link set ens224.100 up
Debian ip netns exec namespace-1 ip link set ens224.100 up
Rocky ip netns exec namespace-1 ip link set ens34.100 up

Assign IP in the “VRF”:

Distro command
Alpine ip netns exec namespace-1 ip addr add 10.208.116.201/24 dev eth1.100
Ubuntu ip netns exec namespace-1 ip addr add 10.208.116.202/24 dev ens224.100
Debian ip netns exec namespace-1 ip addr add 10.208.116.203/24 dev ens224.100
Rocky ip netns exec namespace-1 ip addr add 10.208.116.204/24 dev ens34.100

Create default route in the “VRF”:

Distro command
Alpine ip netns exec namespace-1 ip route add default via 10.208.116.1
Ubuntu ip netns exec namespace-1 ip route add default via 10.208.116.1
Debian ip netns exec namespace-1 ip route add default via 10.208.116.1
Rocky ip netns exec namespace-1 ip route add default via 10.208.116.1

And test it:

ip netns exec namespace-1 ping 10.208.116.204

If you are using vSphere and VMs like me it is essential have primary network adapter configured as “VLAN type” and second interface (with 802.1q trunking") as “VLAN trunking”