3

I tried many ways such as

terminator -x abc.sh The above command gives me this error:

You need to run terminator in an X environment. Make sure $DISPLAY is properly set

I also tried

gnome-terminal -x ./abc.sh

The above command gives me the error below

Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Failed to parse arguments: Cannot open display:
1
  • Use screen or similar or use & script.sh &
    – Panther
    May 26, 2018 at 12:45

3 Answers 3

3

Assuming Ubuntu to Ubuntu

If you use Windows 10 desktop and want to connect to your ubuntu server

A simple solution is to install and use Putty in Windows according to the following link,

www.putty.org/

PuTTY is an SSH and telnet client, developed originally by Simon Tatham for the Windows platform. PuTTY is open source software that is available with source code and is developed and supported by a group of volunteers.

You can download PuTTY here.

You can start one or more Putty windows and run different tasks via ssh.


As described in a comment by @SergiyKolodyazhnyy, you can use xrdp to open remote desktop session from Windows to Ubuntu.

As described in a comment by @PerlDuck, you can install and use an X server in Windows to be able to run graphical application programs via ssh, but according to your original question, it might be overkill.

4
  • what if i am using windows 10 desktop and i want to connect to my ubuntu server ?
    – Avery
    May 26, 2018 at 13:44
  • 1
    @Avery Then you need to install an X server on Windows, e.g. xming.
    – PerlDuck
    May 26, 2018 at 14:21
  • 1
    That doesn't sound proper. You could use xrdp to open remote desktop session from Windows to Ubuntu, but installing X server seems to be an overkill, especially since what OP needs is a client , not a server, technically. May 27, 2018 at 9:02
  • It should work with Putty from one or more command line window(s) in Windows.
    – sudodus
    May 27, 2018 at 9:22
2

Here's the thing: terminator and gnome-terminal are GUI apps. If your script doesn't require GUI and is a simple shell script, then you could run it in your ssh session just fine without needing a terminal emulator. Of course, your script needs to live on the filesystem where you're trying to run the script.

If for some reason you absolutely need terminator or gnome-terminal, you could always make use of xrdp to start remote desktop session. That's of course if the Ubuntu system you're trying to access has X server at all; server computers for instance often don't have any GUI because it's a security risk.

2

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:

enter image description here


5. References and more examples are available here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .