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 VP of engineering operations at Final, Inc., the author of a number of books including DevOps Troubleshooting and The Official Ubuntu Server Book, and is a columnist for Linux Journal. Follow him @kylerankin.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- My +1 Sword of Productivity
- Managing Linux Using Puppet
- Non-Linux FOSS: Caffeine!
- Doing for User Space What We Did for Kernel Space
- SuperTuxKart 0.9.2 Released
- Parsing an RSS News Feed with a Bash Script
- Google's SwiftShader Released
- Rogue Wave Software's Zend Server
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide