TigerVNC Server Install & Configuration

1. Environment

2. Install

  • 安装tigervnc-server

    yum -y groups install "GNOME DESKTOP" "Graphical Administration Tools"
    yum install -y tigervnc-server tigervnc.x86_64
    

3. Configuration

  • 将默认提供的文件复制到/etc/systemd/system,命令如下

    cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:100.service
    
  • 修改并保存该配置文件/etc/systemd/system/vncserver@:100.service,将其中<USER>替换为你想要的用户名,我这里是root,添加设置分辨率的参数-geometry 1920x1080,所有内容如下

    vim /etc/systemd/system/vncserver@:100.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 root -c "/usr/bin/vncserver %i -geometry 2560x1440" 
    PIDFile=/home/root/.vnc/%H%i.pid
    ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
    
    [Install]
    WantedBy=multi-user.target
    
  • 重新加载配置

    systemctl daemon-reload
    
  • 设置为开机自启动

    # 启动
    systemctl start vncserver@:100.service
    # 自启动
    systemctl enable vncserver@:100.service
    
    # 查看VNC状态
    systemctl status vncserver@:100.service
    # 停止
    systemctl stop vncserver@:100.service
    

4. 设置VNC密码

  • 切换到上面配置文件中使用的用户名,这里是root

    su root
    
  • 启动vncserver,终端会提示你输入密码,如下

    vncserver
    
    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 /root/.vnc/PM_njzlm_I2.host:2.log
    

    如果想修改密码,可以使用vncpasswd。现在已经有一个vnc服务在运行了,但我们需要使用刚刚配置的服务来启动,所以我们需要先杀死刚刚的vnc服务,使用下面命令。

  • 查看日志

     tail -f /root/.vnc/PM_njzlm_I2.host:2.log
    
  • 重启我们配置的服务

    systemctl daemon-reload
    
    systemctl stop vncserver@:100.service
    # 删除临时文件
    rm -rf /tmp/.X11-unix/X1
    
    systemctl restart vncserver@:100.service
    
  • 查看服务运行状态

    systemctl status vncserver@:100.service -l
    

    正确启动的运行状态

    ● vncserver@:2.service - Remote desktop service (VNC)
       Loaded: loaded (/etc/systemd/system/vncserver@: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/vncserver@: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).
    

5. Configuration 防火墙

  • 查看Nginx使用端口80

    netstat -lnpt
    
  • 开放端口,这里使用80端口

    firewall-cmd --permanent --zone=public --add-port=6000/tcp
    
  • 重启防火墙

    firewall-cmd --reload
    
  • 查看防火墙,已开放端口列表

    firewall-cmd --list-ports
    

6. 卸载

  • 删除安装的tigervnc

    yum -y remove tigervnc-server
    
  • 删除配置文件和相关的文件夹

    rm /etc/systemd/system/vncserver@:*
    
  • 删除 home 目录下 远程连接账户 下的 .vnc 文件夹

    rm -rf /home/root/.vnc
    
  • 删除 /tmp/.X11-unix 目录下文件

    rm -f /tmp/.X11-unix/*