Building a linux terminal server with vncserver
by Brian on Jun.09, 2017, under Computer Stuff, Linux
Sometimes you need to allow multiple people to login to a linux server, and run something that requires Xorg/X11/Xwhatever they’re calling it these days. (xenocara if you’re cool ;)
So, from the start. Install your OS, (I’m using Ubuntu) install gnome, xfce4, openssh-server and vncserver, and add your users.
-
Setting up your user accounts for VNCserver
Log in as your first user, and run the command: vncserver
in a terminal. This will start an instance of the vncserver, and allocate an available port to you. It should look something like this:
username@laptop:/home/username$ vncserver
You will require a password to access your desktops.
Password: (enter password)
Verify: (verify password)
xauth: file /home/username/.Xauthority does not exist
New 'laptop:1 (username)' desktop is laptop:1
Creating default startup script /home/username/.vnc/xstartup
Starting applications specified in /home/username/.vnc/xstartup
Log file is /home/username/.vnc/laptop:1.log
Immediately after this, you’ll want to kill the server with:
username@laptop:/home/username$ vncserver -kill :1
You’ll want to do that for each user account, so later, we can use their passwd file to start the vncserver as a system service, so it will run the vncserver every time the server boots up. The next user you add and run vncserver
for should get port :2 (or, 5902)
We can see that this was the first instance of vncserver to run on this server, because we were assigned port :1 (technically, port 5901)
This will also create the .vnc folder in your home dir. In my case, this is ‘/home/username/.vnc’. Inside this hidden folder, you’ll find the passwd file you created when setting your password for vncserver, and the xstartup file that is created by default.
It doesn’t tend to be extremely useful out of the box, so we’ll replace the contents of it with this for a Gnome desktop in your vnc session:
#!/bin/sh
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
x-window-manager &
gnome-session &
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
Replace it with this for an xfce4 desktop in your vnc session:
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
startxfce4 &
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey &
One you’ve got your xstartup configured the way you want it, run vncserver once more, and make sure that you get the desktop you expect when connecting.
Immediately after this, you’ll want to kill the server with:
username@laptop:/home/username$ vncserver -kill :1
-
Configure vncserver as a system service for each user
As of right now, each user would need to first ssh to the server, and start their instance of vncserver manually to take over an X desktop. This is not what we want. We want their individual vncserver instances to run every time the server boots, and this way, we can restart the vncserver instance for each individual user, rather than having to kill the entire server to reset one connection.
For that, we need to install some init scripts in /etc/init.d/ You need to be root to do this, or able to use sudo.
(at this point I wonder if I should outline how to edit files.. I feel like if you’ve read this far, you already know, or are googling it as I speak.. )
username@laptop:/home/username# vi /etc/init.d/vncserver-username
Insert the following into the newly created file:
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: vncserver:1
# Required-Start: networking
# Required-Stop:
# Default-Start: S
# Default-Stop: 0 6
### END INIT INFO
# The Username:Group that will run VNCserver
export USER=”username”
#${RUNAS}
# The display that VNC will use
DISPLAY=”1″
# Color depth (between 8 and 32)
DEPTH=”16″
# The name that the VNC Desktop will have.
NAME=”username on Laptop”
. /lib/lsb/init-functions
case “$1” in
start)
log_action_begin_msg “Starting vncserver for user ‘$USER’ on localhost:$DISPLAY”
su username -c “/usr/bin/vncserver :1 -geometry 1920×1080 -geometry 1280×1024 -geometry 1024×768 -f ~/.vnc/passwd”
;;
stop)
log_action_begin_msg “Stoping vncserver for user ‘$USER’ on localhost:$DISPLAY”
su username -c “/usr/bin/vncserver -kill :1”
;;
restart)
$0 stop
$0 start
;;
esac
exit 0