Networking notes

Revision as of 21:23, 24 May 2014 by Root (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Set static IP

These settings are lost on reboot. This is just for a temporary config. You may or may not have to set the default route. Note that the output of ip route show is the same argument format used for ip route add or ip route change. You must delete any existing default route before using ip route add or use ip route change.

ip link show dev eth0
ip link set dev eth0 down
ip route show
ip route del default
ip route add default via dev eth0 proto static
ip addr show
ip addr del dev eth0
ip addr add dev eth0
ip link set dev eth0 up

Note that the old fashioned way of assigning an IP address to an interface would use the ifconfig command, but everything can be done through the iproute2 interface (the ip command). See Linux Advanced Routing & Traffic Control The iproute2 interface allows access to all Linux networking through a single, consistent interface.

ifconfig eth0 netmask

Add Virtual Interfaces

Adding virtual interfaces is easy in Linux. Just add a colon and an integer to a real interface name and configure if as if it already existed; you don't have to create it first. In this example, assume 'eth0' is the real interface name and use 1 for the virtual interface integer. In other words, configure 'eth0:1'. You can pick any integer as long as it is not already used.

ifconfig eth0:1

The netmask defaults to

Delete a virtual interface

Set the link down on the virtual interface to delete it:

ifconfig eth0:1 down

Configure persistent network settings

This describes persistent Linux network interface settings. That is, settings that will be restored after a reboot. Different distributions of Linux do this differently. This first shows the Debian/Ubuntu way followed by the RedHat/CentOS way. After you make changes you will need to restart networking to make the changes active:

/etc/init.d/networking restart


Edit the file:


Edit the section for your primary network interface. Example for setting up

Static IP address:

auto eth0
iface eth0 inet static

DHCP assigned IP address:

auto eth0
iface eth0 inet dhcp

Restart the network layer:

/etc/init.d/networking restart


All network config files are in this directory:


Each interface will have its own file named after the infterface:


The contents of a minimal ifcfg-eth0 file looks like this (GATEWAY may not be needed if you are just setting up a LAN between a few machines):


You need to restart the network system to have the new settings take effect:

 # service network restart


You don't need to worry about the broadcast address (or Bcast). By default, it is set to the interface address bitwise OR'ed with the inverse of the netmask.

how to change the interface name for a new network device using udev

Ubuntu uses udev to keep device names consistent between each boot or device hot-swap. It keeps track of the MAC address of the device and matches any previously seen MAC address with a previously assigned interface name. If it has never seen the new MAC address before then the device is assigned a new interface name and that MAC-to-ifname mapping is recorded for later.

Sometimes you replace a network card or swap out the entire motherboard; you never intend to plug in the old device again and you want the new device to take on the old interface name.

Ubuntu keeps track of the mappings in this file: /etc/udev/rules.d/70-persistent-net.rules You may edit this file, but be sure to obey the comment in the file, "# You can modify it, as long as you keep each rule on a single line."

# PCI device 0x14e4:0x164c (bnx2)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:22:19:b7:a5:42", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

Set the NAME parameter; write the file; then run '...TODO...'.

routing the route

add a route

This can also be done with the ip2 command:

route add default gw

display routing table

On Linux you can use `netstat -rn` or `route` or `ip route`.

I always forget this when I work on a BSD machine. I've got some kind of mental block against this:

netstat -rn


The `ip link show` command will display flags associated with each interface. One that used to bug me was 'LOWER_UP'. What the hell? It wasn't documented anywhere in the iproute2 tools. Eventually I heard from word of mouth that it was the physical layer link flag; meaning, if LOWER_UP was set then your Ethernet cable was plugged in and connected to a network. I finally went through the source and found the definition in <linux/if.h>, see IFF_LOWER_UP.


See also Port_to_PID.

The most used form of netstat is netstat -apnut. This show listening and non-listening ports. It shows the program and PID that has the socket. It turns off all name resolution.

This shows processes listening and established connections on any network ports.

netstat --all --program --numeric --udp --tcp
# short form
netstat -apnut

If you want to include UNIX domain sockets use this:

netstat --all --program --numeric

You may also want to add the --extend option to see the user and inode associated with the processing listening on a port.

For OpenBSD you can use this:

netstat -an