Network Configuration in Linux

↓ Contents ↓
Introduction
Get a list of network interfaces (ip a)
Set a static IP
Enable (up) / disable (down) the network interface
Change the gateway
Change the netmask
Listening ports
iftop

Introduction

Network Configuration is one of the most frequent tasks of a Linux administrator

For network configuration in Red Hat, CentOS and other .rpm distributions read here

Network Configuration

You can examine the file listing the interfaces in use with the command

vi /etc/network/interfaces

Further configuration depends on the version of Linux you are using.

On newer versions of Debian . For example, Debian 9 uses Iproute2

If you need to configure the network on an old version of Debian using the utilities ifconfig scroll the page down or follow the link

Network Configuration with Iproute2

Relevant for Debian 9 and above.

To get a list of interfaces you need to use the command

ip a

The output will be something like

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 30:f9:ed:d9:ea:d3 brd ff:ff:ff:ff:ff:ff inet 192.0.2.131/24 brd 192.0.2.255 scope global enp3s0 valid_lft forever preferred_lft forever inet6 fe80::32f9:edff:fed9:ead3/64 scope link valid_lft forever preferred_lft forever 3: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether b8:76:3f:f5:c4:1f brd ff:ff:ff:ff:ff:ff inet 192.168.43.4/24 brd 192.168.43.255 scope global dynamic noprefixroute wlp1s0 valid_lft 2800sec preferred_lft 2800sec inet6 fe80::ba76:3fff:fef5:c41f/64 scope link noprefixroute valid_lft forever preferred_lft forever

Interfaces used to be called eth0, eth1 and so on; now they have more varied names.



1: The first interface is lo it points to the local machine and has the IP 127.0.0.1/8



2: The second interface enp3s0 is my Ethernet connection to another PC.

In this example a computer with Debian (1) is connected to a computer with Windows (2) directly via an Ethernet cable

Through this connection I log in to computer number 1 over SSH.

You can see the IP of the Debian computer above 192.0.2.131/24 , and the Windows computer has the IP 192.0.2.130/24 of course, it is not visible here.

Read how to configure this IP further



3: The third interface wlp1s0 is the connection to the local Wi-Fi from a mobile phone. The IP is obtained dynamically 192.168.43.4/24

How to set a static IP

To connect to another computer via Ethernet you need to set the adapter parameters through the control panel in Windows. The same had to be done in Linux.

To do this you need to run the command

sudo vi /etc/network/interfaces

And add there

auto enp3s0 iface enp3s0 inet static address 192.0.2.131 netmask 255.255.255.0 dns-nameservers 8.8.8.8,8.8.4.4 auto wlp1s0 iface wlp1s0 inet dhcp

Note that the gateway is not specified.

And restart the network

/etc/init.d/networking restart

or

sudo service networking restart

The second option is easier to remember, but for it to work the service utility must be present

Now if you have SSH installed you can log in to the Linux computer over ssh using Putty or MobaXterm

If you need to install SSH, execute

sudo apt install openssh-server

sudo apt-get install openssh-client

ENABLE the interface enp0s25:

ip link set dev enp0s25 up

DISABLE the interface enp0s25:

ip link set dev enp0s25 down

Change the Gateway

Deleting the old gateway

route delete default

Adding a new gateway

route add default gw IP_Address

If there are many old gateways, you need to specify which exact IP should be deleted from which interface

route delete default gw 10.0.0.1 eth0

For creation you can also specify concrete data

route add default gw 192.168.0.1 eth0

🔝

Change the netmask

Find out your mask

ip r

default via 10.6.0.1 dev eth0
10.6.0.0/8 dev eth0 src 10.6.1.68

Delete the old one

ip address del 10.6.1.100/8 dev eth0

Add a new one

ip address add 10.6.1.68/22 dev eth0

Listening ports

lsof

The lsof utility allows you to view all open connections in the system.

To examine specifically network connections, use the option i

To display ports rather than network service names, apply the option P

lsof -i -P

ss

The ss utility can also answer the question of how to view the list of open ports

ss -lntup

If you need to check whether a specific port is in use, for example 5000

ss -na | grep :5000

If the output is empty, the port is not in use

To find out which program is listening on a specific port, you first need to install one of the tools:

netstat, fuser or lsof

Let us start with netstat

Netstat

sudo apt install net-tools

Using the command netstat you can view the list of active network connections

netstat

Usually netstat is used with the set of options tulpn which allows you to understand which program is listening on which port.

netstat -tulpn

(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN - tcp6 0 0 :::22 :::* LISTEN - tcp6 0 0 ::1:631 :::* LISTEN - tcp6 0 0 ::1:6010 :::* LISTEN - tcp6 0 0 ::1:6011 :::* LISTEN - tcp6 0 0 :::80 :::* LISTEN - udp 0 0 0.0.0.0:52756 0.0.0.0:* - udp 0 0 0.0.0.0:68 0.0.0.0:* - udp 0 0 0.0.0.0:631 0.0.0.0:* - udp 0 0 0.0.0.0:5353 0.0.0.0:* - udp6 0 0 :::45815 :::* - udp6 0 0 :::5353 :::* -

Network Configuration with ifconfig

You can get information about the available interfaces on old versions of Debian with the command

ifconfig

Study the output of this command and see which interface is available to you. Usually it is eth0. , if you need to obtain an ip automatically

iface eth0 inet dhcp

If you need to set a static ip

iface eth0 inet static
address 192.168.1.1
netmask 255.255.255.0

For the changes to take effect you need to run

ifup eth0

Turn off wi-fi

ifconfig wlan0 down

Turn the network on/off

ifconfig eth0 up/down

Restart/stop/start the network services

/etc/init.d/networking restart/stop/start

DNS can be configured through the file /etc/resolv.conf

nameserver 8.8.8.8

During the installation you needed to remember which network interfaces Debian offers you to configure.

In old versions of Debian this was simple, they were called eth0, eth1 and so on.

Therefore, to bring up a network interface, in the command above you need to change 0 to 1, and so on.

In new versions of Debian and in other Unix-like distributions, for example OpenBSD network interfaces have more varied names (msk0, sk0, etc.)

To avoid difficulties with this, it is useful to write down what is offered for configuration during the distribution installation.

Enable automatic ip acquisition

dhclient eth0

You can disable automatic ip acquisition by pulling out the network cable and plugging it back in. They say that it should reset.

I recommend running

man interfaces

and studying the output.

iftop

You can monitor the volume of incoming and outgoing traffic on a specific interface with the command

iftop -i eth0

Article author: Andrei Olegovich