Linux网络设置

Linux网络设置

    • 查看网络配置
    • 测试网络连接
    • 使用网络配置命令
    • 修改网络配置文件

查看网络配置

查看所有活动的网络接口信息

[root@localhost ~]# ifconfig  //执行ifconfig命令
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500  //centos7默认主网卡
        inet 192.168.177.3  netmask 255.255.255.0  broadcast 192.168.177.255
        inet6 fe80::20c:29ff:fed2:bab2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:ba:b2  txqueuelen 1000  (Ethernet)
        RX packets 6453  bytes 560140 (547.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2847  bytes 473775 (462.6 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 74  bytes 6368 (6.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 74  bytes 6368 (6.2 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:47:38:1b  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 ~]# 

查看所有的网络接口信息

[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.177.3  netmask 255.255.255.0  broadcast 192.168.177.255
        inet6 fe80::20c:29ff:fed2:bab2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:ba:b2  txqueuelen 1000  (Ethernet)
        RX packets 7314  bytes 633884 (619.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3242  bytes 525591 (513.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:0c:29:d2:ba:bc  txqueuelen 1000  (Ethernet)
        RX packets 2642  bytes 230098 (224.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 773  bytes 89152 (87.0 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 74  bytes 6368 (6.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 74  bytes 6368 (6.2 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:47:38:1b  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

virbr0-nic: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:47:38:1b  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 ~]# 

查看指定网络接口信息

[root@localhost ~]# ifconfig ens33  //查看ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.177.3  netmask 255.255.255.0  broadcast 192.168.177.255
        inet6 fe80::20c:29ff:fed2:bab2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:ba:b2  txqueuelen 1000  (Ethernet)
        RX packets 6523  bytes 566026 (552.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2876  bytes 477459 (466.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# 

在主机的/etc/hosts下面有解析的话,显示主机的IP

[root@localhost ~]# cat /etc/hosts   //查看文件内容
127.0.0.1  localhost localhost.localdomain localhost6 localhost6.localdomain6
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# hostname -i   //显示解析的ip
::1 127.0.0.1 
[root@localhost ~]# 

查看本机路由表

route  /  route -n  /  ip route  /  ip  route show   / netstat -r     
[root@localhost ~]# route -n  //其他命令显示结果相似,此处不在演示
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.177.2   0.0.0.0         UG    0      0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.177.0   0.0.0.0         255.255.255.0   U     0      0        0 ens33
[root@localhost ~]# 

测试网络连接

测试本机TCP/IP协议是否正常

[root@localhost ~]# ping 127.0.0.1  //ping本地环回口IP,有回包则正常
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.034 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.063 ms
^C
--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.034/0.053/0.063/0.014 ms
[root@localhost ~]# 

测试到网关的是否正常

[root@localhost ~]# ping 192.168.177.2
PING 192.168.177.2 (192.168.177.2) 56(84) bytes of data.
64 bytes from 192.168.177.2: icmp_seq=1 ttl=128 time=0.191 ms
64 bytes from 192.168.177.2: icmp_seq=2 ttl=128 time=0.290 ms
64 bytes from 192.168.177.2: icmp_seq=3 ttl=128 time=0.274 ms
^C
--- 192.168.177.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.191/0.251/0.290/0.047 ms
[root@localhost ~]# 

测试DNS是否正常

[root@localhost ~]# ping 114.114.114.114
PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1 ttl=128 time=4.94 ms
64 bytes from 114.114.114.114: icmp_seq=2 ttl=128 time=4.39 ms
64 bytes from 114.114.114.114: icmp_seq=3 ttl=128 time=4.68 ms
^C
--- 114.114.114.114 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 4.390/4.674/4.947/0.234 ms
[root@localhost ~]# 

测试www.baidu.com

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (180.101.49.12) 56(84) bytes of data.
64 bytes from 180.101.49.12 (180.101.49.12): icmp_seq=1 ttl=128 time=4.89 ms
64 bytes from 180.101.49.12 (180.101.49.12): icmp_seq=2 ttl=128 time=4.89 ms
64 bytes from 180.101.49.12 (180.101.49.12): icmp_seq=3 ttl=128 time=6.03 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 4.895/5.276/6.038/0.541 ms
[root@localhost ~]# 

ping命令的使用

ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面>][-l<前置载入>][-p<范本样式>][-s<数据包大小>][-t<存活数值>][主机名称或IP地址]
-d 使用Socket的SO_DEBUG功能。
-c <完成次数> 设置完成要求回应的次数。
-f 极限检测。
-i<间隔秒数> 指定收发信息的间隔时间。
-I<网络界面> 使用指定的网络接口送出数据包。
-l<前置载入> 设置在送出要求信息之前,先行发出的数据包。
-n 只输出数值。
-p<范本样式> 设置填满数据包的范本样式。
-q 不显示指令执行过程,开头和结尾的相关信息除外。
-r 忽略普通的Routing Table,直接将数据包送到远端主机上。
-R 记录路由过程。
-s<数据包大小> 设置数据包的大小。
-t<存活数值> 设置存活数值TTL的大小。
-v 详细显示指令的执行过程。
-w <deadline> 在 deadline 秒后退出。
-W <timeout> 在等待 timeout 秒后开始执行。

路由跟踪

[root@localhost ~]# traceroute www.baidu.com  //由于被防火墙限制,所以看不到中间经过哪些路由器
traceroute to www.baidu.com (180.101.49.11), 30 hops max, 60 byte packets
 1  gateway (192.168.177.2)  0.099 ms  0.110 ms  0.073 ms
 2  * * *
 3  * * *
 4  * * *
    。。。
28  * * *
29  * * *
30  * * *
[root@localhost ~]# 

使用网络配置命令

停用网络接口

ifdown ens33  / ifconfig ens33 down
[root@localhost ~]# ifdown ens33   //停用网卡之后,导致此网卡不可用,ssh断开

Socket error Event: 32 Error: 10053.
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(centos1) at 15:01:26.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

启用网络接口

ifup  ens33 / ifconfig ens33 up
[root@localhost ~]# ifup ens33   //启用接口
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.177.3  netmask 255.255.255.0  broadcast 192.168.177.255
        inet6 fe80::20c:29ff:fed2:bab2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:ba:b2  txqueuelen 1000  (Ethernet)
        RX packets 8843  bytes 767144 (749.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4078  bytes 618367 (603.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[C:\~]$ ssh 192.168.177.3  //新开一个窗口,尝试以ens33地址ssh,成功


Connecting to 192.168.177.3:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Wed May 19 15:00:23 2021 from 192.168.213.1
[root@localhost ~]# 

测试环境需要临时修改密码

[root@localhost ~]# ifconfig ens33 192.168.177.10/24  //临时修改密码,ssh断开

Socket error Event: 32 Error: 10053.
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.177.3:22) at 16:14:57.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

[root@localhost ~]# ifconfig ens33   //重连,查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.177.10  netmask 255.255.255.0  broadcast 192.168.177.255
        inet6 fe80::20c:29ff:fed2:bab2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:ba:b2  txqueuelen 1000  (Ethernet)
        RX packets 9268  bytes 807847 (788.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4291  bytes 650158 (634.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# 

测试环静,需要多个ip,可临时创建虚拟网卡

[root@localhost ~]# ifconfig ens33:0 192.168.177.4/24  //临时创建,重启失效,多块网卡则依次从0开始向上加
[root@localhost ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.177.4  netmask 255.255.255.0  broadcast 192.168.177.255
        ether 00:0c:29:d2:ba:b2  txqueuelen 1000  (Ethernet)

[root@localhost ~]# 

修改网络配置文件

网卡配置文件在 /etc/sysconfig/network-scripts/

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens33  ifdown-ib    ifdown-routes    ifup-aliases  ifup-isdn    ifup-sit          network-functions
ifcfg-ens37  ifdown-ippp  ifdown-sit       ifup-bnep     ifup-plip    ifup-Team         network-functions-ipv6
ifcfg-lo     ifdown-ipv6  ifdown-Team      ifup-eth      ifup-plusb   ifup-TeamPort
ifdown       ifdown-isdn  ifdown-TeamPort  ifup-ib       ifup-post    ifup-tunnel
ifdown-bnep  ifdown-post  ifdown-tunnel    ifup-ippp     ifup-ppp     ifup-wireless
ifdown-eth   ifdown-ppp   ifup             ifup-ipv6     ifup-routes  init.ipv6-global
[root@localhost network-scripts]# 

修改ens33配置文件

[root@localhost network-scripts]# vi ifcfg-ens33

TYPE=Ethernet              //网卡类型:为以太网
PROXY_ METHOD=none         //代理方式:关闭状态
BROWSER_ ONLY=no           //只是浏览器:否
BOOTPROTO=dhcp             //网卡协议DHCP动态主机配置协议
DEFROUTE=yes               //默认路由:是
IPV4_ FAILURE_ FATAL=no    //否开启IPV4致命错误检测:否
PV6INIT=yes      //IPV6是否自动初始化:是
IPV6_ AUTOCONF="yes"       //IPV6是否自动配置:是
IPV6_ ADDR _GEN _MODE=stable- privacy     // IPV6地址生成模型
stable-privacy是一种生成IPV6的策略
NAME=ens33                 //网卡名字(与DEVICE- 致)
UUID=6ac17c2b-473e- -4894-811b-4940ee7bf455     //唯一标识码
DEVICE=ens33               //网卡设备
ONBOOT=yes                 //开机是否激活网卡
IPADDR=192.168.177.3       //ip地址(static设置 )
NETMASK=255.255.255.0      //子网掩码
GATEWAY= 192.168.177.2     //网关
DNS1=114.114.114.1 14      //dns地址解析(即使只有一个也要写DNS1)
              

你可能感兴趣的:(linux)