需要添加的几项内容的解释:
IPADDR IP地址
NETMASK 子网掩码
NETWORK 网关地址
通常,如果我们想更改主机地址为静态地址或者更改主机名,需要修改的文件包括:
/etc/resolv.conf 设置DNS
/etc/hosts 设置指定的域名解析地址
/etc/sysconfig/network 设置主机名和网络配置
/etc/sysconfig/network-scripts/ifcfg-eth0 针对特定的网卡进行设置
一般我们只需要修改网卡的配置文件就可以了,例如我的配置文件如下:
DEVICE=eth0
BOOTPROTO=static
TYPE=Ethernet
NAME="System etho0"
BROADCAST=192.168.56.255
HWADDR=08:00:27:24:F8:9B
IPADDR=192.168.56.101
IPV6INIT=yes
IPV6_AUTOCONF=yes
NETMASK=255.255.255.0
NETWORK=192.168.56.1
ONBOOT=yes
设置完成后,重启一下网卡就可以了:
# service network restart
------------- 以下为针对CentOS 7和CentOS 6.5的详细教程 -----------
- CentOS 7
- 在 /etc/sysconfig/network-scripts 路径下找到 ifcfg-* ,* 代表具体网卡,本文修改的网卡是 ifcfg-eth0,你的有可能是 ifcfg-enp0s3 ,除 ONBOOT 和 BOOTPROTO 修改外,其他几项为新增。修改后内容参见下文。
TYPE=Ethernet
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
NAME=enp0s3
UUID=23b2b3e7-e6d5-4a6d-83b4-f6949392a486
DEVICE=enp0s3
#static assignment
ONBOOT=yes #开机启动
BOOTPROTO=static #静态IP
IPADDR=192.168.1.151 #本机地址
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.1.1 #默认网关
- DNS 官方建议在 /etc/sysconfig/network 中配置,比较简单直接给出配置
# Created by anaconda
DNS1=192.168.1.1
DNS2=8.8.8.8
重置网络配置
# service network restart
查看网路配置
shell># ip addr
...
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:0e:35:27 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.151/24 brd 192.168.1.255 scope global enp0s3
...
- CentOS 6.5
# ifconfig -a
--- 若出现的是eth0,且/etc/sysconfig/network-scripts 路径下找到 ifcfg-,其中也是eth0,则按上述步骤进行配置即可;
--- 若出现的不是eth0,而是eth1(eth2/eth3同理,此处以eth1为例),请先/etc/udev/rules.d/70-persistent-net.rules内容如下面例子所示:
# vi /etc/udev/rules.d/70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
# PCI device 0x1022:0x2000 (pcnet32)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:8f:89:9
7", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x1022:0x2000 (pcnet32)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:50:bd:1
7", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
--- 记录下,eth1网卡的mac地址00:0c:29:50:bd:17,接下来
# mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1
# vim /etc/sysconfig/network-scripts/ifcfg-eth1
--- 将 DEVICE="eth0" 改成 DEVICE="eth1" ,
--- 将 HWADDR="00:0c:29:8f:89:97" 改成上面的mac地址HWADDR="00:0c:29:50:bd:17"
--- 最后,重启网络
# service network restart
参考:
Centos下设置静态IP
Centos 7 学习之静态IP设置(续)
CentOS Linux解决Device eth0 does not seem to be present