[size=x-large]
centos7配置vnc
1.安装tigervncserver,yum install tigervnc-server tigervnc-server-module
2.拷贝配置文件cp /lib/systemd/system/
[email protected] /etc/systemd/system/vncserver@:1.service
3.进入到配置文件目录cd /etc/systemd/system
4.修改配置文件vim vncserver@:1.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 :1 > /dev/null 2>&1 || :'
ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver :1 -geometry 1280x720 -depth 24"
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill :1 > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-us
5. 启用配置文件 systemctl enable vncserver@:1.service
6. 设置登陆密码:vncpasswd
7. 启动vncserver:systemctl start vncserver@:1.service
8 . 启动状态查看:systemctl status vncserver@:1.serviceer.target
9.在iptables中加入对应的端口5901(注意,如果有其他用户,那么也需要将端口加上。vnc的端口默认是5900 + n)
[root@localhost system]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
[color=red]-A INPUT -p tcp -m state --state NEW -m tcp --dport 5901 -j ACCEPT[/color]-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出
systemctl restart iptables.service #最后重启防火墙使配置生效
注意1、如果你不配置和启用防火墙,此时用VNC Viewer连接的话,会报:"connect:Connection timed out(10060)"错误。
2、本文是以root用户为例,如果其他用户也需要登录,那么,还需要将文件复制为
1
cp /lib/systemd/system/
[email protected] /lib/systemd/system/vncserver@:2.service
同时,将文件内的%i改为2,并建立对应用户的vncpasswd。
3、
vncserver 启动失败的一种情况
#vncserver
Warning: localhost.localdomain:1 is taken because of /tmp/.X11-unix/X1
Remove this file if there is no X server localhost.localdomain:1
Warning: localhost.localdomain:2 is taken because of /tmp/.X11-unix/X2
Remove this file if there is no X server localhost.localdomain:2
New 'localhost.localdomain:3 (root)' desktop is localhost.localdomain:3
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost.localdomain:3.log
解决办法:
删除 / tmp/.X11-unix/的内容[/size]