VirtualBox爲虛擬機服務器設置固定IP

  1. 服務器版本 CentOS 7.5
  2. VirtualBox配置網絡爲橋接模式
  3. 物理機(宿主機)信息
ip:192.168.0.104
subnet mask:255.255.255.0
gateway:192.168.0.1
  1. 查看服務器ip信息
>ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:af:44:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.106/24 brd 192.168.0.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::xxx:xxxx:xxxx:4488/64 scope link
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:xx:xx:xx:xx:xx 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 group default qlen 1000
    link/ether 52:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

服務器當前ip: 192.168.0.106, 網卡: enp0s3vim

  1. 修改配置文件
>vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE="Ethernet"
BOOTPROTO="static" #靜態方式
NM_CONTROLLED="no" #不使用網絡管理器

DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp0s3"
UUID="2b4bcec5-6c11-411b-b7b5-177d6b1075b2"
DEVICE="enp0s3"
ONBOOT="yes"

IPADDR=192.168.0.110 #設置固定ip
NETMASK=255.255.255.0 #子網掩碼爲物理機子網掩碼(subnet mask)
GATEWAY=192.168.0.1 #設置網關爲物理機網關(gateway)
  1. 重啓網卡
>service network restart
  1. 添加DNS服務器
>vim /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 114.114.114.114 #114DNS
nameserver 114.114.114.115
  1. 驗證ip
>ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:af:44:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.110/24 brd 192.168.0.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::xxx:xxxx:xxxx:4488/64 scope link
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:xx:xx:xx:xx:xx 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 group default qlen 1000
    link/ether 52:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
>ping www.baidu.com #可ping通

服務器當前ip: 192.168.0.110, 網卡: enp0s3bash

相關文章
相關標籤/搜索