Network Configuration in Linux
| Network Configuration in Debian | |
| Network Configuration in Ubuntu and others | |
| Network Configuration in CentOS/Red Hat… | |
| Network Configuration at Rocky |
| Introduction | |
| Get a list of network interfaces (ip a) | |
| Install static IP | |
| Turn on (up) / Turn off (down) interface | |
| Change the gateway | |
| Change netmas k | |
| Listening ports | |
| iftop |
Introduction
Network Configuration is one of the most common activities of a Linux administrator.
About setting up a network in Red Hat,
CentOS
and other .rpm distributions
read
here
Network Configuration
You can study a file with a list of interfaces involved using commands
vi /etc/network/interfaces
It depends on the version of Linux you are using.
New versions.
Debian
.
For example, on Debian 9 it is used.
Iproute2
If you need to set up a network on the old version of Debian using utilities
ifconfig
scroll down the page or go down
reference
Network Configuration with Iproute2
Relevant for Debian 9 and above.
To get a list of interfaces you need to use command
ip a
There will be something like this coming out.
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 called eth0, eth1 and so on, but now they have more diverse names.
1: The first interface.
lo
It is directed to a local machine and has an IP.
127.0.0.1/8
2: Second interface
enp3s0 This is my Ethernet connection to another PC.
In this example, a computer with Debian (1) is connected to a computer.
on
Windows
(2) Directly via Ethernet cable
Through this connection, I go to SSH computer number 1.
IP computer with Debian you can see above
192.0.2.131/24
And a Windows IP computer.
192.0.2.130/24
Of course you can't see him here.
How to set up this IP read
further
3: Third inteface.
wlp1s0
It's a local Wi-Fi connection from a mobile phone.
IP received dynamically
192.168.43.4/24
How to Install a Static IP
To tap into another computer via Ethernet you need to set in Windows
Adapter parameters through the control panel. The same thing had to be done and
Linux.
To do this, you need to execute command.
sudo vi /etc/network/interfaces
And add it.
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
Please note that the gateway is not listed.
And restart the network.
/etc/init.d/networking restart
or
sudo service networking restart
The second option is easier to remember, but to make it work need to have a service
Now if you have SSH installed, you can go to a computer with Linux on ssh using
Putty
or
MobaXterm
If you want to install SSH execute
sudo apt install openssh-server
sudo apt-get install openssh-client
Insert / Turn off the network interface
Include the enp0s25 interface:
ip link set dev enp0s25 up
Turn off the enp0s25 interface:
ip link set dev enp0s25 down
Change the 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 IP from which interface. remove
route delete default gw 10.0.0.1 eth0
Specific data can also be provided to create
route add default gw 192.168.0.1 eth0
Change netmas k
What is netmask or subnet mask you can find in the article
«Networks»
Know your mask
ip r
default via 10.6.0.1 dev eth0
10.6.0.0/8 dev eth0 src 10.6.1.68
Remove the old one.
ip address del 10.6.1.100/8 dev eth0
Add new
ip address add 10.6.1.68/22 dev eth0
Listening ports
lsof
The lsof utility allows you to see
everything
open connections in the system.
To learn exactly the network connections use the option
i
To display the ports, not the names of network services
P
lsof -i -P
ss
The question of how to view the list of open ports can also be answered by the utility sss
ss -lntup
If you want to check if a specific port is being used, for example 5,000
ss -na | grep :5000
If the output is empty, the port is not used.
To find out which program listens to a particular port, you need to pre-installe one of them.
instruments:
netstat, fuser or lsof
Let's start with netstat.
Netstat
sudo apt install net-tools
With commands. netstat You can see the list of active connections on the network.
netstat
Usually netstat is used with a set of tulpn options. This allows you to know which program is listening to 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 existing interfaces on older versions of Debian using commands.
ifconfig
Study the output of this commands and see what interface is available to you. Usually. eth0. If you need to receive 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
The changes have come into force and must be implemented.
ifup eth0
Turn off wi-fi.
ifconfig wlan0 down
Enable/disable the network
ifconfig eth0 up/down
Restart/stop/start network services
/etc/init.d/networking restart/stop/start
You can configure DNS through a file /etc/resolv.conf
nameserver 8.8.8.8
Prix
fitting
You need to remember which network interfaces
I suggest you configure Debian.
In older versions of Dbian it was simple, they were called eth0, eth1 and so on.
Therefore, to enable the network interface, in the above command, you need to change 0 to 1, etc.
In newer versions of Debian and other Unix-like distributions, for example
OpenBSD
Network interfaces are called more diverse (msk0, sk0, etc.).
In order to avoid difficulties with this, it is useful to write down what is proposed to be configured when
installing a distribution.
Enable automatic receipt of ip
dhclient eth0
You can turn off the automatic receiving ip by pulling the network cord and inserting it back. They say that
He should drop.
I suggest you do.
man interfaces
and study the conclusion.
iftop
You can monitor the volume of incoming and outgoing traffic on a certain interface. command command command command
iftop -i eth0