环境:
OS:Centos 7
前提条件:
系统需要安装相关图形界面的软件包,否则安装vnc后启动不了
yum groupinstall "GNOME Desktop" "Graphical Administration Tools"
或是安装如下的
yum groups install GNOME
查看已经安装的组
yum grouplist
1.安装vnc和vncserver
yum -y install vnc
yum -y install tigervnc-server
2.关闭防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
vi /etc/selinux/config
set "SELINUX=disabled" and comment the rest lines.
以开启1号窗口为例(也可以同时开启多个窗口,修改数字即可),方法如下:
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
编辑/etc/systemd/system/vncserver@:1.service,设置用户root相关参数,修改前后内容如下:
[root@hxl01 ~]# more /etc/systemd/system/vncserver@:1.service # The vncserver service unit file # # Quick HowTo: # 1. Copy this file to /etc/systemd/system/vncserver@.service # 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 # 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 <USER> -c "/usr/bin/vncserver %i" PIDFile=/home/<USER>/.vnc/%H%i.pid ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' [Install] WantedBy=multi-user.target
修改为:
[root@hxl01 ~]# more /lib/systemd/system/vncserver@:1.service # The vncserver service unit file # # Quick HowTo: # 1. Copy this file to /etc/systemd/system/vncserver@.service # 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 # 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=simple # 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 oracle -c "/usr/bin/vncserver %i" 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
4.更新systemctl以使其生效;
systemctl daemon-reload
5.进入到oracle设置vnc密码
su - oracle
执行vncpasswd,并输入访问密码,操作如下:
$ vncpasswd
Password:
Verify:
6.启动
然后返回root用户下,以后就可以通过systemd来控制vncserver服务的启停了。
例如,启动服务:
# systemctl start vncserver@:1.service
7.查看状态
systemctl status vncserver@:1.service
8.查看5901端口
[root@host01 /]# ss -nlp|grep 5901
问题:
问题1 vnc连接后老是出现:authentication is required to set the network proxy used for
解决办法:
cd /etc/xdg/autostart/
vi polkit-gnome-authentication-agent-1.desktop
添加:X-GNOME-Autostart-enabled=false
然后重启系统
reboot
或是
1. vi /etc/xdg/autostart/gpk-update-icon.desktop
2. 在底部添加X-GNOME-Autostart-enabled=false
问题2:
取消屏幕保护
Application—System Tools—Setting—Hardware—Power Saving—Blank screen—never
3.问题
[root@host01 ~]# systemctl start vncserver@:2.service
Job for vncserver@:2.service failed because a configured resource limit was exceeded. See "systemctl status vncserver@:2.service" and "journalctl -xe" for details.
解决办法:
[root@host01 ~]# vi /lib/systemd/system/vncserver@:2.service
修改type=simple
4.发现服务状态没有启动但是端口是启动了的
[root@localhost ~]# ss -nlp|grep 5901
tcp LISTEN 0 5 *:5901 *:* users:(("Xvnc",pid=1883,fd=9))
tcp LISTEN 0 5 [::]:5901 [::]:* users:(("Xvnc",pid=1883,fd=10))
[root@localhost ~]#
[root@localhost ~]# systemctl status vncserver@:1.service
● vncserver@:1.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver@:1.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Oct 22 08:04:17 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)...
Oct 22 08:04:17 localhost.localdomain systemd[1]: Started Remote desktop service (VNC).
Oct 22 08:04:21 localhost.localdomain systemd[1]: New main PID 1883 does not belong to service, and PID file is not owned by root. Refusing.
[root@localhost ~]#
5.服务已经启动但是端口没有启动
解决:
rm -rf /tmp/.X*
[root@gaia data]# systemctl daemon-reload
[root@gaia data]# systemctl restart vncserver@:1.service
[root@gaia data]# systemctl status vncserver@:1.service
6.vnc连接黑屏解决办法
su - oracle
cd /home/oracle/.vnc
chmod 777 ./xstartup
问题7:
/home/dmdba/.vnc/xstartup: line 5: 27299 Terminated /etc/X11/xinit/xinitrc
Killing Xvnc process ID 27263
解决办法:
yum install mesa*