Linux installation VNC (Linux desktop version remote)

1. Linux installation VNC service

For CentOS7

#检查系统没有装GUI界面
rpm -qa | grep gnome

yum -y groupinstall "X Window System"
yum -y groupinstall "GNOME Desktop"

#安装VNC
yum install tigervnc tigervnc-server -y
检查开机运行级别
systemctl get-default  #查询当前开机运行级别

systemctl set-default multi-user.target  #表示切换到运行级别3,3为命令行模式
systemctl set-default graphical.target  #表示切换到运行级别5,5为图形化GUI模式
#可以不开启,但要保证linux有安装GUI服务。( "X Window System"、"GNOME Desktop")

set password

vncpasswd  #输入两次
Would you like to enter a view-only password (y/n)? y  #再输两次

Start and stop the service, just start and stop and use it

vncserver :1  
#1表示开启的是5901端口,1--5901,5901为客户端连接端口号,2--5902,以此类推,可同时开启多个端口

vncserver -list  #可查看已开启的端口列表
netstat -anp | grep 5901  #查看5901进程

connection test

 Close the port. After closing, the link failure shown in the figure below will appear.

vncserver -kill :1  #表示关闭5901端口,但不影响除5901外的其他端口。
vncserver -kill :2  #表示关闭5902端口,但不影响除5902外的其他端口。

For CentOS6

The method of modifying the vim /etc/sysconfig/vncservers file in the online tutorial is the method of CentOS6. It is meaningless to change the configuration file in CentOS7.
 

vim /etc/sysconfig/vncservers

###填入以下内容###
# THIS FILE HAS BEEN REPLACED BY /lib/systemd/system/[email protected]
VNCSERVERS="2:root"  #用户
VNCSERVERARGS[2]="-geomotry 1366x768"  #分辨率

2. Set the VNC server to start automatically

According to the above method, it is indeed possible to achieve remote connection, but it is obviously unreasonable to use the vncserver:1 command to start the service every time it is turned on. You can set it to start automatically by using cron scheduling tasks, but the vnc service can also be created. The server service file can be automatically started at boot through the systemctl command.

For CentOS7

Copy the [email protected] file, pay attention to change <USER> to the real user name, I changed it to root here, a user needs to create a service service file.

The form of the file name must be in the form of vncserver@:2.service. Changing the number in the file name means changing the port. For example, vncserver@:2.service means that port 5902 is started after the service is opened, and vncserver@:3.service means that Port 5903, vncserver@:50.service represents port 5950 and so on.

cp /lib/systemd/system/[email protected] /lib/systemd/system/vncserver@:2.service
vim /lib/systemd/system/vncserver@:2.service

#找到其中的<USER> ,修改成自己的用户名,这里我改成root用户。
###例子###
[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 root %i
ExecStart=/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
###例子###

systemctl start vncserver@:2.service
systemctl enable vncserver@:2.service

 Start vncserver@:50.service

Disable autostart

systemctl stop vncserver@:2.service
systemctl disable vncserver@:2.service

The virtual machine is init3, but vnc is still a Linux desktop version GUI, that is, init5, which is another host 192.168.136.69. The operating level of the client and server can be different, as long as the server has a GUI installed.

 

Guess you like

Origin blog.csdn.net/weixin_48878440/article/details/130954743