16 ultimate SSH hacks

Page 2 of 3

SSH tip #11: SSHFS is better than NFS

sshfs
is better than NFS for a single user with multiple machines. I keep a herd of computers running because it's part of my job to always be testing stuff. I like having nice friendly herds of computers. Some people collect Elvis plates, I gather computers. At any rate opening files one at a time over an SSH session for editing is slow; with
sshfs
you can mount entire directories from remote computers. First create a directory to mount your
sshfs
share in:

$ mkdir remote2

Then mount whatever remote directory you want like this:

$ sshfs user@remote2:/home/user/documents remote2/

Now you can browse the remote directory just as though it were local, and read, copy, move, and edit files all you want. The neat thing about

sshfs
is all you need is
sshd
running on your remote machines, and the
sshfs
command installed on your client PCs.

SSH tip #10: Log in and run a command in one step

You can log in and establish your SSH session and then run commands, but when you have a single command to run why not eliminate a step and do it with a single command? Suppose you want to power off a remote computer; you can log in and run the command in one step:

carla@local:~$ ssh user@remotehost sudo poweroff

This works for any command or script. (The example assumes you have a

sudo
user set up with appropriate restrictions, because allowing a root login over SSH is considered an unsafe practice.) What if you want to run a long complex command, and don't want to type it out every time? One way is to put it in a Bash alias and use that. Another way is to put your long complex command in a text file and run it according to tip #9.

SSH tip #9: Putting long commands in text files

Put your long command in a plain text file on your local PC, and then use it this way to log in and run it on the remote PC:

carla@local:~$ ssh user@remotehost "`cat filename.txt`"

Mind that you use straight quotations marks and not fancy ones copied from a Web page, and back-ticks, not single apostrophes.

SSH tip #8: Copy public keys the easy way

The

ssh-copy-id
command is not as well-known as it should be, which is a shame because it is a great time-saver. This nifty command copies your public key to a remote host in the correct format, and to the correct directory. It even has a safety check that won't let you copy a private key by mistake. Specify which key you want to copy, like this:

$ ssh-copy-id -i .ssh/id_rsa.pub user@remote

SSH tip #7: Give SSH keys unique names

Speaking of key names, did you know you can name them anything you want? This helps when you're administering a number of remote computers, like this example which creates then private key web-admin and public key web-admin.pub:

$ ssh-keygen -t rsa -f .ssh/web-admin
| 1 2 3 Page 2
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon