Hack and / - Lightning Hacks--SSH Strikes Back
Every year or so, I like to write a column I title “Lightning Hacks”. This column is inspired by the lightning talks common at most conferences. In a lightning talk, instead of having one speaker give a 60-minute presentation, multiple speakers give short 5–10-minute presentations. By the end of a lightning talk, you end up hearing about all sorts of cool topics that wouldn't have gotten their own time slot. In this column, I get a chance to talk about a few cool “hacks” I've run across that wouldn't fill an entire column by themselves.
In prior Lightning Hacks columns, I've covered a number of different topics, but this time I've decided to focus on only one: SSH. Like many system administrators, I spend a great deal of my day within SSH sessions, and over the years, I've found a few shortcuts and handy tips that I save in shell scripts so I don't forget them.
This first hack seems really simple—after all, I am adding only one extra flag to SSH. Normally, if you want to ssh into a machine and run a program, you simply pass the program at the end of your SSH command:
$ ssh email@example.com df
Yet, if you ever have tried to write a shell script that would automatically ssh in to a remote machine and launch mutt or screen or similar programs, you have seen the session either sit there or exit with some message like “Must be connected to a terminal.” I ran into this problem on my N900 palmtop when I wanted to launch two special terminal sessions: one that automatically reconnected to a remote screen session and another that loaded mutt. Yeah, that's right. I still prefer mutt and irssi, even on a palmtop. Neither worked though until I added the -t flag:
$ ssh -t firstname.lastname@example.org screen -dr $ ssh -t email@example.com mutt
The first example connects to the remote host and re-attaches my remote screen session (I run only a single screen session on my host and then use Ctrl-a c to create windows within that session). The second example simply runs mutt. The -t flag forces pseudo-tty allocation. It turns out that when you run programs like screen or mutt, you need to force SSH to create a pseudo-tty.
I know a million articles have been written about SSH tunneling, but this particular type of tunneling is so useful; however, I use it infrequently and forget the proper syntax. A problem you often may run into is needing to scp a large number of files between two servers (let's say londonweb1 and seattleweb1), but for some reason, the two machines are firewalled off from each other. Usually, you have one server that is able to ssh into both machines (let's call that server admin1), and if it were just one or two files that needed to be transferred, you could copy the files first from londonweb1 to admin1, then from admin1 to seattleweb1.
When you need to transfer multiple files (or perhaps pipe dd traffic) between the two sites, it can be impractical, if not impossible, to move data to an intermediary server first. That's where SSH reverse tunnels come in handy. With a reverse tunnel, you launch an SSH session from your intermediary server (admin1 in this case) to the first server (londonweb1) and open up a local high port that is unused, such as 2222. Then, you tell SSH to tunnel all traffic on that port over to the remote server (seattleweb1). Once the tunnel is set up, you can use scp as you normally would, except you point it to localhost port 2222.
To set up the tunnel, I would run the following command from admin1:
kyle@admin1:~$ ssh -R 2222:seattleweb1:22 londonweb1
The arguments to -R can be easy to mix up. Note that the last server in the command (londonweb1) is the server to which I log in. The first argument to -R is the port to open up on that server (2222). The next two arguments list to which server and port to forward any traffic (seattleweb1 and 22, respectively).
Once I log in to londonweb1, I can use scp (or rsync) like I normally would, but I point it to localhost port 2222:
kyle@londonweb1:~$ scp -r -P 2222 /var/www/mysite localhost:/var/www/
When I initiate this scp command, all the traffic enters the tunnel and goes to admin1, and then from there, it is forwarded to port 22 on seattleweb1. Keep in mind that this means if these machines are far apart, your bottleneck will be the slowest link between the servers.
If you are a security-minded individual in charge of a network, you may not like how easy it is to route around your basic firewall rules. It's important to realize that reverse tunnels also can be used to connect from inside your network to a person's home machine, so even with incoming firewall rules set, a user still could tunnel in.
Kyle Rankin is a systems architect; and the author of DevOps Troubleshooting, The Official Ubuntu Server Book, Knoppix Hacks, Knoppix Pocket Reference, Linux Multimedia Hacks, and Ubuntu Hacks.
|Dynamic DNS—an Object Lesson in Problem Solving||May 21, 2013|
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
- Roll your own dynamic dns
4 hours 7 min ago
- Please correct the URL for Salt Stack's web site
7 hours 18 min ago
- Android is Linux -- why no better inter-operation
9 hours 34 min ago
- Connecting Android device to desktop Linux via USB
10 hours 2 min ago
- Find new cell phone and tablet pc
11 hours 55 sec ago
12 hours 29 min ago
- Automatically updating Guest Additions
13 hours 38 min ago
- I like your topic on android
14 hours 24 min ago
- This is the easiest tutorial
21 hours 29 sec ago
- Ahh, the Koolaid.
1 day 2 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?