How to configure network in Linux (using initscripts/ifcfg/ifupdown, NetworkManager/nmcli, netctl and systemd-networkd)

You can use network managers (nmcli,…), initscripts/systemd or call user-land network command line tools directly (iproute2/net-tools) to configure your network devices either static or dynamic/dhcp.

  • ifup/ifdown@man network interface configuration files used by ifup/ifdown, called by initscripts (used prior to systemd). Works all distros (but configuration file location and syntax changes).
## fedora/rhel
$ cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=myhostname
#static
#GATEWAY=192.168.1.1
$ cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
ONBOOT=yes
#dynamic
BOOTPROTO=dhcp
#static
#BOOTPROTO=static
#IPADDR=192.168.1.2
#NETMASK=255.255.255.0
$ sudo /etc/init.d/network restart

## debian/ubuntu
$ sudo apt-get install ifupdown
$ man interfaces
$ cat /etc/network/interfaces
auto lo
iface lo inet loopback
#dynamic
#auto eth0
#    allow-hotplug eth0
#    iface eth0 inet dhcp
#static
auto eth0
iface eth0 inet static
    address 192.168.1.2
    netmask 255.255.255.0
    gateway 192.168.1.1
$ sudo networking restart

## dns (static)
$ cat /etc/resolv.conf
search mydomain.net
nameserver 10.254.0.121

## manual, non-persistent static using iproute2/net-tools
$ ip link set eth0 up 
$ ip addr add 192.168.1.2/24 broadcast 192.168.1.255 dev eth0
$ ip route add default via 192.168.1.1
$ ifconfig eth0 up
$ ifconfig eth0 192.168.1.2/24 broadcast 192.168.1.255
$ route add default gw 192.168.1.1 eth0

from NetworkConfiguration@debian, networkscripts@rhel and static ip@nixcraft

$ sudo yum install NetworkManager | sudo apt-get install network-manager | sudo pacman -Sy networkmanager

# nmcli manages all interfaces/devices except 'NM_CONTROLLED=no' in '/etc/sysconfig/network-scripts/ifcfg-*'
$ nmcli dev show ; nmcli con show

## unattened add/modify
nmcli con add type ethernet con-name <con-name> ifname <interface-name> ?ip4 <address/netmask> gw4 <defaultgw>?
nmcli con mod <con-name> +|-<setting>.<property> "<value>"
## interactive edit
nmcli con edit <con-name>
> goto ipv4 ; set addresses ip/mask defgw ; set dns ip1 ip2 ; set dns-search domain ; verify ; save persistent ; quit

## static
$ nmcli con mod ens192 ipv4.addresses "10.4.2.108/8 10.254.0.2"
$ nmcli con mod ens192 ipv4.dns "10.254.0.121 10.254.0.122" ipv4.dns-search "mydomain"

## dynamic 
$ nmcli con mod ens192 ipv4.method auto

## hostname
$ nmcli general hostname <myhostname>

$ systemctl restart NetworkManager | service network-manager restart
or
$ systemctl restart network.service | service networking restart

from configure static ip in centos7@xmodulo

  • netctl@arch is a CLI-based tool used to configure and manage network connections via profiles. Arch only.
## install
$ sudo pacman -Sy netctl

## static
$ cp /etc/netctl/examples/ethernet-static /etc/netctl/profile1
$ cat /etc/netctl/profile1
Description='A basic static ethernet connection'
Interface=eth0
Connection=ethernet
IP=static
Address=('192.168.1.23/24' '192.168.1.87/24')
#Routes=('192.168.0.0/24 via 192.168.1.2')
Gateway='192.168.1.1'
DNS=('192.168.1.1')

## dynamic
$ cp /etc/netctl/examples/ethernet-dhcp /etc/netctl/profile1
Description='A basic dhcp ethernet connection'
Interface=eth0
Connection=ethernet
IP=dhcp

$ netctl start profile1

## enable on boot
# either enable by profile; this creates and enables a systemd service on boot
$ netctl enable profile1
# or enable all profiles with eth0, needs 'pacmman -Sy ifplugd'
$ systemctl enable netctl-ifplugd@eth0.service
  • systemd.network as of version 210, systemd supports basic network configuration through udev and networkd.
$ systemctl enable systemd-networkd ; systemctl restart systemd-networkd

## static
$ cat /etc/systemd/network/10-static.network
[Match]
Name=ens32
[Network]
Address=10.4.2.111/8
Gateway=10.254.0.2
DNS=10.254.0.121
DNS=10.254.0.122
Domains=mydomain

## dynamic
$ cat /etc/systemd/network/20-dhcp.network
[Match]
Name=en*
[Network]
DHCP=v4

## hostname
$ hostnamectl set-hostname myhostname

from systemd-networkd@arch and systemd-networkd@coreos

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s