如何通过ssh连接到Ubuntu 16.04服务器后在新终端中运行脚本(.sh)文件?

我试过很多方法,比如

terminator -x abc.sh上面的命令给了我这个错误:

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

我也试过了

 gnome-terminal -x ./abc.sh 

上面的命令给出了下面的错误

 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: 

假设Ubuntu到Ubuntu

  • 如果已在服务器中安装了相关软件,则可以使用ssh -X远程ssh -X ,然后运行terminatorgnome-terminal等图形应用程序。 看到这个链接,

    从Ubuntu 16.04“桌面”远程GUI访问Ubuntu 16.04“服务器”的最简单方法是什么?

  • 您也可以在本地启动其他终端窗口,并在这些窗口中使用ssh远程ssh ,这样就可以在服务器中运行多个文本模式应用程序(每个都在自己的终端窗口中)。

如果您使用Windows 10桌面并想要连接到您的ubuntu服务器

一个简单的解决方案是根据以下链接在Windows中安装和使用Putty,

http://www.putty.org/

PuTTY是一个SSH和telnet客户端,最初由Simon Tatham为Windows平台开发。 PuTTY是一个开源软件,提供源代码,由一组志愿者开发和支持。

你可以在这里下载PuTTY。

您可以启动一个或多个Putty窗口并通过ssh运行不同的任务。


如@SergiyKolodyazhnyy的评论中所述,您可以使用xrdp打开从Windows到Ubuntu的远程桌面会话。

如@PerlDuck的评论中所述,您可以在Windows中安装和使用X服务器,以便能够通过ssh运行图形应用程序,但根据您的原始问题,它可能有点过分。

事情就是这样: terminatorgnome-terminal是GUI应用程序。 如果你的脚本不需要GUI并且是一个简单的shell脚本,那么你可以在你的ssh会话中运行它而不需要终端模拟器。 当然,您的脚本需要存在于您尝试运行脚本的文件系统上。

如果由于某种原因你绝对需要terminatorgnome-terminal ,你总是可以使用xrdp来启动远程桌面会话。 当然,如果您尝试访问的Ubuntu系统完全具有X服务器; 例如,服务器计算机通常没有任何GUI,因为它存在安全风险。

如果您真的需要,可以运行需要从SSH会话进入桌面会话的GUI的应用程序。 我正在使用以下方法在需要时启动VMWare虚拟机,但我不在计算机的前端。

我要强调你已经提到你正在连接到Ubuntu Server,默认情况下没有安装Desktop环境。 在这种情况下,值得使用tmuxscreen ,或将脚本推送到后台,或使用第二个SSH会话。 如果在服务器中安装了桌面环境,则可以应用以下步骤。

以下脚本适用于Lightdm和Unity,它们是Ubuntu 16.04的默认设置。


1.首要要求是您的用户必须在桌面会话中登录。 我用来实现这个目的的是以下脚本( 来源和解释 ):

 #!/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" 
  • 该脚本应作为普通用户(属于sudoers组)执行。

  • 我更喜欢将脚本放在/usr/local/bin ,以便作为系统范围的shell命令访问。 别忘了让它可执行。


2.其次,必须将很少的环境变量(如$DISPLAY等)从Desktop会话导出到SSH会话中。 以下脚本将执行此操作,并将启动作为位置参数传递的命令( 源和说明 ):

 #!/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 
  • 该脚本将一直有效,直到用户登录,包括锁定的屏幕。

  • 我更喜欢将脚本放在/usr/local/bin ,以便作为系统范围的shell命令访问。 别忘了让它可执行。


3.用法:

  • 建立SSH会话;
  • 执行lightdm-auto-login ;
  • 执行gui-launcher , 例如 :

     gui-launcher gnome-terminal -x bash -c "; exec bash" 

    注意最后一个子命令exec bash将在上一个命令完成后保持启动的gnome-terminal打开。


4.示范:

在此处输入图像描述