当前位置: 移动技术网 > 网络运营>服务器>Linux > Centos7远程桌面 vnc/vnc-server的设置详解

Centos7远程桌面 vnc/vnc-server的设置详解

2019年04月28日  | 移动技术网网络运营  | 我要评论

centos7与centos6.x有了很大的不同。

为了给一台服务器装上远程桌面,走了不少弯路。写这篇博文,纯粹为了记录,以后如果遇到相同问题,可以追溯。

1、假定你的系统没有安装vnc的任何软件,那么,首先安装vnc

yum -y install tigervnc-server tigervnc

2、centos7之前的系统,如果安装vnc一般都需要配置

[root@localhost ~]# cat /etc/sysconfig/vncservers 
# this file has been replaced by /lib/systemd/system/vncserver@.service

但是,如上所述,centos7需要配置的文件在

[root@localhost ~]# ll /lib/systemd/system/vncserver@.service
-rw-r--r--. 1 root root 1744 jun 10 14:15 /lib/systemd/system/vncserver@.service

3、文件内有如下提示

# 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

4、复制一份文件,并改名为vncserver@:1.service

[root@localhost ~]# cp /lib/systemd/system/vncserver@.service/lib/systemd/system/vncserver@:1.service

5、将文件中的<user>用你当前的用户替换,将%i替换为1

[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-user.target

6、更新systemctl

systemctl daemon-reload

7、设置为自动启动

systemctl enable vncserver@:1.service

8、启动vnc服务

systemctl start vncserver@:1.service

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
-a input -p tcp -m state --state new -m tcp --dport 5901 -j accept
-a input -j reject --reject-with icmp-host-prohibited
-a forward -j reject --reject-with icmp-host-prohibited
commit

注意:

1、如果你不配置和启用防火墙,此时用vnc viewer连接的话,会报:"connect:connection timed out(10060)"错误。

2、本文是以root用户为例,如果其他用户也需要登录,那么,还需要将文件复制为

cp /lib/systemd/system/vncserver@.service/lib/systemd/system/vncserver@:2.service

同时,将文件内的%i改为2,并建立对应用户的vncpasswd。

3、你可以通过ultravnc,tigervnc或者realvnc viewer进行访问,我在win7下使用这3中工具均能连接

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网