
最近用到了虛擬機,之前虛擬機的網絡配置使用的NAT配置好了,但是無論怎樣設置都無法使用局域網內的其它主機訪問虛擬機的服務。經過了一天的折騰,遠程主機仍然連接不上虛擬機服務,后來找到原因,NAT連接模式下局域網內的其它主機通過無線網卡連接虛擬機的映射,但是虛擬機從有線網卡中傳遞信號返回故局域網內其它主機實現對宿主主機的虛擬機的訪問使用NAT模式是不可行的,看到有些人寫的NAT的配置中將虛擬機的網絡適配器ip地址修改為與宿主機ip同頻段的地址,這種方法雖然實現了宿主機對虛擬機的訪問但是此時的虛擬機是ping不到互聯網絡的(因為此時虛擬機的ip是NAT模式下分發的子網ip地址,與宿主機的ip不在一個網絡內,故不可以實現網絡訪問)。
折騰了一天的時間后斷然改用橋接的方式實現局域網內主機對虛擬機的訪問設置。橋接的模式就是使虛擬機和宿主機在同一ip網絡內,即給虛擬機一個物理地址共享主機的無線網卡。
使用橋接配置網絡###
下面主要是橋接的設置步驟。
步驟一####
首先在虛擬機配置中更改網絡模式為 橋接

步驟二####
查看虛擬網絡編輯器:(注意我們在NAT模式時需要設置網絡編輯器,而在橋接模式上我們不需要對它進行更改,並且要確保模式中只有一種本地模式)

步驟三####
打開宿主機的網絡配置,對Vmnet8(對應上圖中的適配器名稱)進行配置Ipv4,要保證ip地址與宿主機的ip在同一個ip頻段上,但不要與宿主機ip相同,網關一定要與宿主機的網關一樣,子網掩碼也要與宿主機相同,一般子網掩碼為255.255.255.0

步驟四####
在Centos的虛擬機上設置網絡,由於我是從NAT切換成橋接模式的所以我這是在NAT模式上進行的修改,BOOTPROTO=“static”靜態獲取ip (ip對應宿主機的ip頻段但與宿主機ip地址不能一樣,網關以及子網掩碼與宿主機相同),當然可以設置成 BOOTPROTO=“dhcp”動態獲取,並且將IPADDR及以下的設置刪除掉。
另外ONBOOT要為yes。有些人的電腦中不存在ifcfg-ens33文件,使用命令cd /etc/sysconfig/network-scripts進入network-scripts文件夾內使用ls查看ifcfg-ensxxxx文件並進行如下設置。

執行命令 sudo vi /etc/sysconfig/network 點擊i鍵進入修改模式后設置GATEWAY(網關)與主機網關相同,HOSTNAME隨意設置。修改完成后Esc並使用:wq保存。

執行命令 sudo vi /etc/resolv.conf 進入后點擊i鍵進行修改操作。內容如下:
其中nameserver與網關地址一致。修改完成后Esc並使用:wq保存。

