1. 安装tigervnc-server
yum install tigervnc-server
2. 设置vncserver服务器
将默认提供的文件复制到/etc/systemd/system
,命令如下:
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
修改配置
vi /etc/systemd/system/vncserver@:1.service
将其中<USER>
替换为你想要的用户名,本人使用的账号为root,添加设置分辨率的参数-geometry 1280x720
,所有内容如下
# The vncserver service unit file # # Quick HowTo: # 1. Copy this file to /etc/systemd/system/vncserver@:<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 vncserver@:<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 root -c "/usr/bin/vncserver %i -geometry 1280x720" PIDFile=/root/.vnc/%H%i.pid # 普通用户可能路径为 /home/<USER>/.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 enable vncserver@:1.service
3. 设置防火墙
首先判断firewalld
是否启动
firewall-cmd --state
如果没有启动,先启动防火墙
systemctl start firewalld
添加端口号5901-5905例外
firewall-cmd --permanent --zone=public --add-port=5901-5905/tcp
重新加载防火墙
firewall-cmd --reload
可以使用下面命令查看端口号是否被加入
firewall-cmd --list-all-zones
4. 设置VNC密码
通过ssh,用oracle用户名登录到服务器,执行下面命令
vncserver
终端会提示你输入密码,如下
You will require a password to access your desktops. Password: Verify: Would you like to enter a view-only password (y/n)? y Password: Verify: New 'lenmomDesktop:1 (root)' desktop is lenmomDesktop:1 Creating default startup script /root/.vnc/xstartup Creating default config /root/.vnc/config Starting applications specified in /root/.vnc/xstartup Log file is /root/.vnc/lenmomDesktop:1.log
如果想修改密码,可以使用vncpasswd
。现在已经有一个vnc服务在运行了,但我们需要使用刚刚配置的服务来启动,所以我们需要先杀死刚刚的vnc服务,使用下面命令:
vncserver -kill :1
接下来,重启服务:
systemctl daemon-reload
systemctl restart vncserver@:1.service
使用下面命令查看该服务是否正确运行
systemctl status vncserver@:1.service -l
如果正确启动,输出应为:
vncserver@:1.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).
如果想配置多用户同时访问,需要将上面vncserver@:1.service
,改为vncserver@:2.service
,然后配置其中用户名、分辨率参数,再按上述步骤做一遍。