Unix / Linux – 网络命令

本章介绍一些常用网络命令。

ping

PING命令将数据包请求发送到指定地址,以测试两个节点之间的连接。

[kevin@localhost ~]$ ping www.qikegu.com
PING www.qikegu.com (121.199.13.249) 56(84) bytes of data.
64 bytes from 121.199.13.249 (121.199.13.249): icmp_seq=1 ttl=51 time=4.49 ms
64 bytes from 121.199.13.249 (121.199.13.249): icmp_seq=2 ttl=51 time=5.24 ms
64 bytes from 121.199.13.249 (121.199.13.249): icmp_seq=3 ttl=51 time=4.93 ms
^C
--- www.qikegu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 4.497/4.892/5.241/0.305 ms

ping命令会一直执行,可使用-c选项指定执行次数。

[kevin@localhost ~]$ ping -c 3 www.qikegu.com
PING www.qikegu.com (121.199.13.249) 56(84) bytes of data.
64 bytes from 121.199.13.249 (121.199.13.249): icmp_seq=1 ttl=51 time=4.52 ms
64 bytes from 121.199.13.249 (121.199.13.249): icmp_seq=2 ttl=51 time=5.55 ms
64 bytes from 121.199.13.249 (121.199.13.249): icmp_seq=3 ttl=51 time=5.16 ms

--- www.qikegu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2007ms
rtt min/avg/max/mdev = 4.522/5.080/5.551/0.428 ms

netstat

Netstat命令显示各种网络连接信息,信息包括套接字和路由表等。

[kevin@localhost ~]$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost.localdoma:ssh gateway:64763           ESTABLISHED
tcp        0      0 localhost.localdo:47130 59.111.0.251:http       TIME_WAIT
tcp        0      0 localhost.localdoma:ssh gateway:64748           ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  3      [ ]         DGRAM                    7442     /run/systemd/notify
unix  2      [ ]         DGRAM                    7444     /run/systemd/cgroups-agent
unix  5      [ ]         DGRAM                    7464     /run/systemd/journal/socket
unix  15     [ ]         DGRAM                    7466     /dev/log

...

添加选项-r显示路由表信息:

kevin@localhost ~]$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 enp0s3

ifconfig

ifconfig命令用于配置网络接口。

大多数情况下,我们使用这个命令查看主机IP地址。

[kevin@localhost ~]$ ifconfig -a
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::a35f:b12e:1ef3:dd19  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:9e:aa:23  txqueuelen 1000  (Ethernet)
        RX packets 11875  bytes 9469332 (9.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4535  bytes 1480350 (1.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 68  bytes 5896 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5896 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

telnet

telnet连接目标主机: 如果连接建立,则通过telnet协议进行端口连接,这意味着两台主机之间的连接工作正常。

[kevin@localhost ~]$ telnet www.qikegu.com 443
Trying 121.199.13.249...
Connected to www.qikegu.com.
Escape character is '^]'.

nmap

黑客常用工具,功能强大,用于扫描服务器端口。

[kevin@localhost ~]$ nmap baidu.com

Starting Nmap 6.40 ( http://nmap.org ) at 2019-04-03 12:36 CST
Nmap scan report for baidu.com (220.181.57.216)
Host is up (0.035s latency).
Other addresses for baidu.com (not scanned): 123.125.114.144
Not shown: 998 filtered ports
PORT    STATE SERVICE
80/tcp  open  http
443/tcp open  https

Nmap done: 1 IP address (1 host up) scanned in 4.51 seconds
[kevin@localhost ~]$

scp

scp命令可以在目标主机与本地主机之间安全复制文件。

scp $filename user@targethost:/$path

启用/禁用网络接口

ifup/ifdown命令可以启用或禁用网络接口

启用:

# ifup eth0

禁用:

# ifdown eth0

nslookup

nslookup命令可以查询互联网域名服务器获取域名信息。

[kevin@localhost ~]$ nslookup www.qikegu.com
Server:         192.168.31.1
Address:        192.168.31.1#53

Non-authoritative answer:
Name:   www.qikegu.com
Address: 121.199.13.249

tcpdump

tcpdump是一个嗅探器/抓包工具,用于抓取网络数据包。

tcpdump host 1.1.1.1

traceroute

traceroute跟踪到网络主机的路由。

[kevin@localhost ~]$ traceroute www.qikegu.com
traceroute to www.qikegu.com (121.199.13.249), 30 hops max, 60 byte packets
 1  gateway (10.0.2.2)  0.191 ms  0.242 ms  0.114 ms
 2  gateway (10.0.2.2)  2.995 ms  2.892 ms  2.790 ms
[kevin@localhost ~]$



浙ICP备17015664号-1 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载