Pradeep Singh | 29th Jun 2016
Let’s explore Raspberry Pi’s commands and option related to IP Network:
1. IP ADDRESS CONFIGURATION:
1.1 How to Check Current IP Address?
You can use “ifconfig” command to check the current IP Address configuration on Raspberry Pi –
Example:
pi@raspberrypi ~ $ ifconfig eth0 Link encap:Ethernet HWaddr b8:27:eb:c3:b1:45 inet addr:10.0.1.55 Bcast:10.0.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:224 errors:0 dropped:12 overruns:0 frame:0 TX packets:921 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:25423 (24.8 KiB) TX bytes:132944 (129.8 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:72 errors:0 dropped:0 overruns:0 frame:0 TX packets:72 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:6288 (6.1 KiB) TX bytes:6288 (6.1 KiB) pi@raspberrypi ~ $
1.2 How to Assigning Temporary Static IP Address?
“ifconfig” command can also be used to configure Static IP Address on Raspberry Pi. However, the IP Address assigned using this command will be lost if Raspberry Pi is rebooted.
sudo ifconfig <nw interface> <ip add> netmask <subnet mask> <up|down>
Example:
pi@raspberrypi ~ $ sudo ifconfig eth0 10.0.1.55 netmask 255.255.255.0 up pi@raspberrypi ~ $
1.3 How to Assign Permanent Static IP Address?
Raspberry Pi saves all the Network Interface related configurations in “/etc/network/interfaces” file. To preserve IP Address even after the device reboots, the IP Address configuration should be added to this file.
Edit this file and add following configuration to the Network Interface that should use static IP Add –
auto <nw interface name> allow-hotplug <nw interface name> iface <nw interface name> inet static address <ip address> netmask <subnet mask> network <network ip add> broadcast <broadcast ip add> gateway <gateway ip add>
Example:
pi@raspberrypi ~ $ cat /etc/network/interfaces auto lo iface lo inet loopback auto eth0 allow-hotplug eth0 iface eth0 inet static address 10.0.1.55 netmask 255.255.255.0 network 10.0.1.0 broadcast 10.0.1.255 gateway 10.0.1.255 auto wlan0 allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf auto wlan1 allow-hotplug wlan1 iface wlan1 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf pi@raspberrypi ~ $
After making the changes in “/etc/network/interfaces” file, networking service should be restarted.
sudo vi /etc/network/interfaces
or simply reboot the device.
sudo reboot
1.4 How to Revert Back to DHCP From Static IP Address?
Edit “/etc/network/interfaces” file and remove Network Interface Configuration related to static IP Address. Add following configuration back to this file –
auto <nw interface name> allow-hotplug <nw interface name> iface <nw interface name> inet dhcp
Example:
pi@raspberrypi ~ $ cat /etc/network/interfaces auto lo iface lo inet loopback auto eth0 allow-hotplug eth0 iface eth0 inet dhcp auto wlan0 allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf auto wlan1 allow-hotplug wlan1 iface wlan1 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf pi@raspberrypi ~ $
*Sometimes in case of DHCP Server failure or Network Cable fault, using “dhcp” option with “inet” may cause delay in Raspberry Pi boot-up process . In such scenario you may try replacing “dhcp” with “manual”.
Example:
auto eth0 allow-hotplug eth0 iface eth0 inet manual
2. ROUTING TABLE:
2.1 How to Check Routing Table?
Raspberry Pi’s routing table can be checked using “route” command –
Example:
pi@raspberrypi ~ $ route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default APAP.local 0.0.0.0 UG 0 0 0 eth0 10.0.1.0 * 255.255.255.0 U 0 0 0 eth0 pi@raspberrypi ~ $ pi@raspberrypi ~ $ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.1.1 0.0.0.0 UG 0 0 0 eth0 10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 pi@raspberrypi ~ $
2.2 How to Add Default Gateway in Routing Table?
If the default route is missing from routing table it can be added with “route” command.
sudo route add default gw <ip add> <nw interface>
Example:
pi@raspberrypi ~ $ sudo route add default gw 10.0.1.1 eth0 pi@raspberrypi ~ $
2.3 How to Remove Route from Routing Table?
A route entry can be deleted from routing table using “route del” command.
sudo route del -net <nw address> netmask <subnet mask> gw <gateway ip add>
Example:
pi@raspberrypi ~ $ sudo route del -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.1.1 pi@raspberrypi ~ $
3. DOMAIN NAME SYSTEM (DNS):
3.1 How to Check and Change DNS Server IP Address?
Raspberry Pi saves DNS IP in “/etc/resolv.conf” file. To add or change the DNS IP this file can be edited with any text editor such as “vi”.
Example:
pi@raspberrypi ~ $ cat /etc/resolv.conf # Generated by resolvconf nameserver 10.0.1.1 pi@raspberrypi ~ $
3.2 How to resolve DNS Name into IP Address?
DNS Names can be resolved using “nslookup” command.
Example:
pi@raspberrypi ~ $ nslookup google.com Server: 10.0.1.1 Address: 10.0.1.1#53 Non-authoritative answer: Name: google.com Address: 172.217.24.238 pi@raspberrypi ~ $
If you see following error while executing “nslookup” command, install “dnsutils” on Raspberry Pi.
pi@raspberrypi ~ $ nslookup -bash: nslookup: command not found
“dnsutils” can be installed using following two commands –
sudo apt-get update sudo apt-get install dnsutils
3.3 Alternate Method for DNS Lookup-
“host” command can also be used to resolve DNS Name into IP Address.
Example:
pi@raspberrypi ~ $ host www.google.com www.google.com has address 74.125.23.105 www.google.com has address 74.125.23.99 www.google.com has address 74.125.23.106 www.google.com has address 74.125.23.104 www.google.com has address 74.125.23.147 www.google.com has address 74.125.23.103 www.google.com has IPv6 address 2404:6800:4008:c02::93 pi@raspberrypi ~ $
4. ADDRESS RESOLUTION PROTOCOL (ARP):
4.1 How to Check ARP Table?
ARP (Address Resolution Protocol) entries can be checked using “arp” command. Use “arp -a” to display IP Address along with Host Name and MAC Address.
Example:
pi@raspberrypi ~ $ arp Address HWtype HWaddress Flags Mask Iface APAP.local ether 24:a2:e1:e6:ee:9b C eth0 PRADEESI-M-40CK.local ether 28:cf:e9:21:14:8f C eth0 pi@raspberrypi ~ $ arp -a APAP.local (10.0.1.1) at 24:a2:e1:e6:ee:9b [ether] on eth0 PRADEESI-M-40CK.local (10.0.1.5) at 28:cf:e9:21:14:8f [ether] on eth0 pi@raspberrypi ~ $
4.2 How to Remove ARP Entry?
ARP entry can be deleted with “arp -d” command.
sudo arp -d <ip add or hostname>
Example:
pi@raspberrypi ~ $ arp -d 10.0.1.1 pi@raspberrypi ~ $
5. NETWORK INTERFACE CARD (NIC):
5.1 How to Disable Network Interface?
Network interface on Raspberry Pi can be disabled using “ifdown” command.
sudo ifdown <nw interface>
Example:
pi@raspberrypi ~ $ sudo ifdown eth0 pi@raspberrypi ~ $
5.2 How to Enable Network Interface?
Network interface on Raspberry Pi can be enabled using “ifup” command.
sudo ifup <nw interface>
Example:
pi@raspberrypi ~ $ sudo ifup eth0 pi@raspberrypi ~ $
6. TROUBLESHOOTING:
6.1 How to Check Network Connectivity With a Host?
Network connectivity can be checked using “ping” command.
ping <ip add or host name>
Example:
pi@raspberrypi ~ $ ping google.com PING google.com (216.58.197.46) 56(84) bytes of data. 64 bytes from maa03s20-in-f14.1e100.net (216.58.197.46): icmp_req=1 ttl=56 time=9.20 ms 64 bytes from maa03s20-in-f14.1e100.net (216.58.197.46): icmp_req=2 ttl=56 time=9.53 ms ^C --- google.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 9.209/9.373/9.537/0.164 ms pi@raspberrypi ~ $
6.2 How to Trace All L3 Devices Between Two Hosts?
All the Layer-3 Devices between two hosts can be traced using “traceroute” command.
traceroute <ip add or host name>
Example:
pi@raspberrypi ~ $ traceroute google.com traceroute to google.com (216.58.197.46), 30 hops max, 60 byte packets 1 APAP.local (10.0.1.1) 0.547 ms 0.479 ms 1.532 ms 2 10.242.0.1 (10.242.0.1) 5.327 ms 5.241 ms 5.325 ms 3 83.20-broadband.acttv.in (202.83.20.205) 5.234 ms 5.145 ms 5.060 ms 4 83.26-broadband.acttv.in (202.83.26.1) 5.809 ms 5.728 ms 5.689 ms 5 83.20-broadband.acttv.in (202.83.20.26) 13.589 ms 13.474 ms 13.454 ms 6 72.14.194.18 (72.14.194.18) 11.897 ms 9.558 ms 9.334 ms 7 209.85.242.219 (209.85.242.219) 9.006 ms 10.022 ms 10.133 ms 8 209.85.250.65 (209.85.250.65) 10.012 ms 9.891 ms 9.771 ms 9 maa03s20-in-f14.1e100.net (216.58.197.46) 9.651 ms 10.628 ms 10.507 ms pi@raspberrypi ~ $