Network Configuration from command line in Ubuntu 16.04 LTS

by Remy Pereira on 14th February 2018

Ubuntu 16.04 LTS comes with graphical utilities for network configuration. For desktops you may install network-manager and network-manager-gnome packages and just use the nm-connection-editor utility. However you may not want to use these utilities on your server. This article focusses on how to configure networking from command line interface.

List all the interfaces

You can use the ip command to list interfaces.

# ip link show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:9c:e3:5c brd ff:ff:ff:ff:ff:ff
3: enp0s8:  mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether 08:00:27:10:9b:3f brd ff:ff:ff:ff:ff:ff

Configure DHCP on an interface

Let's say we want to configure DHCP on interface enp0s8. Edit the file /etc/network/interfaces and add the following lines.

#Configuration for interface enp0s8
auto enp0s8
iface enp0s8 inet dhcp

Restart networking service.

# systemctl restart networking

Configure Static IP on an interface

Let's say we want to configure static IP in interface enp0s3. Edit /etc/network/interfaces and add the following lines.

# Configuration for interface enp0s3
auto enp0s3
iface enp0s3 inet static
address 192.168.1.151
netmask 255.255.255.0

Optionally you may also add gateway, DNS-nameservers as well. Finally restart networking service.

# systemctl restart networking

You can verify your configuration using the ip command.

# ip addr

The ip command can also be used for setting IP addresses, but the changes are not permanent. For permanent change you have to edit /etc/network/interfaces and restart networking.


Post a comment

Comments

Durga Charan Ojha | December 23, 2019 10:37 AM |

Yes, it worked !!!