Multiple terminals in a single SSH session

When I sit to work, I am using linux terminals 95% of the time. And I tend to use lots of them. One for vim, another for compiling and testing, another for checking whatever files the program outputs, and so on… If you’re a multiple windows/tabs kind of guy (or gal, why not?) like me, you know what I am talking about!

Ctrl + Shift + N is my favorite shortcut ever on gnome-terminal, but when I am not physically on the machine but working via SSH, it is not nearly as fun. In fact, it is annoying as hell as it opens a new local terminal, and I have to log in again, go to the path I am interested in, and only then being able to do whatever I want to do (which, sometimes, is pretty quick and I end up closing the terminal, just to regret next time I need it again). Thankfully, I found a nice way to solve this!

Normal Use (under Linux)

Well, a friend of mine reminded me of cool switch that is ssh -X. What it does is enable X11 forwarding. In other words, it redirects to your local (from your point of view) computer all X/graphical applications you run remotely; that is, if you run, let’s say, gnome-terminal, it will show a terminal on your computer that is actually running on the remote (again, from your point of view) machine.

Ok, so what do I do with that? How about Ctrl + Shift + N? Cool, uh? 😎

Enough stories, this tip is about opening multiple remote terminals through SSH and only having to log in once. What I do is:

$ ssh -X mylogin@remotemachine gnome-terminal
Password: ...
[Ctrl+Z]
$ bg
$ exit

Ok, that sounded complicated, so I’ll go over it line-by-line:

  1. The syntax for ssh is ssh [options] [login@]host [command]. So what I am doing is connecting with the -X switch, and running gnome-terminal remotely. If you provide a command like this, after the process terminates, the connection is closed.
  2. I enter my password to connect…
  3. By now, the terminal we ran remotely should pop on your screen. Go back to your local terminal and hit Ctrl + Z to pause the ssh process. You’ll get a prompt back.
  4. On the prompt, type bg, which unpauses the paused process, but runs it on background.
  5. You now close your terminal with exit. It is important to use exit and not clicking on the close button from your window manager, otherwise the ssh process we sent to background will be terminated too, and your remote terminals will disappear. I’m not sure why, but it works like that.

Now you try it… It’s fun, isn’t it? But why, oh why haven’t I just gone with the ssh -X mylogin@remotemachine gnome-terminal & one-liner? Well, because I couldn’t type my password that way. I know there are ways to allow direct login without password, but I have never fiddled with that that’s covered in anoter post. In case you got direct login working, you could use the one-liner.

If you’re not much into GNOME, things should probably be just the same with other terminals. However, if you’re on a Windows machine… fear not, as you can use this too!

Using it under Windows

A friend once told me about a X Server you can install on Windows that, along with PuTTY (a.k.a. the stuff that makes your Windows useful), can be used to make your terminal windows pop as if you were running Linux! (why aren’t you, by the way? 😉 )

The name of the program is Xming, and that’s all you should need. He gave me the instructions back then, but it’s been a while, so I found this (30sec tutorial with screenshots) and this (text-only, with some troubleshooting info) on Google, tested it, and it worked nicely. A bit slower than on Linux (and the terminal looked pretty ugly – maybe I needed to install the fonts package), but worked.

Quick note, although it is out of the scope of this post: you could set up some shortcuts on the system tray icon for applications you launch regularly, like this. I haven’t tried it, and it is up to you to learn how. In case you feel like writing a nice tutorial or something, I’d be happy to post it here.

Ubuntu Troubleshooting

I was perfectly able to use the above method to connect to the Fedora machine where I work, but couldn’t do the same to my home Ubuntu, where the X applications never ran. I googled around, and found this blog post that might be useful for people running Ubuntu Server. However, that is not my case, but the interesting part for me was this link on the blog post indicating the necessary configs on /etc/ssh/ssh_config (note it is not the daemon config – not sure why!) which were turned off for me. After that, everything worked perfectly.

On the same blog, there was also this tip for speeding up X11 forwarding, which could be useful eventually.

Wrapping up

Hope you liked it. In case you were wondering, yes, this was what got my friend so excited that made me go ahead and create this blog. Took me a while because I wanted to make it all nicely detailed. So if you get all excited too, I’d love to hear about it on the comments! 😉

Advertisements

7 Responses to “Multiple terminals in a single SSH session”

  1. Thighnam Says:

    emm. really like it ))

  2. octoberdan Says:

    What about cases when gnome-terminal isn’t installed on the remote computer?

  3. asdasd Says:

    I want to marry you I love you. AWESOME

  4. tonybaldwin Says:

    Yeah, but, what if there’s no X server on the remote machine (like a web server)?
    Then you use gnu screen.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: