Linux network commands 1

Thanks to Elad Levi for the more deep insight of the netstat command.

  • ifconfig – show basic network information for the different interfaces  on the computer. This include ip addresses, broadcast address, and net mask.
    The command can also be used to define network setting for the different interface. in some distributions, this command is missing, instead you will have to use the ip command.
  • netstat – one of the most useful command. it allow us to see network connection and what service listen to what port.
    Some option it has:
    -l  show only listening socket.
    -t  show only tcp connection.
    -n show port number.
    -u show only udp connection.
    -r show routing table
    So for example if we want all tcp listener servers, we can run:

    
    bob@vmUbuntu:~$ netstat -lt
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State
    tcp        0      0 localhost:mysql         *:*                     LISTEN
    tcp        0      0 *:ssh                   *:*                     LISTEN
    tcp        0      0 *:smtp                  *:*                     LISTEN
    tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
    

    So in this example we can see that we have mysql server (actually mariadb), ssh server, and smtp server (mail server)
    If we want to see the port number instead then we can use  -n:

    
    bob@vmUbuntu:~$ netstat -ltn
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State      
    tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN     
    tcp6       0      0 :::22                   :::*                    LISTEN     
    
    
  • route show routing table. The default destination is the default gateway, the address of the router that allow us to communicate the world outside.
    For example:

    
    bob@vmUbuntu:~$ sudo route
    [sudo] password for bob: 
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         10.0.0.138      0.0.0.0         UG    0      0        0 enp0s3
    10.0.0.0        *               255.255.255.0   U     0      0        0 enp0s3
    
    

    In this example we can see that the default gateway is 10.0.0.138.
    Alternatively we can use netstat -r and it will have the same effect.
    For example:

    
    bob@vmUbuntu:~$ netstat -r
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    default         10.0.0.138      0.0.0.0         UG        0 0          0 enp0s3
    10.0.0.0        *               255.255.255.0   U         0 0          0 enp0s3
  • ifup <inteface> take the interface up
  • ifdown <interface> take the interface down.
  • ip  show and manipulate routing, network interface, policy and tunneling.
    ip addr  show network information about the current interface. similar to ifconfig.
    For example:

    
    bob@vmUbuntu:~$ ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
        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
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 08:00:27:cf:ea:16 brd ff:ff:ff:ff:ff:ff
        inet 10.0.0.2/24 brd 10.0.0.255 scope global enp0s3
           valid_lft forever preferred_lft forever
        inet6 fe80::a00:27ff:fecf:ea16/64 scope link 
           valid_lft forever preferred_lft forever
    
    
    
  • host <domain> resolve that address to the ip. This is useful to check if we have problem wit the dns.
    For Example if we want to check for the ip of google:

    
    bob@vmUbuntu:~$ host www.google.com
    www.google.com has address 212.179.180.109
    www.google.com has address 212.179.180.89
    www.google.com has address 212.179.180.82
    www.google.com has address 212.179.180.116
    www.google.com has address 212.179.180.102
    www.google.com has address 212.179.180.117
    www.google.com has address 212.179.180.96
    www.google.com has address 212.179.180.88
    www.google.com has address 212.179.180.95
    www.google.com has address 212.179.180.110
    www.google.com has address 212.179.180.123
    www.google.com has address 212.179.180.103
    www.google.com has IPv6 address 2a00:1450:4017:804::2004
    
    
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s