IP Configuration
Understanding and managing IP addresses is the foundation of networking on Unix and Linux systems. In this section, we’ll focus on viewing interfaces, checking IP addresses and assigning temporary addresses for testing.
Viewing Network interfaces
The modern command to inspect interfaces is ip (Preferred over the older ifconfig)
user@machine:/$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:15:5d:4b:89:84 brd ff:ff:ff:ff:ff:fflo-> Loopback interface (used internally, equivalent to ip127.0.0.1)eth0-> first Ethernet interfaceUP-> interface is activemtu-> maximum transmission unit (packet size)
Note that eth0 can vary some times. Be sure to check that your interface is eth0. If not, use whatever thats there.
Checking IP addresses
To see assigned IPs, use ip addr(can be shortened to ip a)
user@machine:/$ ip a
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
inet 10.255.255.254/32 brd 10.255.255.254 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host proto kernel_lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:4b:89:84 brd ff:ff:ff:ff:ff:ff
inet 172.29.26.129/20 brd 172.29.31.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe4b:8984/64 scope link proto kernel_ll
valid_lft forever preferred_lft foreverThe highlighted line shows the IPv4 address of eth0, 172.29.26.129/20, which has the broadcast address(brd) of 172.29.31.255.
Scope refers to the range of visibility (global vs link-local).
Since inet refers to IPv4, inet6 refers to IPv6.
Assigning IP Addresses Temporarily
You can assign an IP to an interface (will reset on boot) using ip addr add. This requires root or sudo permissions.
user@machine:/$ sudo ip addr add 192.168.1.50/24 dev eth0
sudo ip link set eth0 up
Ensure your network interface is UP.
ip addr add-> add IPdev eth0-> specify interfaceip link set up-> enable the interface To remove it, you run the same command but instead ofip addr add, it’sip addr del.
user@machine:/$ sudo ip addr del 192.168.1.50/24 dev eth0
You will need to use ip a to verify changes as these commands do not print any output.
Viewing Routes
Check the routing table to see how packets are forwarded via ip route show.
user@machine:/$ ip route show
default via 172.29.16.1 dev eth0 proto kernel
172.29.16.0/20 dev eth0 proto kernel scope link src 172.29.26.129default-> default gateway172.29.16.0/20-> local network route
Quick Reference
user@machine:/$ ip link show # list interfaces
user@machine:/$ ip addr show # show IP addresses
user@machine:/$ sudo ip addr add # assign IP temporarily
user@machine:/$ sudo ip addr del # remove IP
user@machine:/$ ip route show # view routing table