61

Is there a way to open a terminal window ( or tab ) with another instance of an open SSH session ?

EDIT: I have a terminal window with a remote ssh session into a server. I want to open a new tab connected to the same remote ssh session.

With File->New Tab I get another tab but I have to open another ssh session to the remote server.

Tulains Córdova
  • 1,609
  • 3
  • 20
  • 28
  • If you use ssh-copy-id and a local SSH config, connecting to yourwebserver.company.net is just like 'ssh web', even no need to authenticate. – davidbaumann Jun 05 '15 at 16:14
  • @davidbaumann The comment is too terse. Could you elaborate it into an answer? What is 'ssh web'? How does that make possible to open a new terminal window with the same session as ? – Tulains Córdova Jun 05 '15 at 16:26
  • It's not the same session, but connecting by SSH will BR so.mich easier you won't need any other tricks. – davidbaumann Jun 05 '15 at 17:41
  • @davidbaumann, same session means same user (if you did su - someuser), same environment variables (if you sourced a src file), same working dir, same history, etc. Connecting without being asked for a password is nice but doesn't do all the rest. – Tulains Córdova Jun 05 '15 at 18:20

5 Answers5

50

Perhaps this could help: screen

It is not installed by default on Ubuntu, but is provided in the repositories.

It is very useful: ssh to a remote host and type screen to enter a screen session.

Start whatever time consuming task you like, and then press Ctrl+A followed by Ctrl+C to create a new window in the screen session.

This will spawn a new shell where you can do what you want. Create even more windows using the same command.

To switch between two windows quickly, use Ctrl+A followed by Ctrl+A again. As you can see, Ctrl+A puts screen in command mode.

Ctrl+A Ctrl+D will detach from the screen session. You can then disconnect from the host and log in again later and use screen -dr to resume your session.

To go to a specific screen window, type Ctrl+A followed by a number.

Screen will exit when all active windows are closed (or the shells within have exited).

I got this from SSH and SFTP sessions.

Edit-

tmux is an alternative to screen. http://www.sitepoint.com/tmux-a-simple-start/

