1. 程式人生 > >CentOS7.2 設定靜態ip

CentOS7.2 設定靜態ip

先找到配置檔案:

複製程式碼

[[email protected] bin]# cd /etc/sysconfig/network-scripts
[[email protected] network-scripts]# ls
ifcfg-eno16777736  ifdown-ppp       ifup-ib      ifup-Team
ifcfg-lo           ifdown-routes    ifup-ippp    ifup-TeamPort
ifdown             ifdown-sit       ifup-ipv6    ifup-tunnel
ifdown-bnep        ifdown-Team      ifup-isdn    ifup-wireless
ifdown-eth         ifdown-TeamPort  ifup-plip    init.ipv6-global
ifdown-ib          ifdown-tunnel    ifup-plusb   network-functions
ifdown-ippp        ifup             ifup-post    network-functions-ipv6
ifdown-ipv6        ifup-aliases     ifup-ppp
ifdown-isdn        ifup-bnep        ifup-routes
ifdown-post        ifup-eth         ifup-sit
[
[email protected]
network-scripts]# gedit ifcfg-eno16777736

複製程式碼

開始配置是:

複製程式碼

TYPE="Ethernet"
BOOTPROTO="dhcp"
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="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
ONBOOT="yes"

複製程式碼

修改成:

複製程式碼

TYPE="Ethernet"
#BOOTPROTO="dhcp"
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="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
#ONBOOT="yes"

#static assignment
NM_CONTROLLED=no #表示該介面將通過該配置檔案進行設定,而不是通過網路管理器進行管理
ONBOOT=yes #開機啟動
BOOTPROTO=static #靜態IP
IPADDR=192.168.1.122 #本機地址
NETMASK=255.255.255.0 #子網掩碼
GATEWAY=192.168.1.1 #預設閘道器

複製程式碼

修改/etc/sysconfig/network

# Created by anaconda
NETWORKING=yes
GATEWAY=192.168.1.1
DNS1=119.29.29.29
DNS2=182.254.116.116

然後重啟服務即可

service network restart

一般這樣就可以,但是如果有問題

必須要在network-scripts裡面的預設的檔案裡面新增HWADDR

複製程式碼

[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:a0:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.104/24 brd 192.168.1.255 scope global dynamic eno16777736
       valid_lft 7141sec preferred_lft 7141sec
    inet 192.168.1.122/24 brd 192.168.1.255 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::ac30:2bcb:e910:3ae/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff

複製程式碼

先檢視具體的 link/ether

再賦值到eno16777736中

比如:

複製程式碼

TYPE="Ethernet"
#BOOTPROTO="dhcp"
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="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
#ONBOOT="yes"

#static assignment
NM_CONTROLLED=no #表示該介面將通過該配置檔案進行設定,而不是通過網路管理器進行管理
ONBOOT=yes #開機啟動
BOOTPROTO=static #靜態IP
IPADDR=192.168.1.122 #本機地址
NETMASK=255.255.255.0 #子網掩碼
GATEWAY=192.168.1.1 #預設閘道器

HWADDR=00:0c:29:a7:a0:8c

複製程式碼

設定開機啟動一個名為NetworkManager-wait-online服務,命令為:

systemctl enable NetworkManager-wait-online.service

重啟服務。

=======================================================================

錯誤的經驗:

誤以為是橋接模式,後來發現是NAT,沒有設定虛擬機器的 虛擬網路編輯器,坑!

修改成:

複製程式碼

TYPE="Ethernet"
BOOTPROTO="static"
NM_CONTROLLED=no #表示該介面將通過該配置檔案進行設定,而不是通過網路管理器進行管理

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="eno16777736"
UUID="157c8fb5-23b3-414c-8519-25bef9b20a54"
DEVICE="eno16777736"
ONBOOT="yes" #系統將在啟動時開啟該介面。
 
IPADDR=192.168.1.122 #靜態IP  
GATEWAY=192.168.1.1 #預設閘道器  
NETMASK=255.255.255.0 #子網掩碼  
#DNS1=192.168.7.1 #DNS 配置

複製程式碼

重啟網路服務:

service network restart  

重啟網路服務以後ok,但是重啟系統以後還是變成了動態的,

複製程式碼

[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:a0:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.104/24 brd 192.168.1.255 scope global dynamic eno16777736
       valid_lft 6995sec preferred_lft 6995sec
    inet 192.168.1.122/24 brd 192.168.1.255 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::9283:cd42:a281:7a9b/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:74:cb:1a brd ff:ff:ff:ff:ff:ff
[[email protected] ~]# systemctl disable NetworkManager.service
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
[[email protected] ~]# chkconfig network on
[[email protected] ~]# systemctl stop NetworkManager
[[email protected] ~]# systemctl restart network

複製程式碼

可以從上面看出兩個ip

192.168.1.104

192.168.1.122

解決方法:

systemctl disable NetworkManager.service
chkconfig network on
systemctl stop NetworkManager
systemctl restart network

如果還出現

複製程式碼

[[email protected] ~]$ systemctl status network.service
● network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since 日 2017-12-03 23:05:01 CST; 2min 45s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3957 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=1/FAILURE)

12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain network[3957]: RTNETLINK answers: File exists
12月 03 23:05:01 localhost.localdomain systemd[1]: network.service: control process exited, code=exited status=1
12月 03 23:05:01 localhost.localdomain systemd[1]: Failed to start LSB: Bring up/down networking.
12月 03 23:05:01 localhost.localdomain systemd[1]: Unit network.service entered failed state.
12月 03 23:05:01 localhost.localdomain systemd[1]: network.service fail

複製程式碼

那麼。。。