1. 程式人生 > >CentOS 7安裝TigerVNC Server

CentOS 7安裝TigerVNC Server

運行 文件鎖 sage cat git tcp span geo dep

http://blog.csdn.net/wamath/article/details/76003128

1. CentOS 7安裝TigerVNC Server

本文介紹如何在CentOS 7上安裝VNC Server,以便遠程訪問。本文參照了DigitalOcean的教程,加入了一些安裝經驗。

2. 安裝TigerVNC Server

建議使用非root用戶安裝,-y代表直接安裝

sudo yum install -y tigervnc-server

3. 配置VNC Service

以下方法是新方法,以前是要配置/etc/sysconfig/vncservers,現在第一步是將默認提供的文件復制到/etc/systemd/system

,命令如下

sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

接下來修改該配置文件

sudo vim /etc/systemd/system/vncserver@:1.service

將其中<USER>替換為你想要的用戶名,我這裏是oracle,添加設置分辨率的參數-geometry 1280x720,所有內容如下

# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/[email protected]:<display>.service
# 2. Edit <USER> and vncserver parameters appropriately
#   ("runuser -l <USER> -c /usr/bin/vncserver %i -arg1 -arg2")
# 3. Run `systemctl daemon-reload`
# 4. Run `systemctl enable [email protected]:<display>.service`
#

. . .

[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=/sbin/runuser -l oracle -c "/usr/bin/vncserver %i -geometry 1280x720" 
PIDFile=/home/oracle/.vnc/%H%i.pid
ExecStop=/bin/sh -c ‘/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :‘

[Install]
WantedBy=multi-user.target

保存文件並退出vim,重新加載配置

sudo systemctl daemon-reload

也可以設置成開機啟動

sudo systemctl enable [email protected]:1.service

4. 修改防火墻

首先判斷firewalld是否啟動,輸入以下命令判斷

sudo firewall-cmd --state

如果啟動應該輸出

running

如果是not running,執行下面命令

sudo systemctl start firewalld

添加端口號5901-5905

sudo firewall-cmd --permanent --zone=public --add-port=5901-5905/tcp

重新加載防火墻

sudo firewall-cmd --reload

可以使用下面命令查看端口號是否被加入

firewall-cmd --list-all-zones

5. 設置VNC密碼

通過ssh,用oracle用戶名登錄到服務器,執行下面命令,這裏相當於配置密碼並啟動一個vnc-server實例,用需要連接vnc的用戶登錄來設置密碼

vncserver
vncserver 用法:
 vncserver -h

usage: vncserver [:<number>] [-name <desktop-name>] [-depth <depth>]
                 [-geometry <width>x<height>]
                 [-pixelformat rgbNNN|bgrNNN]
                 [-fp <font-path>]
                 [-cc <visual>]
                 [-fg]
                 [-autokill]
                 [-noxstartup]
                 [-xstartup <file>]
                 <Xvnc-options>...

       vncserver -kill <X-display>

       vncserver -list

終端會提示你輸入密碼,如下

You will require a password to access your desktops.
Password:
Verify:
xauth:  file /home/oracle/.Xauthority does not exist

New ‘localhost.localdomain:1 (oracle)‘ desktop is localhost.localdomain:1

Creating default startup script /home/oracle/.vnc/xstartup
Starting applications specified in /home/oracle/.vnc/xstartup
Log file is /home/oracle/.vnc/localhost.localdomain:1.log

如果想修改密碼,可以使用vncpasswd。現在已經有一個vnc服務在運行了,但我們需要使用剛剛配置的服務來啟動,所以我們需要先殺死剛剛的vnc服務,使用下面命令。

vncserver -kill :1

接下來,重啟我們配置的服務

sudo systemctl daemon-reload
sudo systemctl restart [email protected]:1.service

使用下面命令查看該服務是否正確運行

sudo systemctl status [email protected]:1.service -l

如果正確啟動,輸出應為

● [email protected]:2.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/[email protected]:2.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2017-07-23 21:55:35 CST; 12h ago
  Process: 8720 ExecStart=/usr/sbin/runuser -l oracle -c /usr/bin/vncserver %i -geometry 1280x720 (code=exited, status=0/SUCCESS)
  Process: 8716 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
 Main PID: 8744 (Xvnc)
   CGroup: /system.slice/system-vncserver.slice/[email protected]:2.service
           ? 8744 /usr/bin/Xvnc :2 -desktop 127.0.0.1:2 (oracle) -auth /home/oracle/.Xauthority -geometry 1280x720 -rfbwait 30000 -rfbauth /home/oracle/.vnc/passwd -rfbport 5902 -fp catalogue:/etc/X11/fontpath.d -pn

7月 23 21:55:32 127.0.0.1 systemd[1]: Starting Remote desktop service (VNC)...
7月 23 21:55:35 127.0.0.1 systemd[1]: Started Remote desktop service (VNC).

如果想配置多用戶同時訪問,需要將上面[email protected]:1.service,改為[email protected]:2.service,然後配置其中用戶名、分辨率參數,再按我的步驟走一遍就可以了

生產的配置編號,由下面兩個文件鎖定編號,要改變l默認vncserver 生產的編號,可刪除相應的文件和用戶家目錄.vnc/下的文件:

/tmp/.X1-lock

/tmp/.X11-unix/X1

CentOS 7安裝TigerVNC Server