Skip to main content

network commands on linux

Recently, the iproute2 package's commands are recommended instead of net-tools to show or control network settings.

確認コマンド

show mac address table

ip [-6] neigh
arp -an

show interface mac address

ip link
ifconfig

show host ip address

ip [-6] addr
ifconfig

show routing table

ip [-6] route
netstat -rn
route -n

show tcp and udp port status

ss -an
netstat -an

query domain name server

dig <@nameserver> domainname [a|any|mx|ns|...]

cat << END > list.dig
@xxx.xxx.xxx.xxx example.com
@xxx.xxx.xxx.yyy example.com
END

dig -f list.dig


nslookup domainname <nameserver>

nslookup << exit
server xxx.xxx.xxx.xxx
example.org
example.com
exit

設定コマンド

down and up the link

ip link set br0 down
ip link set br0 up

delete and add from/to interface

ifconfig eth0 0.0.0.0
ifconfig eth0 192.168.0.1 netmask 255.255.255.0
ip address delete 192.168.0.1/24 dev enp0s25    
ip address add 192.168.0.1/24 brd + dev enp0s25

delete and add default gateway

ip route delete default
ip route add default via 192.168.0.254

add static route

ip route add 10.0.0.0/24 via 192.168.0.252

operate virtual bridge

show information of virtual bridge

brctl show
ip link show master br0
bridge link show

add and delete virtual bridge

ip link add br0 type bridge
ip link del br0
brctl addbr br0
brctl delbr br0

create tap0 and set pormiscuous mode

ip tuntap add dev tap0 mode tap
ip link set tap0 up promisc on
ip link set tap0 down promisc off
ip tuntap del dev tap0 mode tap

add and delete interface to virtual bridge

ip link set dev enp0s25 master br0
ip link set dev tap0 master br0
ip link set dev enp0s25 nomaster
brctl addif br0 eth0
brctl addif br0 tap0
brctl delif br0 eth0

write below to network config file and execute service networking restart

auto eth0
iface eth0 inet static
  address 0.0.0.0
auto br0
iface br0 inet static
  address 192.168.0.1
  netmask 255.255.255.0
  bridge_ports eth0

Alternatively you can use systemd-networkd for handling virtual bridge. At first put these 3 files in /etc/systemd/network

eth0.network

[Match]
Name=eth0

[Network]
Bridge=br0
## if you want to use this interface, comment out above line
## and specify dhcp or static address
#DHCP=ipv4
#Address=192.168.0.1
#Gateway=192.168.0.254
#DNS=192.168.0.254

br0.netdev

[NetDev]
Name=br0
Kind=bridge

br0.network

[Match]
Name=br0

[Network]
# DHCP=ipv4
Address=192.168.0.1
Gateway=192.168.0.254
DNS=192.168.0.254

then enable and start systemd-networkd

systemctl enable  systemd-networkd
systemctl restart systemd-networkd

openvswitch

show information of virtual bridge

ovs-vsctl show

add and delete virtual bridge

ovs-vsctl add-br br0
ovs-vsctl del-br br0

add and dlete interface to virtual bridge

ovs-vsctl add-port br0 eth0
ovs-vsctl add-port br0 tap0
ip link set br0 up
ovs-vsctl del-port br0 tap0
ovs-vsctl del-port br0 eth0