Show information about connections
lsof
- i
select IPv[46] files.
- P
no port names.
- n
no host names.
- sTCP:LISTEN
select protocol and state(s) by name.
ss
- n
don't resolve service names
- r
resolve host names
- t
display only TCP sockets
- a
display all sockets
- u
display only UDP sockets
- l
display listening sockets
- p
show process using socket
- i
show internal TCP information
- E
continually display sockets as they are destroyed
- 4
display only IP version 4 sockets
- K
forcibly close sockets, display what was closed
- H
Suppress header line
- O
socket's data printed on a single line
- A
QUERY. Some queries are inet
, tcp
- F
Filter. Some filters are established
, syn-sent
, time-wait
, listening
netstat
- t
show TCP ports.
- u
show UDP ports.
- l
show only listening ports.
- n
show numerical addresses instead of resolving hosts.
- p
show the PID and name of the listener’s process. This information is shown only if you run the command as root or sudo user.
Capture traffic
Set up a network configuration
You need IP, Mask, Gateway, Route and DNS.
ifupdown
/etc/network/interfaces
---
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
allow-hotplug eth1
iface eth1 inet static
address 192.168.11.100/24
gateway 192.168.11.1
dns-domain example.com
dns-nameservers 192.168.11.1 9.9.9.9
pre-up echo "nameserver 192.168.11.1\nnameserver 9.9.9.9" > /etc/resolv.conf
## Subinterface
auto eth1:1
iface eth1:1 inet static
address 192.168.11.101/24
## Vlan
allow-hotplug eth2 eth2.10
iface eth2 inet static
address 10.0.0.23/24
gateway 10.0.0.1
scope link
up ifup eth2.10
iface eth2.10 inet dhcp
hostname server.example.com
metric 100
scope host
NetworkManager
Show connections
Disconnect
Connect
Add a new connection
nmcli connection add type ethernet con-name Work ifname enp0s3
nmcli con -a con-name Home ## Will ask for any missing parameters
Delete a connection
Change the name of a connection
Details of a connection
Static connection
nmcli con add con-name static ifname enp3s0 ip4 192.168.1.50/24 gw4 192.168.1.1
nmcli con add con-name static ifname enp3s0 ipv4.method manual ip4 192.168.1.50/24 gw4 192.168.1.1 ipv4.dns "8.8.8.8 1.1.1.1" connection.autoconnect no connection.permissions user:user,yu
Add a route
Bridge for vlan. (use vlans in QEMU)
nmcli connection add type bridge con-name Bridge0 ifname br0 ip4 192.168.0.2/24
nmcli connection add type vlan con-name V20 ifname enp3s0.20 dev enp3s0 id 20 master br0 slave-type bridge
## Firewall
sudo iptables -A FORWARD -p udp -i br0 -o br0 --sport 67:68 --dport 67:68 -j ACCEPT
sudo iptables -A FORWARD -p icmp -i br0 -o br0 -j ACCEPT
Wifi
Scan networks.
Connect to network
iproute2
Show connections
Show routes
Show neighbors (ARP)
Add an IP address
Add a route
ip route add <network> via <ip> dev <interface>
ip route add 172.16.34.0/23 via <ip> dev <interface>
Add a default route
Set an interface up/down
Remove an IP address
Remove a route
netplan
The directory should be at /etc/netplan
.
Apply the configuration
Example:
network:
ethernets:
enp1s0:
addresses:
- 192.168.0.59/24
gateway4: 192.168.0.1
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
search: []
version: 2
Bridge:
network:
ethernets:
enp1s0:
dhcp4: false
dhcp6: false
bridges:
br0:
interfaces: [enp1s0]
addresses: [192.168.0.59/24]
gateway4: 192.168.0.1
nameservers:
addresses: [8.8.8.8,1.1.1.1]
dhcp4: false
dhcp6: false
parameters:
forward-delay: 0
version: 2
systemd-networkd
Basic network with manual configuration:
/etc/systemd/network/eth0.network
---
[Match]
Name=eth0
[Network]
Address=192.168.122.45/24
Gateway=192.168.122.1
DNS=1.1.1.1
Basic network with mixed configuration:
/etc/systemd/network/enp1s0.network
---
[Match]
Name=enp1s0
[Network]
DHCP=yes
[DHCPv4]
RouteMetric=10
Restart the service to apply the changes:
Other parameters
Hostname
Timezone
VLANs
Manual vlan
sudo ip link add link enp3s0 name enp3s0.20 type vlan id 20
sudo ip addr add 192.168.0.2/24 brd 192.168.0.255 dev enp3s0.20
sudo ip link set dev enp3s0.20 up
Remove.
VLAN with NetworkManager
nmcli con add con-name enp3s0.50 type vlan vlan.id 50 vlan.parent enp3s0 ipv4.method manual ipv4.addresses 10.0.0.5/24 ipv4.gateway 10.0.0.1 connection.autoconnect no
VLANs with systemctl
/etc/systemd/network/eno1.network
/etc/systemd/network/eno1.10.netdev
/etc/systemd/network/eno1.10.network
[Match]
Name=eno1.10
[Network]
Address=192.168.1.14/24
Address=192.168.1.24/24
[Route]
Gateway=192.168.1.1
Table=10
[RoutingPolicyRule]
From=192.168.1.0/24
Table=10
/etc/systemd/network/eno1.11.netdev
/etc/systemd/network/eno1.11.network
[Match]
Name=eth0.11
[Network]
Address=192.168.100.54/24
[Route]
Gateway=192.168.100.1
Table=11
[RoutingPolicyRule]
From=192.168.100.0/24
Table=11
Rename network adapter with udev
rules
Find the MAC address 00:00:00:00:00:44
Ad a udev rule
/etc/udev/rules.d/70-net.rules
---
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:00:00:00:00:44", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="enpusb0"
You can also add the driver
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="ax88179_178a", ATTR{address}=="00:00:00:00:00:44", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="enpusb0"
Check the result. There is no need to reboot if you can unplug it.
Rename network adapter with systemd-networkd
/etc/systemd/network/10-ethusb0.link
---
[Match]
MACAddress=12:34:56:78:90:ab
[Link]
Description=USB to Ethernet Adapter
Name=ethusb0
Sources
- Debian manual Chapter 5 Network Setup
- https://www.cyberciti.biz/faq/howto-linux-rename-ethernet-devices-named-using-udev/
- https://paulgorman.org/technical/linux-iproute2-cheatsheet.html
- https://dustymabe.com/2020/03/05/network-teaming-using-networkmanager-keyfiles-on-fedora-coreos/
- ArchWiki's systemd-networkd
- https://developer.gnome.org/NetworkManager/stable/nmcli.html
- https://www.tecmint.com/configure-network-connections-using-nmcli-tool-in-linux/
- https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-network_bridging_using_the_networkmanager_command_line_tool_nmcli
- https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/configuring-networking-with-nmcli_configuring-and-managing-networking