How to configure VNC server in cent OS 7?

If you want to access your Linux machine's GUI within your network or over the VPN then VNC server will be the best way. Here I am sharing how to set up or configure VNC server in CentOS 7 step by step. In this scenario we have a CentOS 7 machine with the desktop environment, so we don’t need to install the desktop environment, we can start installing VNC packages directly. 

Install Tiger VNC server using below command 

sudo yum install tigervnc-server -y 

Then create a VNC service for your user. 

sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service 

Open newly created service and update username in it.  

The raw file will look like this.  

[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 2>&1 || :' 
ExecStart=/usr/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 2>&1 || :' 

[Install] 
WantedBy=multi-user.target 

Replace <USER> with your username in this file. [For example, I am replacing it with my user.] 

[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 2>&1 || :' 
ExecStart=/usr/sbin/runuser -l manav -c "/usr/bin/vncserver %i" 
PIDFile=/home/manav/.vnc/%H%i.pid 
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' 

[Install] 
WantedBy=multi-user.target 

If you want to create it for root user, then it will be like as below. 

[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 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 

Now you need to reload systemd for update settings.  

sudo systemctl daemon-reload 

Once it is done, you need to create VNC password. [Here we are creating it for user : manav, then we will run this command under his account.] 

vncpasswd 

You need to exclude created VNC service from the firewall, so it will not block its connection. 

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

And finally, enable and start VNC service. 

sudo systemctl enable vncserver@:1.service 
sudo systemctl start vncserver@:1.service 

Now you can connect it from VNC viewer.  

Here my machine's IP is 192.168.1.2 and we have created service vncserver@:1.service hence our session id will be 1. That is why we have mentioned 192.168.1.2:1 here in below image.  

Once you enter your VNC session, you will be prompted to enter the password and will be done. 

Comments

Popular Posts