Debian (Ubuntu): configuring a network card (or how to set an IP address)

Practice



Network interfaces

Debian names network cards using the name eth followed by a number. Virtual interfaces (loopback, VPN, etc.) are named separately by Debian, also with a name (for example, tun, tap) and a number after it.

For example, if you have 2 network cards in your computer, they will be named eth0 and eth1 by default.

Note that this operating system permanently binds the MAC address of the physical network interface (network card) to the ethX number. So, if you had a network card in your computer named eth0 and you replaced it, then after booting up post-replacement you won't see the expected eth0, but rather eth1.

As a specific but illustrative case: a server with two network interfaces, eth0 and eth1. One of the network cards burns out and the administrator has to replace it. After starting the server, he never gets a ping response from that network interface. Logging in via the console - what does he see? He sees the old second network card named eth1, and the new one, but not named eth0 as expected, rather named eth2! This is because Debian tied a specific interface with a specific MAC address to the eth0 name.

You can view the bound cards and their MAC addresses at:

/etc/udev/rules.d/70-persistent-net.rules

By editing this file you can remove old network cards from Debian's list.


IP addresses

Persistent settings are stored in the file /etc/network/interfaces

Here's an example file:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.0.240

This file defines the IPv4 address 192.168.0.1 on network card eth0. It also defines the default gateway 192.168.0.250. The dns-nameservers option is left in in case the file /etc/resolv.conf lacks DNS server information.

Note that modern Debian doesn't need the network and broadcast lines, calculating them perfectly well on its own from the IP address and mask. However, if you want to override these values, you'll need to specify those options.

Here's an example of this file for two network cards:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.0.240

auto eth1
iface eth1 inet static
address 192.168.100.1
netmask 255.255.255.0
network 192.168.100.0
broadcast 192.168.100.255


You can change the IP address values on the fly with the ifconfig utility:

# ifconfig eth0 inet 192.168.0.33 netmask 255.255.255.0

This command will change the IPv4 address of network card eth0 to 192.168.0.33 with a /24 mask.


If network settings need to be obtained via DHCP - specify this in the interfaces file, for example:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Computer networks"

Terms: Computer networks