Skip to content

Instantly share code, notes, and snippets.

@the0neWhoKnocks
Last active December 8, 2022 02:18
Show Gist options
  • Save the0neWhoKnocks/30a48256b1bb8b53b34d9f41a198769e to your computer and use it in GitHub Desktop.
Save the0neWhoKnocks/30a48256b1bb8b53b34d9f41a198769e to your computer and use it in GitHub Desktop.
Networking

Networking


Commands

Command Description
arp, arping

Arp manipulates or displays the kernel's IPv4 network neighbour cache. It can add entries to the table, delete one or display the current content.

ARP stands for Address Resolution Protocol, which is used to find the MAC (Media Access Control) address of a network neighbor for a given IPv4 Address.

ifconfig

Ifconfig is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.

If no arguments are given, ifconfig displays the status of the currently active interfaces. If a single interface argument is given, it displays the status of the given interface only; if a single -a argument is given, it displays the status of all interfaces, even those that are down. Otherwise, it configures an interface.

ip Show/manipulate routing, network devices, interfaces and tunnels.
netstat Netstat prints information about the Linux networking subsystem.
route

Route manipulates the kernel's IP routing tables. Its primary use is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program.

When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.

Old command (Deprecated) New command Description
ifconfig -a ip a List all interfaces (enabled and disabled)
ifconfig <INT_NAME> down ip link set <INT_NAME> down Disable interface
ifconfig <INT_NAME> up ip link set <INT_NAME> up Enable interface
ifconfig <INT_NAME> <IP>
ifconfig <INT_NAME> netmask 255.255.255.0
ifconfig enp6s0:0 192.168.2.25
ip addr add <IP>/24 dev <INT_NAME> Assign an IP, netmask, or alias to an interface. In the case of ip /24 is the netmask and is required. dev stands for 'device'. There doesn't seem to be an equivalent aliasing function for ip.
ifconfig enp6s0 mtu 9000 ip link set enp6s0 mtu 9000 Set the MTU (Max Transfer Unit) of an interface. In most cases, the optimal value (for a home Internet router) is going to be 1500.
netstat ss Print the active sockets of all configured address families.
netstat -tulpn ss -tulpn Print tcp, udp, listening, program, and numeric items.
netstat -neopa ss -neopa Print numeric, extend, timers, program, all items.
netstat -g ip maddr Print groups
route ip r Displays contents of the routing tables.
route add -net <IP> netmask 255.255.255.0 dev <INT_NAME> ip route add <IP>/24 dev <INT_NAME> Add a new route to a specific interface.
route add default gw <IP> ip route add default via <IP> Adds a default route (which will be used if no other route matches).
arp -a ip neigh Use alternate BSD style output format (with no fixed columns).
arp -v ip -s neigh Display more verbose output.
arp -s <IP> <HARDWARE_ADDR> ip neigh add <IP> lladdr <HARDWARE_ADDR> dev <INT_NAME> Add an entry
arp -i <INT_NAME> -d <IP> ip neigh del <IP> dev <INT_NAME> Delete an entry

For Virtual Machines

Check what networks exist (note that without sudo you may get different results)

sudo virsh net-list --all

Start or auto-start a network

# start
sudo virsh net-start <NAME>

# auto start
sudo virsh net-autostart <NAME>

The default network will start up a couple interfaces which can be viewed by running ifconfig -a.

virbr0
virbr0-nic

dnsmasq was also started. The config for the default network is in /var/lib/libvirt/dnsmasq/default.conf. You can edit the network with

sudo virsh net-edit default

It may prompt to choose your editor. For sudo commands you'll need to sudo select-editor.

Manually Create a Bridge Interface

