centos 7.3 设置静态IP或ping

原文链接:https://blog.csdn.net/zl570932980/article/details/78260951

首先把虚拟机配置为桥接模式,然后开启
再你打算修改虚拟机IP之前首先ping下127.0.0.1看看网卡是否是好的(如果127.0.0.1都ping不通的话,那网卡就是坏的了…)

[root@localhost /]# ping 127.0.0.1
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.106 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.095 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.097 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.073 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.058 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.097 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.375 ms
^C64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.733 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.095 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.102 ms
^C
--- 127.0.0.1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9006ms
rtt min/avg/max/mdev = 0.058/0.183/0.733/0.202 ms
[root@localhost /]# 

在设置静态IP之前可以先看下自己当前开启的网卡:

[root@localhost /]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.121  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::9474:4a7c:66fd:8d90  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e5:c1:96  txqueuelen 1000  (Ethernet)
        RX packets 4436  bytes 372786 (364.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 871  bytes 147085 (143.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 1  (Local Loopback)
        RX packets 16  bytes 1320 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1320 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost /]# 

1.设置网卡ens32静态IP

[root@localhost /]# cd /etc/sysconfig/network-scripts/        #注意点cd与“/etc”之间的空格
[root@localhost network-scripts]# 
[root@localhost network-scripts]# ls     #查看network-scripts下的所有文件
ifcfg-ens32  ifdown-eth   ifdown-isdn    ifdown-sit       ifup          ifup-ib    ifup-plip   ifup-routes    ifup-tunnel        network-functions-ipv6
ifcfg-lo     ifdown-ib    ifdown-post    ifdown-Team      ifup-aliases  ifup-ippp  ifup-plusb  ifup-sit       ifup-wireless
ifdown       ifdown-ippp  ifdown-ppp     ifdown-TeamPort  ifup-bnep     ifup-ipv6  ifup-post   ifup-Team      init.ipv6-global
ifdown-bnep  ifdown-ipv6  ifdown-routes  ifdown-tunnel    ifup-eth      ifup-isdn  ifup-ppp    ifup-TeamPort  network-functions
[root@localhost network-scripts]# 


使用vi进行编辑
[root@localhost network-scripts]# vi ifcfg-ens32
【点击i 进行插入  完成之后Esc 并输入:wq 进行保存】
[root@localhost network-scripts]# vi ifcfg-ens32
TYPE="Ethernet"
BOOTPROTO="static"  #启用静态IP地址
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens32"
UUID="17e371b3-5107-4d72-a58f-e8f71396fd68"
DEVICE="ens32"
ONBOOT="yes"
IPADDR=“192.168.0.121”
GATEWAY=“192.168.0.1”

修改:
BOOTPROTO=“static”  #启用静态IP地址
加入个:
IPADDR=“192.168.0.121”  #设置IP地址
GATEWAY=“192.168.0.1”  #设置网关

【注意】设置好后保存!

然后重启网络吧:

[root@localhost ~]# service network restart    #重启网络
Restarting network (via systemctl):                        [  OK  ]
[root@localhost ~]# 


[root@localhost ~]# ping 192.168.0.107  #ping局域网内的IP正常
PING 192.168.0.107 (192.168.0.107) 56(84) bytes of data.
64 bytes from 192.168.0.107: icmp_seq=1 ttl=64 time=0.759 ms
64 bytes from 192.168.0.107: icmp_seq=2 ttl=64 time=1.07 ms
64 bytes from 192.168.0.107: icmp_seq=3 ttl=64 time=0.615 ms
^C
--- 192.168.0.107 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.615/0.816/1.076/0.195 ms
[root@localhost ~]# ping www.baidu.com    #ping百度域名还是不行
ping: www.baidu.com: Name or service not known  #出现这个错误

ping: www.baidu.com: Name or service not known”错误解决办法
第一步查看ONBOOT:

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens32  ifdown-bnep  ifdown-ippp  ifdown-post    ifdown-sit       ifdown-tunnel  ifup-bnep  ifup-ippp  ifup-plip   ifup-ppp     ifup-Team      ifup-wireless      network-functions-ipv6
ifcfg-lo     ifdown-eth   ifdown-ipv6  ifdown-ppp     ifdown-Team      ifup           ifup-eth   ifup-ipv6  ifup-plusb  ifup-routes  ifup-TeamPort  init.ipv6-global
ifdown       ifdown-ib    ifdown-isdn  ifdown-routes  ifdown-TeamPort  ifup-aliases   ifup-ib    ifup-isdn  ifup-post   ifup-sit     ifup-tunnel    network-functions
[root@localhost network-scripts]# vi ifcfg-ens32
TYPE="Ethernet"
BOOTPROTO="static"  #启用静态IP地址
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens32"
UUID="17e371b3-5107-4d72-a58f-e8f71396fd68"
DEVICE="ens32"
ONBOOT="yes"
IPADDR=“192.168.0.121”
GATEWAY=“192.168.0.1”

发现ONBOOT=”no”已设置为ONBOOT=”yes”

第二步查看:
ping 命令是属于ICMP协议,ping ip地址有效。若直接ping网址(域名),需要配置DNS。编辑添加nameserver如下:

vi /etc/resolv.conf 
nameserver 8.8.8.8 
nameserver 8.8.4.4

【注意】设置完保存!

再ping
[root@localhost /]# 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 (61.135.169.121): icmp_seq=1 ttl=55 time=16.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=25.6 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=21.6 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=55 time=17.2 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=55 time=22.0 ms
^C
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4010ms
rtt min/avg/max/mdev = 16.901/20.716/25.652/3.277 ms
[root@localhost /]# 
到这里就解决了!
上一篇:CentOS 7中网络设置


下一篇:【Linux】一步一步学Linux——ifdown命令(153)