Centos7上面使用root用户运行vnc

1.安装

yum groupinstall "GNOME Desktop" -y
#安装vnc服务
yum install tigervnc-server -y

2.配置启动文件

cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
#编辑文件
vi /etc/systemd/system/vncserver@:1.service

3.配置文件,注意此步骤很重要!!

把i改成:1,使用root用户

[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 :1 > /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 :1 > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

网上很多配置只改了用户没有更改用户pid目录,这样的后果是systemctl启动失败,但是实际上以及启动成功
一定要确定目录已经存在,否则启动会报错

PIDFile=/home/root/.vnc/%H%i.pid

4.防火墙开放服务

firewall-cmd --permanent --zone=public --add-service vnc-server
firewall-cmd --reload

5.设置密码

[duduniao@server1 ~]$ vncserver

You will require a password to access your desktops.

Password:<--yourvncpassword
Verify:<--yourvncpassword
xauth:  file /home/srijan/.Xauthority does not exist

New 'server1.example.com:1 (srijan)' desktop is server1.example.com:1

Creating default startup script /home/kishore/.vnc/xstartup
Starting applications specified in /home/kishore/.vnc/xstartup
Log file is /home/srijan/.vnc/server1.example.com:1.log

6.启动服务

systemctl daemon-reload
systemctl enable vncserver@:1.service
#reboot
systemctl start vncserver@:1.service

报错解决

Job for vncserver@:1.service failed because a configured resource limit was exceeded. See "systemctl status vncserver@:1.service" and "journalctl -xe" for details.

这个报错大部分都是因为启动文件配置错误导致的,解决办法如下
1,kill -9 杀死所有vnc进程
通过ps 过滤pid然后杀死
2.删除pid文件,上图所示我的pid文件在/root/.vnc下面

rm -fr /root/.vnc/*.pid

3.删除tmp目录下unix文件

rm -fr /tmp/.X*

4.启动服务

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

欢迎留言