centos系统下设置固定IP+dns
笔者用的linux系统是centos版本的,在次之前linux是空白,今天我在物理机用XSHELL连接虚拟机中的centos时候出现连接失败的情况,我的第一反应就是IP是不是变了?打开虚拟机之后在终端中输入ifconfig还真是变了,我就想着把IP绑定在虚拟机中的网卡中,方便以后连接不再修改IP,图个方便,下面是步骤:
在终端中输入: vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="dhcp"
HWADDR="00:0C:29:E1:F2:DF"
IPV6INIT="yes"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="17b80e4d-b7f0-4e7d-8887-fe579b8ec03a"
把BOOTPROTO=“dhcp”修改为static或者none,
然后添加IP地址
IPADDR=192.168.10.111(IP地址视自己的而定,我用的是我的)
添加掩码:
NETMASK=255.255.255.0
添加网关:
GATEWAY=192.168.10.1
修改后为:
DEVICE="eth0"
BOOTPROTO="none"
IPADDR=192.168.10.111
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
HWADDR="00:0C:29:E1:F2:DF"
IPV6INIT="yes"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="17b80e4d-b7f0-4e7d-8887-fe579b8ec03a"
然后:wq保存
重启网卡:service network restart
[root@localhost ~]# service network restart
Shutting down interface eth0: Device state: 3 (disconnected)
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Active connection state: activating
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/1
state: activated
Connection activated
[ OK ]
好,现在就绑定好了
[root@localhost ~]# ping www.baidu.com
但是PING不通ip,那就设置下DNS
在终端中输入: [root@localhost ~]# vim /etc/resolv.conf
# Generated by NetworkManager
# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
nameserver 202.98.128.86
nameserver 202.96.128.166
这是固定格式,
现在再ping百度试试
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (180.97.33.108) 56(84) bytes of data.
64 bytes from 180.97.33.108: icmp_seq=1 ttl=54 time=29.1 ms
64 bytes from 180.97.33.108: icmp_seq=2 ttl=54 time=28.6 ms
现在就ping通了