Linux常用命令之网络命令详解(ping、ifconfig、mail、traceroute、netstat、setup、nslookup、route)

Linux常用命令——网络命令

1、ping命令:测试网络连通性

ping是常用的网络命令,主要通过ICMP协议进行网络探测,探测网络中个主机的连通性。基本信息如下:

  • 命令名称:ping
  • 所在路径:/bin/ping
  • 执行权限:所有用户
  • 功能描述:测试网络连通性
  • 基本格式:ping [选项] IP或域名

ping命令的常见选项:

  • -b:后面加入广播地址,用于对整个网段进行探测;
  • -c 次数:用于指定 ping 的次数;
1.1> ping IP/域名

在Linux中,ping命令会一直进行。可以使用“Ctrl + C”快捷键强制中止。

[root@root 桌面]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121: icmp_seq=1 ttl=128 time=31.6 ms
64 bytes from 61.135.169.121: icmp_seq=2 ttl=128 time=33.2 ms
64 bytes from 61.135.169.121: icmp_seq=3 ttl=128 time=33.7 ms
64 bytes from 61.135.169.121: icmp_seq=4 ttl=128 time=32.4 ms
64 bytes from 61.135.169.121: icmp_seq=5 ttl=128 time=30.6 ms
......
1.2> ping -c 次数 IP/域名——>指定ping包的次数
[root@root 桌面]# ping -c 3 www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121: icmp_seq=1 ttl=128 time=32.7 ms
64 bytes from 61.135.169.121: icmp_seq=2 ttl=128 time=34.2 ms
64 bytes from 61.135.169.121: icmp_seq=3 ttl=128 time=31.2 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2054ms
rtt min/avg/max/mdev = 31.278/32.759/34.238/1.208 ms
1.3> “ -b ”选项:后面加入广播地址(即ping网管),用于对整个网段进行测试;
[root@root 桌面]# ping -b 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=128 time=2.95 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=128 time=2.57 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=128 time=2.06 ms
^C
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2687ms
rtt min/avg/max/mdev = 2.067/2.533/2.956/0.368 ms

2、ifconfig命令:查看和设置网卡信息

  • ifconfig(interfaces config),通常需要root身份登录或使用sudo来使用ifconfig工具;

  • ifconfig 命令用来查看和配置网络设备。当网络环境发生改变是可通过次命令对网络进行相应的配置;

  • 用 ifconfig命令配置的网卡信息,在网卡重启后机器或机器重启后,配置就失效了。如果需要该配置长期有效,就需要修改配置文件;
    ifconfig 是Linux中查看和临时修改 IP 地址的命令,基本信息如下:

  • 命令名称:ifconfig

  • 所在路径:/sbin/ifconfig

  • 执行权限:超级用户

  • 功能描述:查看和设置网卡信息

2.1> 查看IP地址信息
[root@root 桌面]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:0C:29:9A:A0:31  
          inet addr:192.168.85.133  Bcast:192.168.85.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe9a:a031/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1629 errors:0 dropped:0 overruns:0 frame:0
          TX packets:579 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:115321 (112.6 KiB)  TX bytes:47994 (46.8 KiB)

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:16436  Metric:1
          RX packets:5192 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5192 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:324462 (316.8 KiB)  TX bytes:324462 (316.8 KiB)

说明:
(1)eth0 表示第一块网卡,其中HWaddr表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址)是00:16:3E:00:1E:51。
(2)inet addr 用来表示网卡的IP地址,此网卡的IP地址是10.160.7.81,广播地址
(3) Bcast:10.160.15.255,掩码地址Mask:255.255.240.0。
(4)lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 httpd
服务器的指定到回坏地址,在浏览器输入127.0.0.1就能看到你所架WEB网站了。但
只是您能看得到,局域网的其它主机或用户无从知道。

  • 第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址)。
  • 第二行:网卡的IP地址、子网、掩码。
  • 第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节。
  • 第四、五行:接收、发送数据包情况统计。
  • 第七行:接收、发送数据字节数统计信息。
2.2> 临时配置IP地址:一旦主机中期,就会失效,如果需要长期有效,就需要修改配置文件;
[root@root 桌面]# ifconfig eth1 192.168.30.30
[root@root 桌面]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:0C:29:5E:76:E7  
          inet addr:192.168.30.30  Bcast:192.168.30.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe5e:76e7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:153 errors:0 dropped:0 overruns:0 frame:0
          TX packets:140 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12477 (12.1 KiB)  TX bytes:11352 (11.0 KiB)

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:16436  Metric:1
          RX packets:38 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2724 (2.6 KiB)  TX bytes:2724 (2.6 KiB)
2.3> 禁止或者重新激活网卡