Note that steps 1, 2, and 5 are only neccessary if you currently have an Ethernet cable plugged in.

  1. Ensure your current connection is disabled/reset.
    # check interfaces (display output with color)
    ip -c a
    
    # disable interface
    sudo ip link set <INT_NAME> down
    
    # verify it's down
    ip a | grep <INT_NAME> | grep -E 'state [A-Z]+'
    • Using ip -c a will output results in color which is useful most of the time, but when using grep you won't get the expected results.
  2. Remove the assigned IP from the interface
    # get the 'inet' IP from
    ip addr show dev <INT_NAME>
    
    # delete IP
    sudo ip addr del <IP> dev <INT_NAME>
    
    # verify no 'inet' is assigned
    ip addr show dev <INT_NAME> | grep -o -E 'inet [^ ]+'
    • This command allows for getting the IP programatically
      ip addr show dev <INT_NAME> | grep -o -E 'inet [^ ]+' | awk -v col=2 '{print $col}'
  3. Disable Network daemons
    # check if running (common for Desktop environments)
    sudo systemctl status NetworkManager | grep 'Active: ' 
    # if so disable
    sudo systemctl stop NetworkManager
    
    # check if running (common in Server environments)
    sudo systemctl status systemd-networkd | grep 'Active: '
    # if so disable
    sudo systemctl stop systemd-networkd
  4. Create Bridge interface
    # A common convention for the name would be 'br0'
    sudo ip link add name <BR_INT_NAME> type bridge
    
    # bind the Ethernet interface to the Bridge (something like 'eth0' master 'br0')
    sudo ip link set <ETH_INT_NAME> master <BR_INT_NAME>
    
    # Assign an IP to the device (check that the IP isn't already assigned).
    # 'brd' is the Broadcast range in which it'll accept requests (basically all)
    sudo ip addr add <IP>/16 dev <BR_INT_NAME> brd 198.168.255.255
    • Refer to the CIDR table for common subnets. Helps determine whether to use /16, /24, or something custom.
  5. Bring interfaces back up
    sudo ip link set up <ETH_INT_NAME>
    sudo ip link set up <BR_INT_NAME>
  6. Try to ping devices
    ping <IP> # IP of device on Local network should work
    ping google.com # should fail (for now)
    • If you run route you'll see something similar to below, and that there's no default gateway set up. Which means that anything within the 192.168.#.# range will be accepted, and everything else will be dropped.
      Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 br0
      
      It's similar to a Server's middleware or routing accept in reverse. For a Server, generally the routes/middleware are processed in numerical order, so top to bottom (add it first, processed first). Kernel routes are proccessed in reverse, so bottom to top.
  7. Add a Default Gateway
    sudo route add default gw 192.168.1.1
    • If you run the route command again you should now have something like
      Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      default         _gateway        0.0.0.0         UG    0      0        0 br0
      192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 br0
      
      Note that _gateway is just an internal alias for the IP you originally entered.
    • You should now be able to run ping google.com and get a result.

Automate Creation of Bridge Interface

If you went through the manual steps, run the below to remove that stuff

sudo ip link delete <BR_INT_NAME>
sudo ip link set <ETH_INT_NAME> down
  1. Switch to root User
    sudo -i
  2. Go to folder where custom configs will live
    cd /etc/systemd/network
  3. Create custom configs
    • vim br.netdev (Create Bridge interface)
      [NetDev]
      Name=br0
      Kind=bridge
      
    • vim 1-br0-bind.network (Bind Ethernet to Bridge)
      [Match]
      Name=en0
      
      [Network]
      Bridge=br0
      
    • vim 2-br0-dhcp.network (Call DHCP to give Bridge an IP)
      [Match]
      Name=br0
      
      [Network]
      DHCP=ipv4
      
  4. Enable Daemon - there should only be one Network daemon running
    sudo systemctl enable systemd-networkd
    sudo systemctl start systemd-networkd
    
    # if it was previously stopped while in the middle of an operation, a restart may be required
    sudo systemctl restart systemd-networkd
  5. Running ip a should now display that the Bridge was set up and the Ethernet is connected to it.

CIDR table

Prefix size Network mask Usable hosts per subnet
/1128.0.0.02,147,483,646
/2192.0.0.01,073,741,822
/3224.0.0.0536,870,910
/4240.0.0.0268,435,454
/5248.0.0.0134,217,726
/6252.0.0.067,108,862
/7254.0.0.033,554,430
Class A
/8255.0.0.016,777,214
/9255.128.0.08,388,606
/10255.192.0.04,194,302
/11255.224.0.02,097,150
/12255.240.0.01,048,574
/13255.248.0.0524,286
/14255.252.0.0262,142
/15255.254.0.0131,070
Class B
/16255.255.0.065,534
/17255.255.128.032,766
/18255.255.192.016,382
/19255.255.224.08,190
/20255.255.240.04,094
/21255.255.248.02,046
/22255.255.252.01,022
/23255.255.254.0510
Class C
/24255.255.255.0254
/25255.255.255.128126
/26255.255.255.19262
/27255.255.255.22430
/28255.255.255.24014
/29255.255.255.2486
/30255.255.255.2522
/31255.255.255.2540
/32255.255.255.2550

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment