centos 7.3 设置静态IP

Posted ✧*꧁一品堂.技术学习笔记꧂*✧.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos 7.3 设置静态IP相关的知识,希望对你有一定的参考价值。

注:本文来源:张亮博客  的 《centos 7.3 设置静态IP或ping 报name or service not known



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

技术分享图片
  1 [[email protected] /]# ping 127.0.0.1
  2 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
  3 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.106 ms
  4 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.095 ms
  5 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.097 ms
  6 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.073 ms
  7 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.058 ms
  8 64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.097 ms
  9 64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.375 ms
 10 ^C64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.733 ms
 11 64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.095 ms
 12 64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.102 ms
 13 ^C
 14 --- 127.0.0.1 ping statistics ---
 15 10 packets transmitted, 10 received, 0% packet loss, time 9006ms
 16 rtt min/avg/max/mdev = 0.058/0.183/0.733/0.202 ms
 17 [[email protected] /]# 
View Code
在设置静态IP之前可以先看下自己当前开启的网卡:
  1 [[email protected] /]# ifconfig
  2 ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  3         inet 192.168.0.121  netmask 255.255.255.0  broadcast 192.168.0.255
  4         inet6 fe80::9474:4a7c:66fd:8d90  prefixlen 64  scopeid 0x20<link>
  5         ether 00:0c:29:e5:c1:96  txqueuelen 1000  (Ethernet)
  6         RX packets 4436  bytes 372786 (364.0 KiB)
  7         RX errors 0  dropped 0  overruns 0  frame 0
  8         TX packets 871  bytes 147085 (143.6 KiB)
  9         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 10 
 11 lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
 12         inet 127.0.0.1  netmask 255.0.0.0
 13         inet6 ::1  prefixlen 128  scopeid 0x10<host>
 14         loop  txqueuelen 1  (Local Loopback)
 15         RX packets 16  bytes 1320 (1.2 KiB)
 16         RX errors 0  dropped 0  overruns 0  frame 0
 17         TX packets 16  bytes 1320 (1.2 KiB)
 18         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 19 
 20 [[email protected] /]# 


1.设置网卡ens32静态IP


  1 [[email protected] /]# cd /etc/sysconfig/network-scripts/        #注意点cd与“/etc”之间的空格
  2 [[email protected] network-scripts]# 
  3 [[email protected] network-scripts]# ls     #查看network-scripts下的所有文件
  4 ifcfg-ens32  ifdown-eth   ifdown-isdn    ifdown-sit       ifup          ifup-ib    ifup-plip   ifup-routes    ifup-tunnel        network-functions-ipv6
  5 ifcfg-lo     ifdown-ib    ifdown-post    ifdown-Team      ifup-aliases  ifup-ippp  ifup-plusb  ifup-sit       ifup-wireless
  6 ifdown       ifdown-ippp  ifdown-ppp     ifdown-TeamPort  ifup-bnep     ifup-ipv6  ifup-post   ifup-Team      init.ipv6-global
  7 ifdown-bnep  ifdown-ipv6  ifdown-routes  ifdown-tunnel    ifup-eth      ifup-isdn  ifup-ppp    ifup-TeamPort  network-functions
  8 [[email protected] network-scripts]# 
  9 
 10 
 11 使用vi进行编辑
 12 [[email protected] network-scripts]# vi ifcfg-ens32
 13 【点击i 进行插入  完成之后Esc 并输入:wq 进行保存】
 14 [[email protected] network-scripts]# vi ifcfg-ens32
 15 TYPE="Ethernet"
 16 BOOTPROTO="static"  #启用静态IP地址
 17 DEFROUTE="yes"
 18 PEERDNS="yes"
 19 PEERROUTES="yes"
 20 IPV4_FAILURE_FATAL="no"
 21 IPV6INIT="yes"
 22 IPV6_AUTOCONF="yes"
 23 IPV6_DEFROUTE="yes"
 24 IPV6_PEERDNS="yes"
 25 IPV6_PEERROUTES="yes"
 26 IPV6_FAILURE_FATAL="no"
 27 IPV6_ADDR_GEN_MODE="stable-privacy"
 28 NAME="ens32"
 29 UUID="17e371b3-5107-4d72-a58f-e8f71396fd68"
 30 DEVICE="ens32"
 31 ONBOOT="yes"
 32 IPADDR=“192.168.0.121”
 33 GATEWAY=“192.168.0.1”
 34 
 35 修改:
 36 BOOTPROTO=“static”  #启用静态IP地址
 37 加入个:
 38 IPADDR=“192.168.0.121”  #设置IP地址
 39 GATEWAY=“192.168.0.1”  #设置网关


【注意】设置好后保存!

然后重启网络吧:

  1 [[email protected] ~]# service network restart    #重启网络
  2 Restarting network (via systemctl):                        [  OK  ]
  3 [[email protected] ~]# 
  4 
  5 
  6 [[email protected] ~]# ping 192.168.0.107  #ping局域网内的IP正常
  7 PING 192.168.0.107 (192.168.0.107) 56(84) bytes of data.
  8 64 bytes from 192.168.0.107: icmp_seq=1 ttl=64 time=0.759 ms
  9 64 bytes from 192.168.0.107: icmp_seq=2 ttl=64 time=1.07 ms
 10 64 bytes from 192.168.0.107: icmp_seq=3 ttl=64 time=0.615 ms
 11 ^C
 12 --- 192.168.0.107 ping statistics ---
 13 3 packets transmitted, 3 received, 0% packet loss, time 2004ms
 14 rtt min/avg/max/mdev = 0.615/0.816/1.076/0.195 ms
 15 [[email protected] ~]# ping www.baidu.com    #ping百度域名还是不行
 16 ping: www.baidu.com: Name or service not known  #出现这个错误








以上是关于centos 7.3 设置静态IP的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7.3配置静态ip

CentOS 7.3 练习环境的安装搭建。

CentOS 7.3 搭建DHCP服务器

centos怎么设置静态ip

Centos8 设置静态IP

CentOS7设置网卡静态IP