格式如下,
启动:ifconfig eth* up
禁止:ifconfig eth* down

[root@root 桌面]# ifconfig eth1 down
[root@root 桌面]# ifconfig eth1 up

3、mail命令:发送和接收电子邮件

mail 是Linux发送和接收电子邮件的命令,基本信息如下:

  • 命令名称:mail
  • 所在路径:/bin/mail
  • 执行权限:所有用户
  • 功能描述:发送和接收电子邮件

mail 收件方用户名,例如:给root用户写邮件,mail查看邮件;

[root@redhat 桌面]# mail root
Subject: test
nihao hello
EOT
[root@redhat 桌面]# mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 8 messages 8 new
>N  1 Anacron               Tue Mar 12 05:07  17/636   "Anacron job 'cron.dai"
 N  2 Anacron               Wed Mar 13 03:11  17/636   "Anacron job 'cron.dai"
 N  3 Anacron               Thu Mar 14 04:09  17/636   "Anacron job 'cron.dai"
 N  4 Anacron               Fri Mar 15 04:42  17/636   "Anacron job 'cron.dai"
 N  5 Anacron               Sat Mar 16 07:16  17/636   "Anacron job 'cron.dai"
 N  6 Anacron               Sat Mar 23 04:39  17/636   "Anacron job 'cron.dai"
 N  7 Anacron               Sun Mar 31 21:44  17/636   "Anacron job 'cron.dai"
 N  8 root                  Tue Apr  2 20:00  18/588   "test"
& 8
Message  8:
From [email protected]  Tue Apr  2 20:00:51 2019
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Date: Tue, 02 Apr 2019 20:00:50 +0800
To: [email protected]
Subject: test
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R
 
nihao hello

4、traceroute 命令:显示数据包到主机之间的路径

traceroute 命令可以显示数据包到主机间的路径,基本信息如下:

  • 命令名称:traceroute
  • 所在路径:/bin/traceroute
  • 执行权限:所有用户
  • 功能描述:显示数据包到主机之间的路径

traceroute:路由跟踪命令,下图中 * 是目标禁止ping;

traceroute to www.sina.com.cn (112.25.53.229), 30 hops max, 60 byte packets
 1  192.168.85.2 (192.168.85.2)  0.114 ms  0.081 ms  0.053 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *

5、netstat命令:查看网络状态

netstat 是网络状态查看命令,即可以查看本机开启的端口,也可以查看哪些客户端连接。
netstat 命令的基本信息如下:

  • 命令名称:netstat
  • 所在路径:/bin/netstat
  • 执行权限:所有用户
  • 功能描述:显示网络相关信息

netstat命令常见选项:

  • -t:列出TCP协议端口
  • -u:列出UDP协议端口
  • -n:不使用域名与服务名,而使用IP地址和端口号
  • -l:仅列出在监听状态网络服务(只有TCP有监听状态)
  • -a:列出所有网络连接
  • -r:列出路由列表
  • -p:显示PID和程序名
[root@root 桌面]# netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:39271               0.0.0.0:*                   LISTEN      
tcp        0      0 :::111                      :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
tcp        0      0 :::33766                    :::*                        LISTEN      

查看本机路由表:

[root@root 桌面]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.85.0    0.0.0.0         255.255.255.0   U         0 0          0 eth1
0.0.0.0         192.168.85.2    0.0.0.0         UG        0 0          0 eth1

6、setup命令:配置网络

setup 命令的基本信息如下:

  • 命令名称:setup
  • 所在路径:/usr/bin/setup
  • 执行权限:所有用户
  • 功能描述:配置网络

步骤如下:setup配置IP,网管信息(重启主机后,依然有效);
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7、nslookup命令:进行域名与IP地址解析

nslookup命令的基本信息如下:

  • 命令名称:nslookup
  • 所在路径:/usr/bin/nslookup
  • 执行权限:所有用户
  • 功能描述:进行域名与IP地址解析
[root@root 桌面]# nslookup
> baidu.com
Server:		192.168.85.2
Address:	192.168.85.2#53

Non-authoritative answer:
Name:	baidu.com
Address: 123.125.114.144
Name:	baidu.com
Address: 220.181.38.148
> server
Default server: 192.168.85.2
Address: 192.168.85.2#53

8、route命令:查看路由表条目

route命令的基本信息如下:

  • 命令名称:route
  • 所在路径:/sbin/route
  • 执行权限:root用户
  • 功能描述:查看或设置主机中路由表信息
[root@root 桌面]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.85.0    *               255.255.255.0   U     1      0        0 eth1
default         192.168.85.2    0.0.0.0         UG    0      0        0 eth1

猜你喜欢

转载自blog.csdn.net/weixin_45116657/article/details/94575854