Network Configuration in Ubuntu
To better navigate network administration, namely such concepts as types of IP addresses, masks, subnets, etc. I advise you to study the article
«Basics of computer networks»
Introduction
Setting up a network connection is a crucial step when working with Ubuntu, and it can raise
many questions for a beginner.
In this article you can find answers to some of them.
nmcli
nmcli conn show
NAME UUID TYPE DEVICE k3-5-bw4-udp-1196 754bf232-b384-4e41-944f-00b37e781ea3 vpn wlp0s20f3 NetGuest 09a61c52-6da7-4947-bcce-bd928c72ec04 wifi wlp0s20f3 tun0 749f87c0-2e48-48d6-a8b9-924e120b4e22 tun tun0 br-cb9e77c3b2f2 744bd9ed-dc76-4632-9bb7-47605e47d16e bridge br-cb9e77c3b2f2 docker0 717d7ef5-2bcf-493e-af17-ea858a83d7ce bridge docker0 Net2.4G cee7ba3b-2835-4383-ae97-70ed50ec6d41 wifi -- NetM 131ea9fd-ca46-4de6-b909-542ae1548894 wifi -- SKYnet 5faa88cd-b6a4-4b8d-aa16-d0c8a62425ea wifi -- S-office 5ca185cc-cd49-4451-99c8-42ae24aef077 wifi -- Wired connection 1 befcb047-1362-33bf-8819-96d783e30df5 ethernet --
ip
To get a list of interfaces you need to use the command
ip a
The output will look 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
Previously interfaces were named eth0, eth1 and so on, now they have more varied names.
1: The first interface is
lo
it is directed at 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 the computer with Ubuntu(1) is connected to a computer with
Windows
(2) directly through an Ethernet cable
Through this connection I access computer number 1 over SSH.
You can see the IP of the Ubuntu computer above
192.0.2.131/24
, and the Windows PC has the IP
192.0.2.130/24
it is, of course, not visible here.
To find out how to configure this IP read
further
3: The third interface
wlp1s0
is the connection to a local Wi-Fi from a mobile phone.
IP is obtained dynamically
192.168.43.4/24
DHCP
If the IP needs to be obtained automatically
iface eth0 inet dhcp
Static IP
First you need to find out the interface names
ip a
Then you can use the ip utility or configure it through netplan
ip
Example of assigning the IP address 10.0.2.16/24 to the interface enp0s31f6
sudo ip addr add 10.0.2.16/24 dev enp0s31f6
netplan
You need to go into the directory
/etc/netplan/
and see what the configuration file is called
cd /etc/netplan
ls -la
Let us edit the file 01-network-manager-all.yaml
sudo vi 01-network-manager-all.yaml
The most important thing is to watch the indentation. For example, the - before the IP must be exactly
under the second letter d in the word addresses.
Indentation must be made with a space, not a tabulation
network: version: 2 renderer: networkd ethernets: ens32: addresses: - 172.16.120.74/16 nameservers: addresses: [127.0.0.53, 172.16.1.5]
nameservers: are not always needed, you can first leave them out and see whether everything suits you.
You can apply the new settings with the command
sudo netplan apply
You can check the result by running ip a again
ip a
1: lo:
ENABLE the interface enp0s25:
ip link set dev enp0s25 up
DISABLE the interface enp0s25:
ip link set dev enp0s25 down
Change Gateway
Removing 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 particular IP to delete 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 netmask
You can learn what a netmask or subnet mask is in the article
«Networks»
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
connections open in the system.
To study 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 certain 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 certain port you need to first install one of the
tools:
netstat, fuser or lsof
Let us start with netstat
Netstat
sudo apt install net-tools
With the command netstat you can view the list of active network connections
netstat
Usually netstat is used with the set of options tulpn which lets you 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 :::* -
rsyslog
It is often necessary to watch the access logs in the file
auth.log
which are located at /var/log/auth.log
This log is handled by
rsyslog
so you first need to check whether this service is installed.
If not, install it with the command
apt-get install rsyslog
Go to /etc/init.d/ and start rsyslog with the command
service start rsyslog
You can stop, restart and check the status with the commands
service stop rsyslog service restart rsyslog service status rsyslog
SSH
To connect to a remote machine over SSH you need, for example, to know
the machine IP, the user name, the password
Connection example:
ssh andrei@192.168.0.2
If you connect for the first time, you will get a warning
The authenticity of host '192.168.0.2 (192.168.0.2)' can't be established. ECDSA key fingerprint is SHA256:ABCDE/FGHIJKLMNOPQRSTUVWXYZ123456789ABCDEFG. Are you sure you want to continue connecting (yes/no/[fingerprint])?
This warning is displayed because in the file
known_hosts
, which is responsible for
known hosts, there is no record of 192.168.0.2
The file
known_hosts
is located in the directory
/home/$(whoami)/.ssh
It contains lines of this kind:
|1|abcdefghijklmnopqrstuvwxyz1=|abcdefghijklmnopqrstuvwxyz1= ecdsa-sha2-nistp256 ABCDEFGHJKLMNOPQRSTUVWXYZABCDEFGHJKLMNOPQRSTUVWXYZABCDEFGHJKLMNOP+ABCDEFGHJKLMNOPQRS/ABCDEFGHJKLMNOPQRSTUVWXYZABCDEFGHJKLMNOPQRSTUVWXYZABCD=
After you choose yes you will see the message
Warning: Permanently added '192.168.0.2' (ECDSA) to the list of known hosts. ssh_dispatch_run_fatal: Connection to 192.168.0.2 port 22: Broken pipe
And in the file known_hosts a new record will appear
If you are connecting to the host not for the first time
ssh andrei@192.168.0.2
You will need to enter the password
andrei@192.168.0.2's password:
After a successful entry a message about the last login may appear
Last login: Wed Nov 11 17:55:40 2020
traceroute
To install traceroute execute
sudo apt-get install traceroute
Article author: Andrei Olegovich