至此所有設置完成,在終端執行ping 主機ip 可以查看通訊。
xrdp+tigervnc實現遠程桌面設置###
安裝步驟####
首先安裝xrdp:
執行命令:yun install epel-release 這個軟件包會自動配置yum的軟件倉庫。如果你也可以不安裝這個包,自行配置軟件倉庫也是一樣的
執行命令:yun install xrdp 實現對xrdp的安裝
執行命令:yum install tigervnc tigervnc-server 實現tigervnc的安裝(遠程桌面的一種實現方式)
執行命令:chcon -t bin_t /usr/sbin/xrdp
執行命令: chcon -t bin_t /usr/sbin/xrdp-sesman 實現對xrdp服務的設置
執行命令:systemctl start xrdp 啟動xrdp服務
執行命令:systemctl enable xrdp 通過systemctl enable實現xrdp服務的開機自啟的設置
執行命令:netstat -antup|grep xrdp 查看xrdp與xrdp-sesman的啟動情況
接下來就是為防火牆添加3389的訪問接口( Server遠程桌面的服務端口)
執行命令:firewall-cmd --permanent --zone=public --add-port=3389/tcp
執行命令:firewall-cmd --reload
至此實現遠程桌面的設置,連接遠程桌面時地址為 : 你的虛擬機的ip地址:3389 輸入用戶名和密碼登錄遠程桌面。
安裝記錄####
以下是安裝記錄打印可供參考:
[root@localhost superme]# yum install epel-release
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.zju.edu.cn
* updates: mirrors.cn99.com
正在解決依賴關系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在檢查事務
---> 軟件包 epel-release.noarch.0.7-11 將被 安裝
--> 解決依賴關系完成
依賴關系解決
================================================================================
Package 架構 版本 源 大小
================================================================================
正在安裝:
epel-release noarch 7-11 extras 15 k
事務概要
================================================================================
安裝 1 軟件包
總下載量:15 k
安裝大小:24 k
Is this ok [y/d/N]: y
Downloading packages:
警告:/var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: 頭V3 RSA/SHA256 Signature, 密鑰 ID f4a80eb5: NOKEY
epel-release-7-11.noarch.rpm 的公鑰尚未安裝
epel-release-7-11.noarch.rpm | 15 kB 00:05
從 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 檢索密鑰
導入 GPG key 0xF4A80EB5:
用戶ID : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
指紋 : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
軟件包 : centos-release-7-5.1804.el7.centos.x86_64 (@anaconda)
來自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
是否繼續?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : epel-release-7-11.noarch 1/1
驗證中 : epel-release-7-11.noarch 1/1
已安裝:
epel-release.noarch 0:7-11
完畢!
[root@localhost superme]# yum install xrdp
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 4.9 kB 00:00
* base: mirrors.cn99.com
* epel: ftp.yz.yamagata-u.ac.jp
* extras: mirrors.zju.edu.cn
* updates: mirrors.cn99.com
epel | 5.4 kB 00:00
epel/x86_64/updateinfo FAILED
http://ftp.riken.jp/Linux/fedora/epel/7/x86_64/repodata/f9261eaff5918473af07052ec939c36c94dba4243ba8aab79c58d4fde90d6ae7-updateinfo.xml.bz2: [Errno 14] HTTP Error 404 - Not Found
正在嘗試其它鏡像。
To address this issue please refer to the below wiki article
https://wiki.centos.org/yum-errors
If above article doesn't help to resolve this issue please use https://bugs.centos.org/.
(1/3): epel/x86_64/group_gz | 90 kB 00:00
epel/x86_64/updateinfo FAILED
http://epel.dionipe.id/7/x86_64/repodata/f9261eaff5918473af07052ec939c36c94dba4243ba8aab79c58d4fde90d6ae7-updateinfo.xml.bz2: [Errno 14] HTTP Error 404 - Not Found
正在嘗試其它鏡像。
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:02
(3/3): epel/x86_64/primary_db | 6.7 MB 01:37
正在解決依賴關系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在檢查事務
---> 軟件包 xrdp.x86_64.1.0.9.12-4.el7 將被 安裝
--> 解決依賴關系完成
依賴關系解決
================================================================================
Package 架構 版本 源 大小
================================================================================
正在安裝:
xrdp x86_64 1:0.9.12-4.el7 epel 427 k
事務概要
================================================================================
安裝 1 軟件包
總下載量:427 k
安裝大小:2.1 M
Is this ok [y/d/N]: y
Downloading packages:
警告:/var/cache/yum/x86_64/7/epel/packages/xrdp-0.9.12-4.el7.x86_64.rpm: 頭V3 RSA/SHA256 Signature, 密鑰 ID 352c64e5: NOKEY
xrdp-0.9.12-4.el7.x86_64.rpm 的公鑰尚未安裝
xrdp-0.9.12-4.el7.x86_64.rpm | 427 kB 00:08
從 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 檢索密鑰
導入 GPG key 0x352C64E5:
用戶ID : "Fedora EPEL (7) <epel@fedoraproject.org>"
指紋 : 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
軟件包 : epel-release-7-11.noarch (@extras)
來自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
是否繼續?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : 1:xrdp-0.9.12-4.el7.x86_64 1/1
驗證中 : 1:xrdp-0.9.12-4.el7.x86_64 1/1
已安裝:
xrdp.x86_64 1:0.9.12-4.el7
完畢!
[root@localhost superme]# yum install tigervnc tigervnc-server
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: ftp.riken.jp
* extras: mirrors.zju.edu.cn
* updates: mirrors.cn99.com
正在解決依賴關系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在檢查事務
---> 軟件包 tigervnc.x86_64.0.1.8.0-17.el7 將被 安裝
--> 正在處理依賴關系 tigervnc-icons,它被軟件包 tigervnc-1.8.0-17.el7.x86_64 需要
--> 正在處理依賴關系 libfltk_images.so.1.3()(64bit),它被軟件包 tigervnc-1.8.0-17.el7.x86_64 需要
--> 正在處理依賴關系 libfltk.so.1.3()(64bit),它被軟件包 tigervnc-1.8.0-17.el7.x86_64 需要
---> 軟件包 tigervnc-server.x86_64.0.1.8.0-17.el7 將被 安裝
--> 正在檢查事務
---> 軟件包 fltk.x86_64.0.1.3.4-1.el7 將被 安裝
---> 軟件包 tigervnc-icons.noarch.0.1.8.0-17.el7 將被 安裝
--> 解決依賴關系完成
依賴關系解決
================================================================================
Package 架構 版本 源 大小
================================================================================
正在安裝:
tigervnc x86_64 1.8.0-17.el7 base 241 k
tigervnc-server x86_64 1.8.0-17.el7 base 215 k
為依賴而安裝:
fltk x86_64 1.3.4-1.el7 base 560 k
tigervnc-icons noarch 1.8.0-17.el7 base 39 k
事務概要
================================================================================
安裝 2 軟件包 (+2 依賴軟件包)
總下載量:1.0 M
安裝大小:2.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): tigervnc-icons-1.8.0-17.el7.noarch.rpm | 39 kB 00:00
(2/4): tigervnc-1.8.0-17.el7.x86_64.rpm | 241 kB 00:00
(3/4): fltk-1.3.4-1.el7.x86_64.rpm | 560 kB 00:02
(4/4): tigervnc-server-1.8.0-17.el7.x86_64.rpm | 215 kB 00:05
--------------------------------------------------------------------------------
總計 172 kB/s | 1.0 MB 00:06
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : fltk-1.3.4-1.el7.x86_64 1/4
正在安裝 : tigervnc-icons-1.8.0-17.el7.noarch 2/4
正在安裝 : tigervnc-1.8.0-17.el7.x86_64 3/4
正在安裝 : tigervnc-server-1.8.0-17.el7.x86_64 4/4
驗證中 : tigervnc-server-1.8.0-17.el7.x86_64 1/4
驗證中 : tigervnc-icons-1.8.0-17.el7.noarch 2/4
驗證中 : fltk-1.3.4-1.el7.x86_64 3/4
驗證中 : tigervnc-1.8.0-17.el7.x86_64 4/4
已安裝:
tigervnc.x86_64 0:1.8.0-17.el7 tigervnc-server.x86_64 0:1.8.0-17.el7
作為依賴被安裝:
fltk.x86_64 0:1.3.4-1.el7 tigervnc-icons.noarch 0:1.8.0-17.el7
完畢!
[root@localhost superme]# chcon -t bin_t /usr/sbin/xrdp
[root@localhost superme]# chcon -t bin_t /usr/sbin/xrdp-sesman
[root@localhost superme]# systemctl start xrdp
[root@localhost superme]# systemctl enable xrdp
Created symlink from /etc/systemd/system/multi-user.target.wants/xrdp.service to /usr/lib/systemd/system/xrdp.service.
[root@localhost superme]# systemctl status xrdp.service
● xrdp.service - xrdp daemon
Loaded: loaded (/usr/lib/systemd/system/xrdp.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2020-02-15 14:12:21 CST; 32s ago
Docs: man:xrdp(8)
man:xrdp.ini(5)
Main PID: 4480 (xrdp)
CGroup: /system.slice/xrdp.service
└─4480 /usr/sbin/xrdp --nodaemon
2月 15 14:12:21 localhost.localdomain systemd[1]: Started xrdp daemon.
2月 15 14:12:21 localhost.localdomain systemd[1]: Starting xrdp daemon...
2月 15 14:12:22 localhost.localdomain xrdp[4480]: (4480)(140549388200384)[IN...
2月 15 14:12:22 localhost.localdomain xrdp[4480]: (4480)(140549388200384)[IN...
2月 15 14:12:22 localhost.localdomain xrdp[4480]: (4480)(140549388200384)[IN...
2月 15 14:12:22 localhost.localdomain xrdp[4480]: (4480)(140549388200384)[IN...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost superme]# netstat -antup|grep xrdp
tcp 0 0 0.0.0.0:3389 0.0.0.0:* LISTEN 4480/xrdp
tcp 0 0 127.0.0.1:3350 0.0.0.0:* LISTEN 4479/xrdp-sesman
[root@localhost superme]# firewill -cmd --permanent --zone=public --add-port=3389/tcp
bash: firewill: 未找到命令...
[root@localhost superme]# firewall -cmd --permanent --zone=public --add-port=3389/tcp
bash: firewall: 未找到命令...
[root@localhost superme]# firewall-cmd --permanent --zone=public --add-port=3389/tcp
success
[root@localhost superme]# firewall-cmd --reload
success
