How to manage the network with Linux command line efficiently?

Keywords: Linux network sudo curl Unix

Preface

In our daily work, the Linux server operating system that we often use, whether it is to view the information of network devices, manage network interfaces, download files, debug network problems or view network statistics, can be completed by commands under the terminal.

ifconfig / ip a

The command ifconfig is similar to ip a, which is used to output various options such as network interface configuration and tuning. It is convenient to view the information of IP address and other network interfaces, as well as the status, name or specified network interface name of all enabled network interfaces to display the information of an interface.

# ip a
# ifconfig
# ifconfig ens32

ifdown / ifup

ifdown and ifup are consistent with the ifconfig down and ifconfig up commands. They are mainly used to specify the network interface name to operate the enabled or disabled state. In Ubuntu, the root permission is required to execute using sudo.

# sudo ifdown eth0
# sudo ifup eth0

ping

ping sends the message to the specified IP address, which can test the connectivity. Using the - c parameter, you can specify the number of packets to be sent.

Executing the ping command will use the ICMP transport protocol.

If it can ping, it will output the required response information. If it can not Ping, it will display the response information that has no routing or rejection correlation. It can solve the specific network situation according to the output information;

# ping -c 4 www.baidu.com

tracepath / traceroute command

tracepath is similar to traceroute command and does not require root permission.

Ubuntu is pre installed with tracepath command, traceroute command is not pre installed.

Tracepath command: it can trace the network path to the specified destination address, and output the address information of each hop on each path. When the network has problems or is slow, tracepath command can be used to find out where the network is broken or slow.

traceroute command: used to track the routing path of network packets. The default packet size is 40Bytes. Packets from a starting point to a destination may take different routes, but they are basically the same.

# tracepath www.baidu.com
# traceroute www.baidu.com

mtr command

mtr command: the ping command and tracepath command are combined.

mtr will continue to send packets, and display the Host Address and the time used for each hop ping, etc;

# mtr www.github.com

Press q or ctrl+c to exit.

ifplugstatus

This command is not installed on the Ubuntu operating system. You can install it through the following command:

# sudo apt install ifplugd

ifplugstatus command: used to view the status of all network interfaces, or to specify network interfaces;

# ifplugstatus
lo: link beat detected
ens32: link beat detected

# ifplugstatus ens32
ens32: link beat detected

curl / wget command

Using curl or wget commands, you can download files directly from the terminal.

curl needs to add a parameter option: - O followed by a file path;

# curl -O http://www.freelogovectors.net/wp-content/uploads/2016/12/ubuntu_logo.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   282  100   282    0     0    156      0  0:00:01  0:00:01 --:--:--   156

# ll | grep ubuntu_logo.png 
-rw-r--r--  1 root     root          282 10 Month 2916:49 ubuntu_logo.png

wget does not need any parameter options. The downloaded files will be saved in the current directory;

# wget http://www.freelogovectors.net/wp-content/uploads/2016/12/ubuntu_logo.png

# ll | grep ubuntu_logo.png 
-rw-r--r--  1 root     root        95737 12 Month 132016 ubuntu_logo.png

host command

host command: used for DNS query. host command: used for DNS query.

If the command parameter is domain name, the command will output the associated IP; if the command parameter is IP, the command will output the associated domain name.

# host www.sina.com
# host IP Address

whois command

Whois command: used to output whois records and other information of the specified site.

# whois www.github.com

netstat command

netstat command: used to display network interface statistics, including open socket and routing table.

-p: the program corresponding to the open socket can be displayed.

# netstat -p
//Activate Internet connection (w/o server)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 jacktian-virtual-ma:ssh 192.168.1.124:44284     ESTABLISHED 4285/sshd: root@pts 
//Active UNIX domain socket (w/o server)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Route
unix  2      [ ]         Datagram                Fifty-nine thousand five hundred and seventy-two    Four thousand two hundred and ninety-six/systemd         /run/user/0/systemd/notify
unix  2      [ ]         Datagram                Forty-nine thousand three hundred and fifty-one    Three thousand three hundred and forty-four/systemd         /run/user/1000/systemd/notify
unix  3      [ ]         Datagram                Nineteen thousand two hundred and twenty-two    1/init               /run/systemd/notify

-s: displays detailed statistics for all ports.

# netstat -s
Ip:
    Forwarding: 2
    30932 total packets received
    2 with invalid headers
    11 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    30911 incoming packets delivered
    18611 requests sent out
    22 outgoing packets dropped
Icmp:
    5499 ICMP messages received
    3 input ICMP message failed
    ICMP History of reception
        destination unreachable: 141
        timeout in transit: 5354
        echo requests: 2
        echo replies: 2
    7523 ICMP messages sent
    0 ICMP messages failed
    ICMP Issue history
        destination unreachable: 145
        echo requests: 7376
        echo replies: 2

For example, the information of port 22 can be viewed in combination with various parameters.

# netstat -anpt | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      979/sshd            
tcp        0     36 192.168.1.142:22        192.168.1.124:44284     ESTABLISHED 4285/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      979/sshd 

Posted by jazz_snob on Sun, 03 Nov 2019 01:49:02 -0700