Linux/Netzwerk/Konfiguration/Setting up an Ethernet Interface

Aus Foxwiki

Setting up an Ethernet Interface

The majority of network setup can be done via the interfaces configuration file at /etc/network/interfaces. Here, you can give your network card an IP address (or use dhcp), set up routing information, configure IP masquerading, set default routes and much more.

Remember to add interfaces that you want brought up at boot time to the 'auto' line.

See man interfaces for more options.


Starting and Stopping Interfaces

Interfaces configured with /etc/network/interfaces can be brought up and down with the ifup and ifdown commands.

Some outdated guides instruct to restart the networking service to apply changes to /etc/network/interfaces, however this was deprecated because it is possible that not all interfaces will be restarted. Instead use ifup and ifdown to apply changes to each interface, for example with an interface named enp7s0:

# ifdown enp7s0
# ifup enp7s0


Reinitialize new network setup

If you make more fundamental network changes e.g. adding new virtual interfaces (e.g. bridge) in /etc/network/interfaces you can reinitialize the network-configuration be restarting the networking daemon:

# systemctl status networking
# systemctl restart networking


Benennung von Netzwerkschnittstellen

Siehe NetworkInterfaceNames. Seit Stretch verwenden neue Systeme standardmäßig keine Schnittstellennamen im alten Stil wie eth0, eth1, wlan0, wlan1 mehr. Das neue System verwendet Namen, die auf dem Hardware-Standort basieren, wie eno0, enp0s31f6, wlp1s7 (oder im Fall von USB-Dongles die MAC-Adresse: enx2c56ac39ec0d).

Sie können Schnittstellen auflisten mit:

ls /sys/class/net

In den folgenden Beispielen wird weiterhin „eth0“ als Standard-Schnittstellenname verwendet, obwohl dieser auf einem modernen System wahrscheinlich nicht existiert.

Upgrade und Netzwerkschnittstellennamen

Stretch und Buster unterstützen weiterhin das alte Benennungssystem, solange die Datei /etc/udev/rules.d/70-local-persistent-net.rules noch vorhanden ist. Benutzern wird jedoch empfohlen, vor dem Upgrade auf Bullseye umzustellen.

Using DHCP to automatically configure the interface

If you're just using DHCP then all you need is something like:

auto eth0
allow-hotplug eth0
iface eth0 inet dhcp

For DHCPv6 (used for IPv6), append also the following iface stanza

iface eth0 inet6 dhcp

Alternatively, IPv6 can be autoconfigured using stateless address autoconfiguration, or SLAAC, which is specified using auto instead of dhcp in the inet6 stanza:

iface eth0 inet6 auto

Also see IPv6PrefixDelegation.


Configuring the interface manually

If you're configuring it manually then something like this will set the default gateway (network, broadcast and gateway are optional):

auto eth0
iface eth0 inet static
address 192.0.2.7/24
gateway 192.0.2.254

If you want to add an IPv6 address, too, append something like:

iface eth0 inet6 static
address 2001:db8::c0ca:1eaf/64
gateway 2001:db8::1ead:ed:beef

See man interfaces for more options.

Make sure to disable all DHCP services, e.g. dhcpcd.

Mixing manual and automatic configuration is also possible, e.g. to use IPv6 SLAAC for internet connectivity and static addresses within the network:

  1. manual unique local address
iface eth0 inet6 static
 address fdc3:cafe::3/64
 # use SLAAC to get global IPv6 address from the router
 # we may not enable ipv6 forwarding, otherwise SLAAC gets disabled
 autoconf 1
 accept_ra 2


Setting the speed and duplex

Autonegotiation repeatedly failing is often a symptom of faulty cabling, so investigate physical matters before assuming that the interfaces' autonegotiation algorithms are incompatible. If you turn off autonegotiation and set speed and duplex manually then the partner interface at the other end of the cable will assume that the absence of autonegotiation indicates a speed of 10Mbps and a duplex of half. For error-free operation if you set speed and duplex manually you must ensure that exactly the same speed and duplex are configured on the partner interface.

If you set your interface's speed and duplex by hand, then some trial and error may be required. Here are the basic steps: * Install the ethtool and net-tools packages, so that you have the ethtool and mii-tool programs. One or both of these might work for your interface.

  • Make sure you have a way to login to the system in case the network interface becomes nonfunctional. An SSH connection could be disrupted, so you should have a fallback strategy.
  • Identify the interface in question (it will often be eth0). Adjust the remainder of these instructions accordingly.
  • Try to determine what its current speed and duplex settings are. This is where it gets fun:
    • As root, try ethtool eth0 first, and see whether the "Speed:" and "Duplex:" lines look valid. If not, the ethtool may not be supported by your device.
    • As root, try mii-tool -v eth0 and see whether its output looks correct. If not, them mii-tool may not be supported by your device.
    • If neither one is supported, you may have to set parameters directly on the kernel driver module. Identify which driver module you're using by reading the output of dmesg and lsmod. You can then try modinfo MODULENAME to see what parameters it accepts, if any. (You can use modinfo even on modules that are not loaded, for comparison.) ToDo: where does one set kernel module parameters?
  • Next, try to change the settings of the interface while it's operating. You'll need to be root, of course. Either:
    • ethtool -s eth0 speed 100 duplex full autoneg off (assuming 100 Mbps and full duplex)
    • mii-tool -F 100baseTx-FD eth0 (same assumption)

In each case, re-check to see whether the interface settings actually changed, and then try sending some data in and out of the system to see whether the NIC is operating correctly.

  • If one of these commands successfully set your NIC, then you can put it into /etc/network/interfaces so it runs when you bring the interface up (e.g. at boot time). However, before you do that, you should understand that some drivers and devices behave differently than others. When the driver module is loaded, the NIC may begin autonegotiation without any way to stop it (particularly with drivers that do not accept parameters). The settings from interfaces are applied at some point after that, which may be right in the middle of the negotiation. So, some people find it necessary to delay the ethtool or mii-tool command by a few seconds. Thus:
    iface eth0 inet static
    address .../...
    gateway ...
    up sleep 5; ethtool -s eth0 ...
    Or the analogous mii-tool command.
  • Reboot the machine to make sure it comes up correctly, and be prepared to intervene manually (e.g. Ctrl-Alt-Del and then boot into single-user mode from GRUB or LILO) if things don't work.


Bringing up an interface without an IP address

To create a network interface without an IP address at all use the manual method and use pre-up and post-down commands to bring the interface up and down.

iface eth0 inet manual
pre-up ifconfig $IFACE up
post-down ifconfig $IFACE down

If the interface is a VLAN interface, the up/down commands must be executed after/before the vlan hooks. (You also have to install the vlan package.)

iface eth0.99 inet manual
post-up ifconfig $IFACE up
pre-down ifconfig $IFACE down

Note: If you create the VLAN interface only to put it into a bridge, there is no need to define the VLAN interface manually. Just configure the bridge, and the VLAN interface will be created automatically when creating the bridge (see below).