Hack and / - Lightning Hacks Strike Twice
It was exactly one year ago when I wrote my first Lightning Hacks column. The column was inspired by lightning talks that often occur at conferences. In a lightning talk, instead of one speaker giving a presentation for an hour, different people give 5–10-minute presentations. The idea is that many people are working on cool projects but may not have an hour's worth of material to present. And, the audience gets a rapid-fire presentation of a few different topics instead of one long lecture.
The idea behind lightning hacks is similar—I can cover some quick hacks I think are interesting but that don't warrant a full column. For example, in the first Lightning Hacks column [June 2008], I talked about an expanded wmctrl script that reset all of my windows to default locations and sizes, another script that toggled my laptop output for when I connect to a projector, and finally, I discussed how to use rdiff to create small diff files for large binaries. Now that a year has passed, I think it's time for lightning to strike twice.
Here's a quick one. I've mentioned this trick to a number of people, and I get one of two responses. This is one of those tricks (like Ctrl-R shell expansion in bash) that you either already know about and seems obvious to you, or one that you can't believe took so long to discover.
If you have spent a lot of time on the command line, you probably have heard about the pushd and popd scripts. These scripts let you create a stack that you can push directories on to and later pop them when you want to return to a previous directory. This script is cool, except you have to know in advance you want to save a directory and push it, so you can pop it later—I never seem to remember. Generally speaking, what I need is some quick way to go back to my previous directory. Lucky for me, bash's cd has this feature built in. All I do is type:
$ cd -
Bash keeps track of your current working directory in the $CWD variable and your previous directory in $OLDPWD. If you type cd -, bash substitutes - with $OLDPWD. Although you certainly could just type cd $OLDPWD, cd - is faster and easier to remember.
If you have to manage a lot of servers or run remote scripts in cron, SSH keys are a lifesaver. It's so nice to be able to ssh to a machine and instantly log in without typing a password. Of course, one of the more annoying parts of the process can be setting up the SSH keys on the remote host. Typically, the process goes something like this: run ssh-keygen locally, scp the ~/.ssh/id_rsa.pub to the remote server, then ssh to the remote server and append that key to your remote ~/.ssh/authorized_keys file.
The above method works, but if you can do the entire thing with a one-liner, why wouldn't you? Here's the SSH one-liner that will copy your local SSH key to the remote host, so you have to type the password only once in the whole process:
$ ssh user@server.example.net "cat >> ~/.ssh/authorized_keys" ↪< ~/.ssh/id_rsa.pub
Many great imaging tools are available, but for me, it's still hard to beat dd (unless your drive is dying, in which case you should use ddrescue). It is such a powerful, blunt, ancient UNIX tool, it's hard not to love it. These days, I don't image too many drives. I use kickstart for server deployments and rsync when I want to migrate files. That said, I still do image drives when I want to perform forensics on the host.
One problem you often have when you image drives is that your server might be in a data center hundreds or thousands of miles away. Even if the server is close by, you might not be able to add an extra drive on the fly. In either case, most sysadmins end up imaging the drive over the network. Traditionally, this was done via netcat, but these days, you always have to figure out some port you can use that won't be blocked by the firewall. Another problem is that netcat will transmit potentially sensitive data over the network in plain text. The modern solution to this problem is to use SSH. Many servers now have SSH running and available out of the box, and with modern processor speeds, the encryption overhead shouldn't be too bad either.
The one-liner to image drives over SSH works much like the one I used for SSH keys above. It takes advantage of the fact that if you pipe data or redirect input to SSH on the command line, it will forward it to the remote connection. So, if I wanted to image /dev/sda on my local machine to a file called /media/disk1/sda-image.img on server.example.net, I would type the following:
$ sudo dd if=/dev/sda | ssh user@server.example.net "cat ↪> /media/disk1/sda-image.img"
If I didn't want to image to a file and instead wanted to image directly to a drive on server.example.net, I simply could replace /media/disk1/sda-image.img with that device file (I just would need to log in as root).
Because you can image a drive over SSH, it makes sense that you can use a variation on the command to restore your image back to a drive. Here's the inverted version of the above command that I would use if I wanted to restore the /media/disk1/sda-image.img image I created back to /dev/sda:
$ ssh user@server.example.net "cat /media/disk1/sda-image.img" ↪| sudo dd of=/dev/sda
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.
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.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Non-Linux FOSS: libnotify, OS X Style
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- RSS Feeds
- Reply to comment | Linux Journal
3 hours 57 min ago - Yeah, user namespaces are
5 hours 14 min ago - Cari Uang
8 hours 45 min ago - user namespaces
11 hours 38 min ago - yea
12 hours 4 min ago - One advantage with VMs
14 hours 33 min ago - about info
15 hours 6 min ago - info
15 hours 7 min ago - info
15 hours 8 min ago - info
15 hours 10 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
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?




Comments
dual head ?
I know that in ubuntu is now allowed to enable dualhead monitor and that probably it is done with xrandr. What about it on other distro ? on fedora there is something related in desktop configuration but it seem to not work
Help Stomp out Control-C programming
two things about this article; first use ssh-copy-id to setup authorized keys on remote hosts.
second, the copy-pasted code in your orientation script drove me nuts so i rewrote it.
#!/bin/sh ORIENTATION_FILE="/tmp/.orientation" function set_orientation() { local orientation="$1" if [ "$orientation" -ne "270" ]; then xrandr --auto fi xrandr --output LVDS --rotate $orientation record_orienation "$orientation" } function record_orientation() { echo "$1" > $ORIENTATION_FILE } function get_orientation() { echo "$(cat $ORIENTATION_FILE)" } function set_osd() { local message="$1" osd_cat --shadow=2 --align=center \ --pos=bottom --color=green \ --delay=2 --font=lucidasanstypewriter-bold-24 \ --offset 40 < "$(cat $message)" } case "$(get_orientation)"; in '90') set_orientation "180" set_osd "180" ;; '180') set_orientation "270" set_osd "270" ;; '270') set_orientation "0" set_osd "Normal" ;; *) set_orientation "90" set_osd "90" ;; esac exit 0Some problems here
I believe you cannot call a function before defining it. i.e. record_orientation should be defined above set orientation.
I don't think you can pass '0' '90' '180' or '270' as arguments to xrandr
Also I think the xrandr syntax is outdated for ubuntu 9.04 users in both this rewrite and the original lightning hack.
Line 34: case statement: remove the ';' before 'in'
schelcj: Did you test this before posting it?
-Z