查看及测试网络

#查看及测试网络
查看及测试网络配置是管理Linux网络服务的第一步

查看网络配置

使用ifconfig命令查看网络接口地址

主机的网络接口卡(网卡)通常称为“网络接口”。在Linux系统中,使用ifconfig命令可以查看网络接口的地址配置信息。
查看网络接口信息ifconfig

  • 查看所有活动的网络接口信息
    执行ifconfig命令
  • 查看指定网络接口信息
    ifconfig 网络接口
    (1)查看活动的网络接口设备
    当ifconfig命令不带任何的参数时,将显示当前主机中已启用(活动)的网络接口信息。

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.41 netmask 255.255.255.0 broadcast 20.0.0.255
inet6 fe80::20c:29ff:fe0d:cb11 prefixlen 64 scopeid 0x20
ether 00:0c:29:0d:cb:11 txqueuelen 1000 (Ethernet)
RX packets 3847 bytes 345342 (337.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2066 bytes 189788 (185.3 KiB)
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 1040  bytes 89220 (87.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1040  bytes 89220 (87.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:10:b5:7a  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

其中ens33是pc机的物理网卡
lo (loopback)是回环网络接口,它并不代表真正的网络接口,而是一个虚拟的网络接口,其中IP地址默认是“127.0.0.1”,通常仅用于对本机的网络测试。
virbr0是虚拟桥接网卡

如果想要查看所有网络接口信息,使用 ifconfig -a

(2)查看指定的网络接口信息
当只需要查看其中某一个网络接口的信息时,使用网络接口的名称作为ifconfig命令的参数(不论该网络是否处于激活状态)

[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.41  netmask 255.255.255.0  broadcast 20.0.0.255
        inet6 fe80::20c:29ff:fe0d:cb11  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:0d:cb:11  txqueuelen 1000  (Ethernet)
        RX packets 3939  bytes 352572 (344.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2134  bytes 195926 (191.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在这里插入图片描述

查看主机名称 hostname

  • hostname命令
    查看或设置当前主机名
    hostname [主机名]

      [root@localhost ~]# hostname
      localhost.localdomain
    

查看路由表条目 route

Linux系统中的路由表决定着从本机向其他主机、其他网络发送数据的去向,是排除网络故障的关键信息。直接执行route命令可以查看当前主机中的路由表信息,在输出结果中,Destination列对应目标网段的地址,Gateway列对应下一跳路由器的地址,iface列对应发送数据的网络接口。
route命令
查看或设置主机中路由表信息
route [-n]

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         20.0.0.2        0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

查看网络连接情况 netstat

netstat命令
查看系统的网络连接状态、路由表、接口统计等信息
netstat [选项]
常用选项
-a:显示当前主机中所有活动的网络连接信息(包括监听,非监听状态的服务端口)
-n:以数字的形式显示相关的主机地址,端口等信息
-p:显示与网络连接相关联的进程号,进程名称信息(‘该选项需要root权限’)
-t:查看TCP协议相关信息
-u:显示UDP协议相关的信息
-r:显示路由信息
-l:显示处于监听(listening)状态的网络连接及端口信息
通常使用“-ntap”组合选项,以数字形式显示当前系统中所有的TCP连接信息,同时显示对应的进程信息。
结合管道符号使用“grep”命令,还可以在结果中过滤出所需要的特定记录。

[root@localhost ~]# netstat -anpt | grep ":80"
tcp6       0      0 :::80                   :::*                    LISTEN      9589/httpd   

获取socket统计信息 ss

ss命令
查看系统的网络连接情况,获取socket统计信息
ss [选项]
常用选项
-a:显示当前主机中所有活动的网络连接信息(包括监听,非监听状态的服务端口)
-n:取消服务名称,显示端口
-p:显示与网络连接相关联的进程号,进程名称信息(‘该选项需要root权限’)
-t:查看TCP协议相关信息
-u:显示UDP协议相关的信息
-r:以名称形式显示(默认)
-l:显示处于监听(listening)状态的网络连接及端口信息

[root@localhost ~]# ss -t state established
Recv-Q Send-Q                                      Local Address:Port                                                       Peer Address:Port                
0      0                                               20.0.0.41:ssh                                                            20.0.0.1

测试网络连接

使用ping命令测试网络连通性

ping [选项] 目标主机

[root@localhost ~]# ping 20.0.0.2
PING 20.0.0.2 (20.0.0.2) 56(84) bytes of data.
64 bytes from 20.0.0.2: icmp_seq=1 ttl=128 time=0.173 ms
64 bytes from 20.0.0.2: icmp_seq=2 ttl=128 time=2.66 ms
64 bytes from 20.0.0.2: icmp_seq=3 ttl=128 time=0.520 ms
64 bytes from 20.0.0.2: icmp_seq=4 ttl=128 time=0.215 ms
^C  //按ctrl+C终止测试
--- 20.0.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 0.173/0.894/2.669/1.033 ms

连接正常会收到返回的数据包
反馈目标主机不可达:可能目标地址不存在或主机已经关闭
反馈 网络不可达:表示没有可用的路由记录,如网关,无法到达目标主机所在网络
反馈 请求超时:表示数据到达对方,但回不来

网络的排障:
自测tcp/ip 127.0.0.1/24
自ping本地网卡地址 如果不通:需要重新配置网络服务功能
ping网关 如果不通:网关地址或者网线有问题
ping外网服务器地址 如果不通:ISP运营商或者对方服务器有问题

跟踪数据包 traceroute

traceroute命令
测试从当前主机到目的主机之间经过的网络节点号
traceroute 目标主机地址

[root@localhost ~]# traceroute 104.193.88.77
traceroute to 104.193.88.77 (104.193.88.77), 30 hops max, 60 byte packets
 1  gateway (20.0.0.2)  0.332 ms  0.246 ms  0.180 ms
 2  * * *  //做NAT转换会以* * *显示
... //省略部分内容

使用nslookup命令测试DNS域名解析

当域名解析出现异常时,将无法使用域名的形式访问网络中的Web站点、电子邮件系统等服务。
nslookup命令
测试DNS域名解析,将域名解析为IP地址
nslookup 目标主机地址 [DNS服务器地址]

[root@localhost ~]# nslookup www.baidu.com
Server:		8.8.8.8    //DNS服务器地址
Address:	8.8.8.8#53  //DNS服务端口号是53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
www.a.shifen.com	canonical name = www.wshifen.com.
Name:	www.wshifen.com
Address: 104.193.88.123  //对方的服务器地址
Name:	www.wshifen.com
Address: 104.193.88.77  //对方的服务器地址

在这里插入图片描述

使用dig命令测试DNS域名解析

dig命令是Linux专有的
比nslookup详细

[root@localhost system]# dig www.baidu.com

; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55338
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		5	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	5	IN	A	180.101.49.12
www.a.shifen.com.	5	IN	A	180.101.49.11

;; Query time: 9 msec
;; SERVER: 20.0.0.2#53(20.0.0.2)
;; WHEN: 四 7月 02 22:48:13 CST 2020
;; MSG SIZE  rcvd: 90

设置网络地址参数

在Linux主机中手动修改网络配置包括两种最基本的方法

  • 临时配置:使用命令调整网络参数
    简单,快速,可直接修改运行中的网络参数
    一般只适合在调试网络的过程中使用
    系统重启以后,所做的修改将会直接失效

  • 固定设置:通过配置文件修改网络参数
    修改各项网络参数的配置文件
    适合对服务器设置固定参数时使用
    需要重载网络服务或者重启以后才会生效

设置网络接口参数 ifconfig

  • 设置网络接口的IP地址,子网掩码
    ifconfig 接口名 IP地址 [netmask 子网掩码]
    ifconfig 网络接口 IP地址[/掩码长度]

    [root@localhost ~]# ifconfig ens33 10.0.0.100/24
    [root@localhost ~]# ifconfig ens33
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
    inet6 fe80::20c:29ff:fe0d:cb11 prefixlen 64 scopeid 0x20
    ether 00:0c:29:0d:cb:11 txqueuelen 1000 (Ethernet)
    RX packets 4530 bytes 401835 (392.4 KiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 2503 bytes 231317 (225.8 KiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

  • 禁用或者重新激活网卡
    ifconfig 网络接口 up
    ifconfig 网络接口 down

      [root@localhost ~]# ifconfig ens33 down
      [root@localhost ~]# ifconfig 
      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 1040  bytes 89220 (87.1 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 1040  bytes 89220 (87.1 KiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      
      virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
              inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
              ether 52:54:00:10:b5:7a  txqueuelen 1000  (Ethernet)
              RX packets 0  bytes 0 (0.0 B)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 0  bytes 0 (0.0 B)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
      [root@localhost ~]# ifconfig ens33 up
      [root@localhost ~]# ifconfig 
      ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              ether 00:0c:29:0d:cb:11  txqueuelen 1000  (Ethernet)
              RX packets 4547  bytes 403239 (393.7 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 2516  bytes 233724 (228.2 KiB)
              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 1040  bytes 89220 (87.1 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 1040  bytes 89220 (87.1 KiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      
      virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
              inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
              ether 52:54:00:10:b5:7a  txqueuelen 1000  (Ethernet)
              RX packets 0  bytes 0 (0.0 B)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 0  bytes 0 (0.0 B)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
  • 为网卡绑定虚拟接口
    在对服务器网络进行调试的过程中,有时候需要临时在同一个网卡上使用一个新的IP地址,但是又不能够覆盖掉原本的IP地址而导致服务程序不可用。
    可以为网卡绑定一个虚拟的网络接口,然后在位虚拟接口配置新的IP地址(相当于一个网卡配置多个IP地址)
    ifconfig 接口名:序号 IP地址

      [root@localhost ~]# ifconfig ens33:0 10.0.0.200
      [root@localhost ~]# ifconfig 
      ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              ether 00:0c:29:0d:cb:11  txqueuelen 1000  (Ethernet)
              RX packets 4547  bytes 403239 (393.7 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 2516  bytes 233724 (228.2 KiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      
      ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 10.0.0.200  netmask 255.0.0.0  broadcast 10.255.255.255
              ether 00:0c:29:0d:cb:11  txqueuelen 1000  (Ethernet)
      
      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 1040  bytes 89220 (87.1 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 1040  bytes 89220 (87.1 KiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      
      virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
              inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
              ether 52:54:00:10:b5:7a  txqueuelen 1000  (Ethernet)
              RX packets 0  bytes 0 (0.0 B)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 0  bytes 0 (0.0 B)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    

设置路由记录 route

添加到指定网段的路由记录
route add -net 网段地址 gw IP地址

	[root@localhost ~]# route add -net 192.168.122.0/24 gw 20.0.0.2
	[root@localhost ~]# route
	Kernel IP routing table
	Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
	default         gateway         0.0.0.0         UG    100    0        0 ens33
	20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
	192.168.122.0   20.0.0.100      255.255.255.0   UG    0      0        0 ens33
	192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

删除到指定网段的路由记录
route del -net 网段地址

root@localhost ~]# route del -net 192.168.122.0/24
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

向路由表中添加默认网关记录
route add default gw IP地址

[root@localhost ~]# route add default gw 192.168.122.100
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 virbr0
default         gateway         0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

删除路由表中的默认网关记录
route del default gw IP地址

[root@localhost ~]# route del default gw 192.168.122.100
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

修改主机名 hostname

hostname命令
设置主机名
hostname 主机名称
临时修改

[root@localhost ~]# hostname zhangsan
[root@localhost ~]# hostname
zhangsan
[root@localhost ~]# bash
[root@zhangsan ~]# 

永久修改

[root@zhangsan ~]# vi /etc/hostname
localhost.localdomain
改成
zhangsan.localdomain

在这里插入图片描述

网络接口配置文件

/etc/sysconfig/network-scripts/ifcfg-ens33/ 目录下
ifcfg-ens33:第一块以太网卡的配置文件

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none 
BROWSER_ONLY=no 
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes 
IPV6_AUTOCONF=yes 
IPV6_DEFROUTE=yes 
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=d373a72c-2f22-4554-90b5-986eebf7c1ac
DEVICE=ens33
ONBOOT=yes
IPADDR=20.0.0.51
NETMASK=255.255.255.0 
GATEWAY=20.0.0.2
DNS1=8.8.8.8
  • 启动,禁用网络接口配置
    重启network网络服务

      [root@localhost ~]# systemctl restart network
              或者
      [root@localhost ~]# service network restart
    
  • 禁用,启用网络接口

      [root@localhost ~]# ifdown ens33		
      [root@localhost ~]# ifup ens33		
    

域名解析配置文件

/etc/resolv.conf文件
保存本机需要使用的DNS服务器的IP地址
[root@localhost ~]# vim /etc/resolv.conf
对该文件所做的修改会立刻生效
Linux系统中最多可以指定3个(第三个以后将被忽略)不同的DNS服务器地址,优先使用第一个DNS服务器
注意:CentOS7需要在NetworkManger.conf文件main段内设置dns=none,并且重启NetworkManger服务,或者使用CentOS7新添加nmcli命令进行设置

[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# systemctl disable NetworkManager
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.

本地主机映射文件

/etc/hosts文件
保存主机名与IP地址的映射记录
一般用来保存经常需要访问的主机的信息

hosts文件和DNS服务器的比较
默认情况下,系统首先从hosts文件查找解析记录
当访问一个未知的域名时,先查找该文件中是否有相应的映射记录,如果找不到在去向DNS服务器查询
hosts文件只对当前的主机有效,因为hosts文件只保存在本地
hosts文件可减少DNS查询过程,从而加快访问速度
在/etc/hosts文件中添加正确的映射记录(经常访问的一些网站),减少了DNS查询,提高了上网速度

DNS配置

方法一:修改 /etc/resolv.conf
nameserver 域名服务器ID
[root@localhost ~]# echo “nameserver 20.0.0.2” > /etc/resolv.conf
在这里插入图片描述
方法二:修改/etc/sysconfig/network-scripts/ifcfg-ens33/ 目录
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
在最后插入一行
DNS1=8.8.8.8
//也可以配置成网关20.0.0.2 (8.8.8.8 114.114.114.114是外部的DNS)
方法三:配置本地解析
[root@localhost ~]# vim /etc/hosts
140.193.88.123 www.baidu.com
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值