Rules for installing and configuring VNC server

We all know that as a system administrator, most of the time you manage servers through the network. In the process of managing servers, in most cases we just use SSH to complete our management tasks. VNC allows us to open a remote graphical session to connect to our server, so that we can remotely access the server's graphical interface through the network.

VNC Server is a free and open source software that allows users to remotely access the server's desktop environment. In addition, you need to use the VNC viewer client to connect to the VNC server.

Some VNC server advantages:

  • Remote graphical management makes work simple and convenient.
  • The clipboard can be shared between the CentOS server host and the VNC client machine.
  • Graphical tools can also be installed on CentOS servers to make management capabilities more powerful.
  • As long as the VNC client is installed, CentOS servers can be managed through any operating system.
  • More reliable than ssh graphics forwarding and RDP connections.
  • So, let's start the journey of installing VNC server. We need to follow the steps below to build a usable VNC step by step.

So, let's start the journey of installing VNC server. We need to follow the steps below to build a usable VNC step by step.

First, we need an available desktop environment (X-Window). If not, install one first.

Note: The following commands must be run with root privileges. To switch to root, run "sudo -s" under the terminal, excluding the double quotes ("") of course.

1. Install X-Window

First we need to install X-Window. Run the following command  in the terminal . The installation will take a while.

# yum check-update
# yum groupinstall "X Window System"

###
# yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts

### 设置默认启动图形界面
# unlink /etc/systemd/system/default.target
# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

###
# reboot

After the server restarts, we have a working CentOS 7 desktop environment.

Now, we are going to install the VNC server on the server.

2. Install the VNC server

Now it's time to install the VNC server on our CentOS 7. We need to execute the following command.

# yum install tigervnc-server -y

3. Configure VNC

Then, we need to create a configuration file in the /etc/systemd/system/ directory. We can copy a sample configuration file from /lib/systemd/sytem/[email protected].

# cp /lib/systemd/system/[email protected] /etc/systemd/system/vncserver@:1.service

Then we use our favorite editor (here we use nano) to open /etc/systemd/system/vncserver@:1.service, find the following lines, and replace them with our own username. For example, my username is youngyoung so I replace it with youngyoung:

ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid

Replace with

ExecStart=/sbin/runuser -l youngyoung -c "/usr/bin/vncserver %i"
PIDFile=/home/youngyoung/.vnc/%H%i.pid

If root user then

ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid

Okay, let's restart systemd.

# systemctl daemon-reload

Finally, you need to set the user's VNC password. To set the password of a certain user, you must have the permission to switch to the user through sudo. Here I use the permissions of youngyoung and execute "su - youngyoung".

# su - youngyoung
$ vncpasswd

Note: Make sure the password you enter is more than 6 characters

4. Start the service

Start the service (permanently) with the following command:

$ sudo systemctl enable vncserver@:1.service

Start the service:

$ sudo systemctl start vncserver@:1.service

5. Firewall Settings

We need to configure the firewall to allow the VNC service to work properly.

$ sudo firewall-cmd --permanent --add-service vnc-server
$ sudo systemctl restart firewalld.service

 Now you can use the IP and port number (LCTT translation: for example 192.168.1.1:1, the port here is not the port of the server, but is sorted from 1 depending on the number of VNC connections) to connect to the VNC server.

6. Use VNC client to connect to the server

Okay, now you have completed the installation of the VNC server. To use VNC to connect to the server, we also need a VNC client installed on the local computer that is used only to connect to the remote computer.

You can use clients like Tightvnc viewer and Realvnc viewer to connect to the server.

To connect with more users, you need to create a configuration file and port, please go back to step 3 and add a new user and port. You need to create vncserver@:2.service and replace the user name in the configuration file and the corresponding file name and port number in the following steps. Please make sure you log in to the VNC server with the same username you used when configuring the VNC password before.

The VNC service itself uses port 5900. Given that there are different users using VNC, each person's connection will get a different port. The number in the configuration file name tells the VNC server to run the service on the subport 5900. In our example, the first VNC service will run on port 5901 (5900 + 1), and the subsequent ones will run on port 5900 + x. Where x refers to the x in the subsequent user's configuration file name vncserver@:x.service.

Before establishing a connection, we need to know the IP address and port of the server. An IP address is a unique identification number for a computer on a network. My server's IP address is 96.126.120.92 and the VNC user port is 1.

Execute the following command to obtain the public IP address of the server (LCTT Translation: If your server is placed on the intranet or uses a dynamic address, you can obtain its public IP address in this way).

# curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Other commands:

Close the VNC service:

# systemctl stop vncserver@:1.service

Disable the VNC service from starting at boot:

# systemctl disable vncserver@:1.service

Turn off the firewall:

# systemctl stop firewalld.service

Guess you like

Origin blog.csdn.net/m0_46829545/article/details/131475746