CentOS7配置VNC Server

时间:2022-12-05 08:55:48

CentOS7与6有些许变化,感觉有点不太适应。

Step 1: 安装tigervnc server 和 X11 fonts:

[root@mdrill ~]# yum install tigervnc-server xorg-x11-fonts-Type1

Step 2:将VNC server的配置文件复制到/etc/systemd/system/下:

[root@mdrill ~]# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:.service

  备注:VNC server默认监听5900端口,但是我们可以为VNC设置端口偏移量,例如我上面设置的偏移是5,那么,你连接VNC服务器时可以指定ipaddress:sub-port(192.168.0.12:5或192.168.0.12:5905)。

Step 3:编辑刚刚复制过来的文件:

[root@mdrill ~]# vi /etc/systemd/system/vncserver@:.service

在文件的最后,我们可以看到如下内容。其中,<USER>对应的就是真实用户的用户名,例如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 %i > /dev/null >& || :’
ExecStart=/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 >& || :’[Install]
WantedBy=multi-user.target

我们将<USER>替换为我们真实存在的用户:mdrill,同时可以在其中配置屏幕分辨率如-geometry 1440×900,当然分辨率也可以在客户端设置:

[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 >& || :’
ExecStart=/sbin/runuser -l mdrill -c “/usr/bin/vncserver %i -geometry ×″
PIDFile=/home/mdrill/.vnc/%H%i.pid
ExecStop=/bin/sh -c ‘/usr/bin/vncserver -kill %i > /dev/null >& || :’[Install]
WantedBy=multi-user.target

Step 4:配置防火墙,允许VNC server使用的端口通过:

[root@mdrill ~]# firewall-cmd --permanent --zone=public --add-port=/tcp
[root@mdrill ~]# firewall-cmd --reload

Step 5:切换到mdrill用户启动VNC server:

[mdrill@mdrill ~]$ vncserver

  设置密码:

You will require a password to access your desktops.

Password:
Verify:
xauth: file /home/mdrill/.Xauthority does not exist New ‘localhost.localdomain: (raj)’ desktop is mdrill: Creating default startup script /home/mdrill/.vnc/xstartup
Starting applications specified in /home/mdrill/.vnc/xstartup
Log file is /home/mdrill/.vnc/server.itzgeek.com:.log

Step 6:切换回root,重新加载daemon-reload:

[root@mdrill ~]# systemctl daemon-reload

  启动VNC server:

[root@mdrill ~]# systemctl start vncserver@:.service

  设置开机自启动:

[root@mdrill ~]# systemctl enable vncserver@:.service

Step 7:windows 7下远程访问:

CentOS7配置VNC Server

  配置屏幕分辨率:

CentOS7配置VNC Server

  最后登录结果:

CentOS7配置VNC Server