需要添加的幾項內容的解釋:
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