1.安装vnc软件包
yum -y install tigervnc-server
2.修改默认的启动配置文件
1.先来拷贝默认的启动文件,并重新命名vncserver@:1.service cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service 2.修改vncserver@:1.service配置文件 vim /etc/systemd/system/vncserver@\:1.service 原内容 [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/bin/vncserver_wrapper <USER> %i ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' [Install] WantedBy=multi-user.target 改后 [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 || :' #这是修改后的内容,我以root登陆,所以修改为root,如果是别的用户可以修改成别的用户 ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver %i" #这也是新加的内容,pid存放的目录 PIDFile=/root/.vnc/%H%i.pid ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' [Install] WantedBy=multi-user.target ########################## 复制这行没有注释的内容,以防报错 [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 root -c "/usr/bin/vncserver %i" PIDFile=/root/.vnc/%H%i.pid ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' [Install] WantedBy=multi-user.target
3.重新加载systemd
systemctl daemon-reload
4.设置vnc密码
[root@centos7 ~]# vncpasswd Password:输入第一遍密码,这里密码是不显示的 Verify:输入第二遍密码,这里密码是不显示的 Would you like to enter a view-only password (y/n)? n 是否输入仅限查看的密码,我这里选择的n,如果你需要仅能查看的密码,就输入一遍和管理操作的密码不一样的密码,根据自己的需求选择 A view-only password is not usedxxxxxxxxxx [root@centos7 ~]# vncpasswd Password:Verify:Would you like to enter a view-only password (y/n)? nA view-only password is not usedvncpasswd bash 解释: Would you like to enter a view-only password (y/n)? n 是否输入仅限查看的密码,我这里选择的n,如果你需要仅能查看的密码,就输入一遍和管理操作的密码不一样的密码,根据自己的需求选择
5.启动vnc,并设置为开机自启动
systemctl enable vncserver@:1.service systemctl start vncserver@:1.service
6.查看vnvserver是否正常启动
[root@centos7 ~]# ps -ef | grep vnc root 3508 1 0 12:35 ? 00:00:00 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop centos7.6:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 30000 root 3533 1 0 12:35 ? 00:00:00 /bin/sh /root/.vnc/xstartup root 4332 2148 0 12:36 pts/0 00:00:00 grep --color=auto vnc [root@centos7 ~]# systemctl status vncserver@:1.service ● vncserver@:1.service - Remote desktop service (VNC) Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2024-04-30 12:35:08 CDT; 1min 19s ago Process: 3459 ExecStop=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS) Process: 3473 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS) Main PID: 3508 (Xvnc) CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service ‣ 3508 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop centos7.6:1 (root) -fp catalogue:/etc/... Apr 30 12:35:08 centos7.6 systemd[1]: Stopped Remote desktop service (VNC). Apr 30 12:35:08 centos7.6 systemd[1]: Starting Remote desktop service (VNC)... Apr 30 12:35:08 centos7.6 systemd[1]: Started Remote desktop service (VNC).
7.在windowd上安装vnc
在windows上面安装VNC Viewer软件,连接远程主机. 下载地址:https://www.realvnc.com/en/connect/download/viewer/windows/