CentOS6或7最小安裝後設置聯網

centos6:html

1. 虛擬機網絡鏈接方式設置成NAT。linux

2. window系統下的兩個服務VMwareDHCP Service和VMware NAT Service須要啓動。shell

3. 用vi編輯文件/etc/sysconfig/network-scripts/ifcfg-eth0,個人系統中沒有雙引號,以下所示:centos

BOOTPROTO=dhcp(沒變)
ONBOOT=yes(修改以前是no)
NM_CONTROLLED=no(修改以前是yes)網絡

修改完以後,ZZ保存退出。less

4. 再輸入service network restart。dom

 

設置靜態ip
編輯網卡文件編輯器

vi /etc/sysconfig/network-scripts/ifcfg-eth0oop

設置網卡eth0的IPV4信息,須要注意的是,設置的IPADDR須要和局域網中其餘機器處於同一網段。需設置容以下:post

DEVICE=eth0

BOOTPROT=static

IPADDR=192.168.1.101

GATEWAY=192.168.1.1

NETMASK=255.255.255.0

ONBOOT= yes

網卡文件內容示例說明:

DEVICE=eth0                  // 指出設備名稱

BOOTPROT=static              // 啓動類型 dhcp|static

BROADCAST=192.168.1.203      // 廣播地址

HWADDR=00:06:5B:FE:DF:7C     // 硬件Mac地址

IPADDR=192.168.0.2           //IP 地址

NETMASK=255.255.255.0        // 子網掩碼

NETWORK=192.168.0.0          // 網絡地址

GATEWAY=192.168.0.1          // 網關地址

ONBOOT= yes                   // 是否啓動應用

TYPE=Ethernet                // 網絡類型

重啓網卡

service network restart

重啓網卡時,shell中可能會有從新配置IP的相關信息出如今shell中。

經過ping局域網中其餘主機的IP來測試配置是否成功

ping 192.168.1.10

注意:若是是在VirtualBox中配置,須要把網絡選項卡中的鏈接方式改成橋接。

 

CentOS7.2 設置靜態ip

先找到配置文件:

[root@localhost bin]# cd /etc/sysconfig/network-scripts
[root@localhost 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
[root@localhost 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

[root@localhost ~]# 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,可是重啓系統之後仍是變成了動態的,

[root@localhost ~]# 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
[root@localhost ~]# 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.
[root@localhost ~]# chkconfig network on
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# 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

若是還出現

[hongdada@localhost ~]$ 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

那麼。。。

查看https://blog.csdn.net/kokuma/article/details/72876106

可使用journel -xe查看日誌,通常就是修改配置文件裏面的Name,DEVICE 爲ens33

首先使用ip addr查看網卡,而後查看ifconfig

通常若是是第一次的,就是否是克隆的話,網卡名稱爲eno16777736,這裏只要設定靜態ip便可,若是是克隆的話,ip addr裏面會發現網卡名稱變成了ens33,這樣的話,咱們也必須在/etc/sysconfig/network-scripts/eno167777736文件裏面把NAME,DEVICE裏面的改爲ens33,從新設定靜態IP,重啓便可。

http://www.cnblogs.com/Leo_wl/p/5484136.html

http://www.bbsmax.com/A/n2d9D39v5D/

http://www.178linux.com/9928

相關文章
相關標籤/搜索