Centos7远程桌面 vnc/vnc-server的设置详解
Centos7与Centos6.x有了很大的不同。
为了给一台服务器装上远程桌面,走了不少弯路。写这篇博文,纯粹为了记录,以后如果遇到相同问题,可以追溯。
1、假定你的系统没有安装vnc的任何软件,那么,首先安装vnc
yum-yinstalltigervnc-servertigervnc
2、Centos7之前的系统,如果安装vnc一般都需要配置
[root@localhost~]#cat/etc/sysconfig/vncservers #THISFILEHASBEENREPLACEDBY/lib/systemd/system/vncserver@.service
但是,如上所述,Centos7需要配置的文件在
[root@localhost~]#ll/lib/systemd/system/vncserver@.service -rw-r--r--.1rootroot1744Jun1014:15/lib/systemd/system/vncserver@.service
3、文件内有如下提示
#QuickHowTo: #1.Copythisfileto/etc/systemd/system/vncserver@:<display>.service #2.Edit<USER>andvncserverparametersappropriately #("runuser-l<USER>-c/usr/bin/vncserver%i-arg1-arg2") #3.Run`systemctldaemon-reload` #4.Run`systemctlenablevncserver@:<display>.service
4、复制一份文件,并改名为vncserver@:1.service
[root@localhost~]#cp/lib/systemd/system/vncserver@.service/lib/systemd/system/vncserver@:1.service
5、将文件中的<User>用你当前的用户替换,将%i替换为1
[Unit] Description=Remotedesktopservice(VNC) After=syslog.targetnetwork.target [Service] Type=forking #Cleananyexistingfilesin/tmp/.X11-unixenvironment ExecStartPre=/bin/sh-c'/usr/bin/vncserver-kill:1>/dev/null2>&1||:' ExecStart=/sbin/runuser-lroot-c"/usr/bin/vncserver:1-geometry1280x720-depth24" PIDFile=/root/.vnc/%H%i.pid ExecStop=/bin/sh-c'/usr/bin/vncserver-kill:1>/dev/null2>&1||:' [Install] WantedBy=multi-user.target
6、更新systemctl
systemctldaemon-reload
7、设置为自动启动
systemctlenablevncserver@:1.service
8、启动vnc服务
systemctlstartvncserver@:1.service
9、在iptables中加入对应的端口5901(注意,如果有其他用户,那么也需要将端口加上。vnc的端口默认是5900+n)
[root@localhostsystem]#cat/etc/sysconfig/iptables #sampleconfigurationforiptablesservice #youcaneditthismanuallyorusesystem-config-firewall #pleasedonotaskustoaddadditionalports/servicestothisdefaultconfiguration *filter :INPUTACCEPT[0:0] :FORWARDACCEPT[0:0] :OUTPUTACCEPT[0:0] -AINPUT-mstate--stateRELATED,ESTABLISHED-jACCEPT -AINPUT-picmp-jACCEPT -AINPUT-ilo-jACCEPT -AINPUT-ptcp-mstate--stateNEW-mtcp--dport22-jACCEPT -AINPUT-ptcp-mstate--stateNEW-mtcp--dport5901-jACCEPT -AINPUT-jREJECT--reject-withicmp-host-prohibited -AFORWARD-jREJECT--reject-withicmp-host-prohibited COMMIT
注意:
1、如果你不配置和启用防火墙,此时用VNCViewer连接的话,会报:"connect:Connectiontimedout(10060)"错误。
2、本文是以root用户为例,如果其他用户也需要登录,那么,还需要将文件复制为
cp/lib/systemd/system/vncserver@.service/lib/systemd/system/vncserver@:2.service
同时,将文件内的%i改为2,并建立对应用户的vncpasswd。
3、你可以通过UltraVNC,TigerVNC或者RealVNCViewer进行访问,我在win7下使用这3中工具均能连接
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!