1. 程式人生 > >CentOS 7安裝GUI介面及遠端連線

CentOS 7安裝GUI介面及遠端連線

轉載請註明文章出處:https://tlanyan.me/install-gui-and-…nnect-on-centos7/

用基於瀏覽器(webdriver)的selenium技術爬取資料,所以程式需執行在GUI環境下。本文分三個部分簡要介紹安裝GUI介面及遠端連線的步驟。

安裝GUI介面

大多數雲伺服器廠商提供的映象都無GUI介面,所以要先安裝圖形環境。本文使用GNOME桌面環境:

yum -y groups install "GNOME Desktop"

這條命令將安裝GNOME桌面的必要軟體包,執行完後配置X系統使用GNOME:

echo "exec gnome-session" >> ~/.xinitrc

安裝KDE或者MATE桌面環境的兩個命令稍微不一樣:

<pre class="sh"># kde yum -y groups install "KDE Plasma Workspaces" echo "exec startkde" >> ~/.xinitrc # mate yum --enablerepo=epel -y groups install "MATE Desktop" echo "exec /usr/bin/mate-session" >> ~/.xinitrc </pre>

以上便安裝好了GUI桌面環境。

啟動桌面環境有兩種方式:

  1. 一次性的:在終端裡輸入startx
  2. 系統啟動時預設進入桌面環境: systemctl set-default graphical.target,然後重啟;

安裝vnc-server

有了桌面環境,一般可以通過雲伺服器廠商提供的web終端遠端連線進入桌面。用web終端每次都需要開啟瀏覽器,然後登陸雲管理後臺再連線,比較麻煩。我們採取直接從桌面客戶端遠端連線的方式,省去開啟瀏覽器和登入雲管理後臺的操作。

遠端桌面技術有多種,例如VNC, TeamViewer, RDP等,本文使用免費且廣泛使用的VNC。

在伺服器上先安裝服務端(tigervnc是tightvnc的分支):

yum install -y tigervnc-server

接著複製一份VNC配置:

cp /lib/systemd/system/[email protected] /etc/systemd/system/vncserver@:1.service

注意上述命令引數重的“@:1”,可以將數字1換成30000內的任意數字,“5900+數字”即為程式的顯示(監聽)埠,如"@:1"表示監聽5901埠。

編輯該配置檔案,將檔案內的<USER>替換成遠端連線時的登入使用者名稱(如果是root,注意將第二個<USER>所在行的"/home"移除掉)。以root身份的配置示例:

# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/[email protected]
# 2. Replace <USER> with the actual user name and edit vncserver
#    parameters appropriately
#    (ExecStart=/usr/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
#     PIDFile=/home/<USER>/.vnc/%H%i.pid)
# 3. Run `systemctl daemon-reload`
# 4. Run `systemctl enable vncserver@:<display>.service`
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted!  For a secure way of using VNC, you should
# limit connections to the local host and then tunnel from
# the machine you want to view VNC on (host A) to the machine
# whose VNC output you want to view (host B)
#
# [user@hostA ~]$ ssh -v -C -L 590N:localhost:590M hostB
#
# this will open a connection on port 590N of your hostA to hostB's port 590M
# (in fact, it ssh-connects to hostB and then connects to localhost (on hostB).
# See the ssh man page for details on port forwarding)
#
# You can then point a VNC client on hostA at vncdisplay N of localhost and with
# the help of ssh, you end up seeing what hostB makes available on port 590M
#
# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.
#
# Use "-localhost" to prevent remote VNC clients connecting except when
# doing so through a secure tunnel.  See the "-via" option in the
# `man vncviewer' manual page.


[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking

# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

接下來設定vnc連線密碼

vncpasswd

注意連線密碼與登入密碼不同:連線密碼用於顯示遠端桌面,登入密碼用於使用者登入系統。

設定好後,啟動vnc服務:

systemctl daemon-reload
systemctl start vncserver@:1
systemctl enable vncserver@:1

如果開啟了防火牆,注意放行相應埠。

客戶端連線

服務端配置完畢,接下來用客戶端連線。

vnc是免費技術,許多客戶端都支援該協議。本文采用App Store上免費的“Remote Desktop - VNC”軟體進行連線,開啟後軟體介面如下:

<a href="https://tlanyan.me/install-gui-and-remote-connnect-on-centos7/jietu20190622-143524/" rel="attachment wp-att-5455"><img src="https://user-gold-cdn.xitu.io/2019/6/23/16b82759c908cb69?w=353&h=138&f=jpeg&s=5728" alt="" width="353" height="138" class="aligncenter size-full wp-image-5455" /></a>

在輸入框填寫伺服器地址:vnc://ip:port,其中ip是伺服器的ip或域名,port是監聽的埠,例如5901。輸入後按回車,彈出密碼輸入框,輸入vncpasswd設定的密碼。密碼正確的話就可以看到伺服器的圖形桌面,例如:

<a href="https://tlanyan.me/install-gui-and-remote-connnect-on-centos7/jietu20190622-143929/" rel="attachment wp-att-5456"><img src="https://user-gold-cdn.xitu.io/2019/6/23/16b82759d12964f0?w=1024&h=836&f=jpeg&s=20252" alt="" width="665" height="543" class="aligncenter size-large wp-image-5456" /></a>

按回車鍵進入登入介面,輸入登入使用者名稱和密碼進入系統:

<a href="https://tlanyan.me/install-gui-and-remote-connnect-on-centos7/jietu20190622-144103/" rel="attachment wp-att-5457"><img src="https://user-gold-cdn.xitu.io/2019/6/23/16b82759c8b888fe?w=1024&h=836&f=jpeg&s=39210" alt="" width="665" height="543" class="aligncenter size-large wp-image-5457" /></a>

接下來就可以做各種圖形操作了。

參考

  1. How to install a GUI on top of CentOS 7
  2. How to Install and Configure VNC Server i