It is possible to run an application that requires GUI from the SSH session into the Desktop session if you really need it. I'm using the following approach to launch VMWare virtual machines when I need them but I'm not on the front of the computer.
I would emphasise you've mentioned that you are connecting to Ubuntu Server, that doesn't have Desktop environment installed by default. And in this case it is worth to use tmux
or screen
, or push the script into the background, or use a second SSH session. If a Desktop environment is installed into the server the following steps could be applied.
The following scripts works with Lightdm and Unity which are default for Ubuntu 16.04.
1. First requirement is that your user must be logged-in in Desktop session. That I'm using to achieve this is the following script (source and explanations):
#!/bin/bash
# NAME: lightdm-auto-login
main() {
# If the file '/etc/lightdm/lightdm.conf' exists create a backup copy
[[ -f /etc/lightdm/lightdm.conf ]] && mv /etc/lightdm/lightdm.conf{,.bak}
# Create autologin configuration for the current $USER = $1
echo -e "[Seat:*]\nautologin-user=$1" > /etc/lightdm/lightdm.conf
# Restart 'lightdm' while autologin option is enabled
systemctl restart lightdm.service
# Wait for a moment to complete the login process and remove the conf file
sleep 30 && rm /etc/lightdm/lightdm.conf
# Restore the backup if exists
[[ -f /etc/lightdm/lightdm.conf.bak ]] && mv /etc/lightdm/lightdm.conf{.bak,}
}
# Execute the 'main()' function with root privileges in the background 'sudo -b'
# Pass the curent $USER as arg (https://unix.stackexchange.com/a/269080/201297)
sudo -b bash -c "$(declare -f main); main $USER"
The script should be executed as regular user (that belongs to the sudoers group).
I would prefer to place the script in /usr/local/bin
to be accessible as shell command system wide. Don't forgot to make it executable.
2. Second, few environment variables (as $DISPLAY
, etc.) must be exported from the Desktop session into the SSH session. The following script will do that and also will launch the commands that are passed as positional parameters (source and explanations):
#!/bin/bash -e
# NAME: gui-launcher
# Check whether the user is logged-in
while [ -z "$(pgrep gnome-session -n -U $UID)" ]; do sleep 3; done
# Export the current desktop session environment variables
export $(xargs -0 -a "/proc/$(pgrep gnome-session -n -U $UID)/environ")
# Execute the input command
nohup "$@" >/dev/null 2>&1 &
exit 0
The script will work until the user is logged-in, including a locked screen.
I would prefer to place the script in /usr/local/bin
to be accessible as shell command system wide. Don't forgot to make it executable.
3. Usage:
Establish SSH session;
Execute lightdm-auto-login
;
Execute gui-launcher <commands or script>
, for example:
gui-launcher gnome-terminal -x bash -c "<my command or script>; exec bash"
Note the last sub command exec bash
will keep the launched gnome-terminal
open, after the previous command is finish.
4. Demonstration:
5. References and more examples are available here.
script.sh &