VirtualBox中虛擬機安裝完畢之後,配置網絡選擇橋接模式,然後指定使用宿主機的哪塊網卡。
登錄虛擬機進行IP配置,有兩種方式:dhcp/static,後者還需要配置具體的IP地址,子網掩碼,網關地址,DNS地址。
動態分配(dhcp)配置:
進入目錄:/etc/sysconfig/network-scripts, 編輯其中的相應的網卡的配置文件:ifcfg-<網卡名>
TYPE="Ethernet" #網絡類型爲以太網
BOOTPROTO="dhcp" #動態獲取ip
NAME="enp0s3" #系統分配的網卡名稱,ip addr 命令查看網卡信息
ONBOOT="yes" #開機啓動網卡
靜態IP(static)配置:
進入目錄:/etc/sysconfig/network-scripts, 編輯其中的相應的網卡的配置文件:ifcfg-<網卡名>
TYPE="Ethernet" #網絡類型爲以太網
BOOTPROTO="static" #靜態配置ip
NAME="enp0s3" #系統分配的網卡名稱,ip addr 命令查看網卡信息
ONBOOT="yes" #開機啓動網卡
IPADDR="192.168.0.128" #靜態IP地址
NETMASK="255.255.255.0" #子網掩碼
GATEWAY="192.168.0.1" #網關地址
DNS1="10.1.12.2" #第一DNS地址
這裏靜態ip的選擇要根據宿主機的ip段,虛擬機的ip要和宿主機的ip在同一網段,使用相同的子網掩碼,網關和DNS。保存配置文件後要重啓網絡服務: systemctl restart network.
nmcli 工具可以用來配置網卡,簡化操作。
nmcli connection show #查看網卡的名字
nmcli connection modify enp0s3 connection.autoconnect yes ipv4.method auto #配置動態分配IP
#下面命令用來靜態分配IP
nmcli connection modify enp0s3 ipv4.address 192.168.20.111/24
nmcli connection modify enp0s3 ipv4.gateway 192.168.20.2
nmcli connection modify enp0s3 ipv4.dns 192.168.20.2
nmcli connection modify enp0s3 ipv4.method manual #manually assign IP
nmcli connection modify enp0s3 connection.autoconnect yes #start as boot.
nmcli connection up enp0s3 #配置結束後要啓動網卡。