abchk1234
  • 1,789
  • 1
  • 15
  • 24
  • 1
    Does "screen" need not be installed in the remote host ? – Tulains Córdova Nov 27 '14 at 14:03
  • I suppose it does. – abchk1234 Nov 28 '14 at 14:46
  • I have to connect to many **production** servers: Solaris servers (new versions as well as legacy versions), Debian servers (several versions) and RedHat servers (several versions). Having to install new software in all of them is out of question. – Tulains Córdova Nov 28 '14 at 16:10
  • Ah, here is something a friend told me about how he manages multiple machine installs: http://bjoernvold.com/forum/viewtopic.php?f=11&t=3228 – abchk1234 Nov 29 '14 at 17:08
  • 1
    When I use screen, I cannot scroll up and down, whereas I can scroll up and down in the normal command line (the one I get right after doing `ssh user@machine`). – ComputerScientist Aug 20 '18 at 22:46
  • To scroll in screen, one needs to go in to copy mode by pressing Ctrl + A followed by [ key. https://www.cyberciti.biz/faq/scroll-up-down-look-at-data-in-gnuscreen-using-pageup-pagedown-keys/ I agree its not very convenient. – abchk1234 Mar 06 '19 at 13:45
  • Ctrl+A followed by k: kill the current window – yukelid Sep 01 '23 at 08:34
24

Connect to your remote server with:

ssh -X [remote-host]      

Now run:

gnome-terminal &

A terminal will appear on your local computer. From this terminal, you can run as many tabs or terminal windows as desired. They will all use the same ssh connection and be connected to the remote machine.

mitchus
  • 195
  • 1
  • 9
L. D. James
  • 24,768
  • 10
  • 68
  • 116
  • The remote host is Debian and it doesn't have gnome-terminal. What is the name of the terminal app in Debian ? – Tulains Córdova Nov 27 '14 at 13:49
  • 1
    @user1598390 On my installation `(Version 7 - wheezy)` the default terminal app is gnome-terminal. There are other terminal applications install. They are `xterm`, `lxterm`, and `uxterm`. I believe the most common on most distros will be `xterm`. You can get a list of the terminal applicatons with the commandline `cd /usr/bin;ls *term*`. – L. D. James Nov 27 '14 at 14:44
  • 2
    I tried it and it doesn't work for me. – Tulains Córdova Nov 28 '14 at 16:14
  • What is the error message you get when you execute `cd /usr/bin;ls *term*`? In other words, what is the cli you executed and what was the error message? – L. D. James Nov 28 '14 at 16:27
  • The error is when executing `xterm&` --> `[1] 26953 postgres@serverxxx:/usr/bin$ X11 connection rejected because of wrong authentication. xterm: Xt error: Can't open display: localhost:10.0` Even though I ran `xhost +` in the local machine. And even when I did `ssh -X` – Tulains Córdova Nov 28 '14 at 16:31
  • @user1598390 You are having other issues that are separate from running an X terminal. Most likely you would get the same error if you tried to run xeyes and any other X application. You may have inadvertently deviated from the original login ID that was used to start the initial session. You can respond here or go into a chat room I setup to have extended discussion on this matter. The resolution may be to make sure you're using the same logged in ID in every case regarding this session instance. (http://chat.stackexchange.com/rooms/19029/expoundonanswer) – L. D. James Nov 28 '14 at 16:47
  • I, in fact, deviated from the original login ID because the postgres user is prohibited by the administratos to open a ssh session, one has to login with a personal ID, then `su` into postgres. It works if don't do `su - postgres`. Remains to be solved how to do the trick after you `su -` into another user. – Tulains Córdova Nov 28 '14 at 17:12
  • The Stack Exchange system is advising for this to be moved into chat. They don't want the comments area to be used as a chat utility. I'll try to answer your new question by saying you may have access to the DISPLAY session if go into the new terminal before changing the userID, then running su to execute what you're trying to do. – L. D. James Nov 28 '14 at 17:17
  • Although this technically works, it only does so if the remote server has gnome-terminal installed. Additionally, it bogs down the network. Ideally, a better solution would entail saving the connection information and starting an additional session with the same information. I have not found a solution for this yet but hope to (how I ended up here) – YayDK Mar 11 '18 at 22:46
  • Your question appears to be different from the user's question. I would use different `ssh` sessions to have two different terminals. As far as **gome-terminal** install, I try to gear my answer close to the user's question and the users objective. He specified gnome-terminal as part of the question (the tag part). Also, gnome-terminal is shipped as part of the Ubuntu suite. Some of the other flavors such as `Lubuntu` and `xubuntu` might not have it installed by default, but as easy, `sudo apt install gnome-terminal` will fix that... which the OP may have done. – L. D. James Mar 12 '18 at 02:26
  • If you see that gnome-terminal is not installed in the server, try `xterm`. It should be there on all linux systems – Clyt May 11 '19 at 08:14
  • 3
    This will forward the image of the terminal, it doesn't open another one locally so it wasted a huge amount of bandwidth. – Calmarius Apr 02 '20 at 14:04
  • I get following error: `libGL error: No matching fbConfigs or visuals found` – alper Jul 03 '20 at 18:51
  • `gnome-terminal` opens on the host desktop for me. I had to use `xterm` instead – JolonB Aug 26 '21 at 22:12
  • This is the only great answer in the internet. Thank you very very much. – Sekomer Feb 17 '22 at 23:20
  • This is a really bad answer, just like @Calmarius points out. Downvoted since none of the X forwarding/bandwidth issue is mentioned or explained. – Moberg Apr 28 '22 at 05:50
2

most probably you might use OpenSSH control master multiplexing described with examples here : https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing

  • 1
    Welcome to AU. To be very useful, you should summarize the key points of the link you refer to. This way, even if the linked document disappears, your answer remains valid. – Marc Vanhoomissen Nov 26 '19 at 14:54
0

I've done this before: Launch gnome-terminal on the server. ctrl+Z then bg from your original terminal window.

I normally use ssh -X -C, so you might have to do that, but I'm not sure. Also, it'll use your server's settings instead of the ones on your machine, which is sometimes inconvenient.

-4

Yeah, in the terminal (menu) click on File -> New Tab

to open new tab in terminal

or are you asking for something else?

abchk1234
  • 1,789
  • 1
  • 15
  • 24