# yum -y install tigervnc tigervnc-server
vncpasswd
# firewall-cmd --permanent --zone=public --add-port=5901/tcp
success
# firewall-cmd --reload
success
cat .vnc/xstartup
#!/bin/sh
def
export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
xrdb $HOME/.Xresources
xsetroot -solid grey
autocutsel -fork
startxfce4 &
Running VNC as a System Service
Next, we’ll set up the VNC server as a systemd service so we can start, stop, and restart it as needed, like any other service. This will also ensure that VNC starts up when your server reboots.
First, create a new unit file called /etc/systemd/system/[email protected]
using your favorite text editor:
sudo nano /etc/systemd/system/[email protected]
The @
symbol at the end of the name will let us pass in an argument we can use in the service configuration. We’ll use this to specify the VNC display port we want to use when we manage the service.
Add the following lines to the file. Be sure to change the value of User, Group, WorkingDirectory, and the username in the value of PIDFILE to match your username:
/etc/systemd/system/[email protected] [Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target
[Service]
Type=forking
User=sammy
Group=sammy
WorkingDirectory=/home/sammy
PIDFile=/home/sammy/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target
The ExecStartPre
command stops VNC if it’s already running. The ExecStart
command starts VNC and sets the color depth to 24-bit color with a resolution of 1280×800. You can modify these startup options as well to meet your needs.
Save and close the file.
Next, make the system aware of the new unit file.
sudo systemctl daemon-reload
Enable the unit file.
sudo systemctl enable [email protected]
The 1
following the @
sign signifies which display number the service should appear over, in this case the default :1
as was discussed in Step 2..
Stop the current instance of the VNC server if it’s still running.
vncserver -kill :1
Then start it as you would start any other systemd service.
sudo systemctl start vncserver@1
You can verify that it started with this command:
sudo systemctl status vncserver@1
If it started correctly, the output should look like this:
Output● [email protected] - Start TightVNC server at startup
Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
Active: active (running) since Mon 2018-07-09 18:13:53 UTC; 2min 14s ago
Process: 22322 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :1 (code=exited, status=0/SUCCESS)
Process: 22316 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=0/SUCCESS)
Main PID: 22330 (Xtightvnc)
...
Your VNC server will now be available when you reboot the machine.
Start your SSH tunnel again:
ssh -L 5901:127.0.0.1:5901 -C -N -l sammy your_server_ip
Then make a new connection using your VNC client software to localhost:5901
to connect to your machine.