basic networking commands in linux

Introduction

Linux system is command-based. To enjoy real beauty of Linux, you must aware with commands used in Linux. All commands are located in /bin and /usr/bin. In man command contains documentation of commands. In this article we learn few basic networking commands like host-name, ifconfig, ping and so many. Lets start with commands:
 

HOSTNAME Command
It shows the systems Hostname. Hostname file present at "/etc/hostname" location. We can change host name permanently by editing this file.

# hostname
csfunda.com

IFCONFIG command
ifconfig (interface configurator) displays ip address, netmask, brodcast network interface, hardware address and current network configuration information.

# ifconfig
enp19s0   Link encap:Ethernet  HWaddr a4:ba:db:xx:xx:xx  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2204 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2204 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:183514 (183.5 KB)  TX bytes:183514 (183.5 KB)

wlp18s0b1 Link encap:Ethernet  HWaddr c4:46:19:xx:xx:xx  
          inet addr:192.168.x.x  Bcast:192.168.x.xx  Mask:255.255.255.0
          inet6 addr: fe80::22f3:f1e1:xxxx:xxxx/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18627 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11016 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:20908110 (20.9 MB)  TX bytes:1289043 (1.2 MB)
ifconfig with interface name (enp19s0) will display only specific interface details.
we can enable specific interface using commands "ifup interface_name" or disable specific interface using commands "ifdown interface_name"

# ifup enp19s0

# ifdown enp19s0

PING command
Ping is used to test the network.It send ICMP ECHO_REQUEST to network hosts.

ping using domain name:
# ping csfunda.com
PING csfunda.com (216.239.32.21) 56(84) bytes of data.
64 bytes from any-in-2015.1e100.net (216.239.32.21): icmp_seq=1 ttl=46 time=66.3 ms
64 bytes from any-in-2015.1e100.net (216.239.32.21): icmp_seq=2 ttl=46 time=72.3 ms
64 bytes from any-in-2015.1e100.net (216.239.32.21): icmp_seq=3 ttl=46 time=63.3 ms
^C
--- csfunda.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 63.313/67.306/72.301/3.743 ms

ping using IP address:
# ping 216.239.34.21
PING 216.239.34.21 (216.239.34.21) 56(84) bytes of data.
64 bytes from 216.239.34.21: icmp_seq=1 ttl=46 time=63.3 ms
64 bytes from 216.239.34.21: icmp_seq=2 ttl=46 time=62.7 ms
64 bytes from 216.239.34.21: icmp_seq=3 ttl=46 time=62.4 ms
^C
--- 216.239.34.21 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 62.416/62.833/63.363/0.394 ms

DIG command
dig (domain information groper) is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried.

dig www.csfunda.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> www.csfunda.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- 06:42:16="" 0="" 127.0.1.1="" 14321="" 16759="" 177="" 1799="" 183="" 1="" 2016="" 216.58.199.179="" 30="" 3="" 4096="" 472="" 4="" 97977="" a="" additional:="" answer:="" answer="" authority:="" authority="" cname="" edns:="" flags:="" ghs.google.com.="" ghs.l.google.com.="" google.com.="" id:="" in="" ist="" msec="" msg="" noerror="" nov="" ns1.google.com.="" ns2.google.com.="" ns3.google.com.="" ns4.google.com.="" ns="" opcode:="" opt="" pre="" pseudosection:="" qr="" query:="" query="" question="" ra="" rcvd:="" rd="" section:="" server:="" size="" status:="" time:="" udp:="" version:="" wed="" when:="" www.csfunda.com.="">

NETSTAT command
This command print network connections, routing tables, interface statistics, masquerade connections, and multi-cast memberships. Useful for finding connection to and from the host.
netstat -t : display only tcp connection
netstat -u : display only udp connection
netstat -g : display all interfaces
netstat -a :display all connection
netstat -nap | grep port : display pid of application using this port
netstat -l : List only listing port

# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.x.x:xxxxx       ec2-35-xxx-xx-xxx:https ESTABLISHED
tcp        0      0 192.168.x.x:xxxxx       103.243.xxx.xx:https    ESTABLISHED
tcp        0      0 localhost:epmd          localhost:49290         TIME_WAIT  
tcp        0      0 localhost:58746         localhost:epmd          ESTABLISHED
tcp        0      0 localhost:epmd          localhost:58746         ESTABLISHED
tcp        0      0 192.168.x.x:xxxxx       bom05s05-in-f14.1:https ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    21494    /run/user/1000/systemd/notify
unix  3      [ ]         DGRAM                    11869    /run/systemd/notify
unix  7      [ ]         DGRAM                    11880    /run/systemd/journal/socket
unix  2      [ ]         DGRAM                    11888    /run/systemd/journal/syslog

NSLOOKUP command
It discovers Hostname from IP address or discover IP address from hostname.

# nslookup csfunda.com
Server:  127.0.1.1
Address: 127.0.1.1#53

Non-authoritative answer:
Name: csfunda.com
Address: 216.239.38.21
Name: csfunda.com
Address: 216.239.32.21
# nslookup 216.58.220.14
Server:  127.0.1.1
Address: 127.0.1.1#53

Non-authoritative answer:
14.220.58.216.in-addr.arpa name = bom05s05-in-f14.1e100.net.

Authoritative answers can be found from:
220.58.216.in-addr.arpa nameserver = ns4.google.com.
220.58.216.in-addr.arpa nameserver = ns2.google.com.
220.58.216.in-addr.arpa nameserver = ns1.google.com.
220.58.216.in-addr.arpa nameserver = ns3.google.com.

ROUTE command
It shows ip routing table. We can also manipulate this table.

# route 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.2.1     0.0.0.0         UG    600    0        0 wlp18s0b1
link-local      *               255.255.0.0     U     1000   0        0 wlp18s0b1
192.168.2.0     *               255.255.255.0   U     600    0        0 wlp18s0b1
We can add/delete route.
Route add

# route add -net  gw 

Route Delete

# route del -net  gw 

TRACEROUTE command
traceroute : IT tracks the route packets taken from an IP network on their way to a given host. It utilizes the IP protocol's time to live (TTL) field and attempts to elicit an ICMP TIME_EXCEEDED response from each gateway along the path to the host.

# traceroute google.com
traceroute to google.com (216.58.220.174), 30 hops max, 60 byte packets
 1  192.168.2.1 (192.168.2.1)  2.113 ms  2.814 ms  3.600 ms
 2  * * *
 3  10.50.50.73 (10.50.50.73)  17.259 ms  18.846 ms  20.375 ms
 4  * * *
 5  * * *
 6  10.240.254.1 (10.240.254.1)  29.267 ms * *
 7  10.241.1.1 (10.241.1.1)  20.147 ms  15.656 ms  14.705 ms
 8  100.100.100.1 (100.100.100.1)  17.392 ms  8.787 ms  11.068 ms
 9  103.243.115.186 (103.243.115.186)  10.239 ms  9.947 ms  8.268 ms
10  * * *
11  * * *
12  * * *
13  bom07s10-in-f14.1e100.net (216.58.220.174)  17.852 ms  19.361 ms  21.089 ms

Start enjoying Linux!

0 comments:

Post a Comment