SSH Tricks
SSH Tricks
SSH Tricks
http://www.debian-administration.org/articles/438
One can do a lot more with ssh than use it for remote terminal session. Here we'll show how to
copy files using ssh, use ssh as part of a pipe, vnc or samba forwarding via ssh and mounting
filesystems using ssh (fuse + sshfs)
(Several of these subjects have been covered upon this site before.)
Contributors:
Sebastian Broekhoven
pointed to obvious method of file copying over ssh - the scp command.
http://www.thenetwork.nl
Hardik Dalwadi
showed the simple method of taking advantage of log-in without password (section 6.1).
http://www.biostat.jhsph.edu/bit/nopassword.html
the version with pictures
13/10/06 15:30
http://www.debian-administration.org/articles/438
This transfers a local file "myfile" to the server. The location of the resulting file is the home
directory of the "user".
The first "cat" command simply outputs the contents of "myfile" to stdout, the stdout gets piped to
the local part of the ssh session and transferred to stdin of the shell on the server. The second
"cat" command (runs on the server) outputs the transferred stdin to stdout. The stdout gets
redirected to the "myfile" file on the server (one can choose a different name for the resulting file as
required). After the transfer is complete, the ssh exits and user is left in the local terminal window.
If different location is required for the resulting file on the server, two options are available:
specifying a full path for the file or changing to the desired directory before running the "cat"
command (i.e. running two commands in sequence on the server):
# cat myfile | ssh user@server "cat > ~/backups/myfile"
or
# cat myfile | ssh user@server "cd ~/backups; cat > myfile"
To transfer in the opposite direction (from remote to local), the pipe is reversed:
# ssh user@server "cat myfile" > myfile
or from a different location:
# ssh user@server "cd ~/backups; cat myfile" > myfile
2 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
13/10/06 15:30
http://www.debian-administration.org/articles/438
4 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
5 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
# ln -s ssh-to-server blue
Then (if your path includes /usr/local/sbin) one can simply ssh to "red" server by typing red (or run
a command uptime on the "red" server by red uptime). If the servers need to be accessed by fully
qualified domain name, symlink the "ssh-to-server" script to the FQDN.
In "6 Setting up ssh authentication using keys" step 2 (transfer of key) can be done by ssh-copy-id(1).
To transfer your public key to the server, use ssh-copy-id, as this sorts out the permissions for you.
Just my 2 cents:
Rsync supports copying symlinks, unlike scp which turns symlinks into actual files on the remote end.
Most versions of rsync actually use ssh by default. Rsync can also be more efficient than scp when you
are just updating the remote directory. There are other advantages to using rsync instead of scp.
However, for simple single-file transfers I still usually use scp.
Also, another way to simplify logging into servers is to use the ssh-argv0 command. You use it pretty
much the same way as the script in part 6.1. Create a symlink that points to /usr/bin/ssh-argv0,
using the remote machine's domain name as the symlink's filename:
ln -s /usr/bin/ssh-argv0 /usr/local/bin/web.server.com
Then you use it just as with the script above: web.server.com uptime
rsync trick
Posted by shavenwarthog (71.107.xx.xx) on Thu 7 Sep 2006 at 17:19
Seconded: 'rsync' is a fantastic tool. I now use it even for local copies, like from a flaky cdrom. The
"-P" (show progress) "-a" (archive, recursive copy), and "-v" (verbose) options are very often used.
To move files, use a command like this:
rsync -av --remove-sent-files zoot.tar.gz /tmp/
13/10/06 15:30
http://www.debian-administration.org/articles/438
Note that passwordless keys are a security risk: anyone who has cracked your account now has access
to all the machines with non-passworded keys for you.
The nice solution is to limit the access that a non-passworded key can grant. On the target account, edit
the ~/.ssh/authorized_keys file.
For example, to limit the ability of a key to only allow forwarding port 6000, without logins, preface the
key in authorized_keys with
no-pty,permitopen="localhost:6000"
There are more of these limitations documented in the man page for sshd.
Another important resource not mentioned is the ~/.ssh/config file. This can contain default options on a
general and per-host basis, and is the best way of organizing the plethora of keys that you will no doubt
accumulate in the course of administering a dozen or a thousand machines.
-dsr-
The number of keys that you have to administer should not be increasing with the number of hosts
that you have, but with the number of users that can enter them.
I mean, that each user (by user I mean person, not "unix account") should have one and only one
prublic/private key pair (one pair! only): Not one pair per machine! One per, not depending te number
of machine that you have.
IMHO, the ideal scenario is that every user own it's own key pair, and every time he starts his desktop
computer (or his workstation, his laptop...) he loads his private key into the ssh-agent (or Putty Agent
or whatever). At that time he should introduce his passphrase.
On the other hand, the user (or sometime for administration sakes, the system administrator -- see
further) should place the unique user's public key (every user only has 1 public key, remember?) in
the ~/.ssh/authorized_keys of all the unix account of every systems he has access.
In that way, the user can log into all it's accounts by using SSH private/public key authentication and
SSH Agent forwarding. He can alsojumps from one machine to another (without providing password).
At my work, we have 206 Unix boxes(HP-UX, Solaris, and a few Linux). We administer 85 public keys
for 85 users and have set up access to over 2100 unix users amongs that 206 boxes. We have
created a set of scripts for administrating the users into groups of users so we can easily distribute
their public keys giving permission for a unix accounts on a group or user basis. For example we
distribute the public keys of all the member of the ADMUNIX (Unix System admins) to the root's
authorized_keys of all the managed systems.
So, if you think about it: 85 users (persons), 206 boxes; a total of 2100 unix accounts: only 85 public
keys. Not that much!
Alex
Le Blog d'Alex -- http://adumont.serveblog.net/
7 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
host1--->FW--->host2
I mean, I need to reach the host1, but I have no access on fw, but my friend sit next to host1 and he can
connect to host2. And when the connection is living from host1 to host2 and I can use host1.
I think what you are looking for is ssh's -D option: Use e.g. "ssh -D 7070 B" to establish your
connection. Configure your browser proxy settings to use localhost:7070 as a SOCKS proxy. Boom,
your browsing traffic is tunnelled through B.
Beware of DNS leaks, though: SOCKS 4 doesn't support proxying DNS. Even with SOCKS 4a/5 (ssh
can do 4/5, dunno about 4a), some apps still resolve names locally and only transmit the resulting IP
to the proxy. E.g. firefox resolves locally by default (see the network.proxy.socks_remote_dns setting
to change that). Privoxy is also mentionned here and there as a DNS leak solution throughout the
web, though I never personnally tried that. Tor's website has more details about DNS leaks.
Firefox tip: There a numerous extensions designed to help with proxy management (such as
SwitchProxy or FoxyProxy).
Last word: Such things are a good way to _not_ get along well with your sysadmin if local policy says
you should not do it (TM). Sure, the traffic is tunnelled/encrypted, but don't think it doesn't show at all
-- any decent network person will understand what you are doing in a breeze. You Have Been
Warned.
8 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
Very cool.
In the first example of section 1.2.2, is there really any reason you need the "cat" command? It seems
to be doing nothing.
"cat | tar -x -f -" : if you look at the version with pictures, it is on the first picture - basically, what you
pipe to ssh will end up on stdin of the remote shell, the cat will dum that to stdout and "|" will transfer
that to tar...
Um, right, but you could leave out the "cat |" entirely, and get the same result; the tar gets fed the
file on stdin regardless.
A "cat" with no arguments is always a no-op as far as I can tell.
connection reuse might be especially helpful if you decide to use advanced bash completions (source
/etc/bash_completion) and then complete smth like
scp boomba.laboomba.com:/home/sdfaq/skd
and press TAB to complete - then it will complete you remote path... and it will be fast due to reuse of
hopefully opened before connection to the remote host
It might be worth mentioning "putty-tools" package. Handy for generating those key files for Windows
PuTTy.
i have to admit, instead of a shell script and different symlinks pointing to it (the very last point in the
article), i'd much rather just setup aliases...
Very interesting! For a coincidence I was re-arranging all my notes about ssh I collected during years,
and it's very similar to this article.
Anyway, I'm not shure about the forward of a forwarded port in section 4: probably it misses the "-g"
option, which allows to other hosts different than localhost to access to a forwarded port? Or maybe it's
not needed here, but can be useful for other tricks.
eNjoy
9 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
My thoughts exactly! This has many of my own tips. Here are a few more:
----SERVING SSH SESSIONS:
http://www.etherboot.org/doc/html/sshterminal/t1.html
-- Serving SSH sessions: This document shows how to provide a login session that directly connects
to remote hosts with SSH.
----TRICK FOR SSH-ING TO A MACHINE THAT IS BEHIND A FIREWALL WITHOUT AN OPEN PORT
EXCEPT FOR THE SSH PORT OF ANOTHER MACHINE:
alias a31-from-outside='ssh -f -L2099:192.168.1.210:22 [email protected] sleep 10 & (sleep
5; ssh -p 2099 sgeiger@localhost)'
----HOW TO CONFIGURE OPENSSH TO ALLOW SCP BUT NOT ALLOW LOGINS:
http://www.pdrap.org/code/enable_scp_disable_ssh/
----OFFLINE MAIL:
http://www.miek.nl/linux/postfix.html
- a good SSH approach to handling the issue of writing mail when you are offline...and flushing the
mail queue when you go back online
----FILE TRANSFER TRICKS WITH SSH:
# GET
alicehost$ ssh alice@bobhost "cat file" > file
# PUT
falicehost$ cat file | ssh alice@bobhost "cat > file"
# LIST
alicehost$ ssh alice@bobhost "ls"
# MGET
alicehost$ ssh alice@bobhost "tar -cf - /etc" | tar -xf # RESUME GET
alicehost$ ssh alice@bobhost "tail -c 231244 file" >> file
-- Caution. I'm a little suspicious of this command. Doesn't one have to know the file size in bytes?
and isn't that dependent upon the block size used by the filesystem (meaning it might be different on
the other host) I need to look into that.
### Another variation on a put: transferring a partition to a remote file:
sudo dd if=/dev/hda5 | ssh [email protected] "cat > ns1.ncee.net-deb5"
### AND THEN A WAY TO KEEP THOSE TWO IN SYNC:
### Since /dev/hda5 is treated just as a file, you should be able to use rsync on that single file.
10 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
### Otherwise, if it is mounted, you can mount on both sides and then:
### on remote:
mount -o loop -t ext3 ns1.ncee.net-deb5 /mnt/foobar
### on origin machine:
## sudo rsync -avz /dev/hda5 -e ssh [email protected] /mnt/foobar/
----MUTT AND SSH:
Placing the following commands in the .muttrc file will create and utilize a tunnel for each connection:
From the .muttrc
set tunnel="ssh -q imap.server.com /usr/sbin/imapd"
set preconnect="ssh -f -q -L 1234:imap.server.com:143 imap.server.com sleep 5
-- YOU CAN USE ONE OR THE OTHER OF THESE...
-- with the first of them, you don't even need to hvae an imap server running.
----VPN WITH SSH & PPP:
http://www.faqs.org/docs/Linux-mini/ppp-ssh.html
----DEBUGGING WITH SSHD:
To debug sshd (server), run this as root on the server: "sshd -d -p 2222". Then use "ssh -p 2222
username@servername" on the client.
----KEYCHAIN AND SSH-AGENT:
Don't forget keychain and ssh-agent for holding onto your keys. ;-) A decent alternative to ssh-copy-id
----KDESSH:
kdessh is for running remotely gui programs, I guess. I haven't used it.
----SENDING MAIL FROM AN SSH COMMAND TO A REMOTE HOST:
This should avoid having to set up a local mail transfer agent...and avoids having to deal with the
DNS issues of having a local MTA.
ssh [email protected] 'echo "evdo connection came up again" | mail -s "evdo up"
[email protected]'
11 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
http://www.linuks.mine.nu/windows/sshx.html
- has a link to a very small x.exe
----X11 FORWARDING NOTE:
Use the -C flag with SSH when doing X11 Forwarding. It noticably improve performance of
tunneled X apps.
----HOSTS.ALLOW:
Set "sshd: ALL" in /etc/hosts.allow, or preferably, if you know where you will SSH from, list only
those hosts.
----SSH REMOTE FORWARDING:
http://www.hackinglinuxexposed.com/articles/20030309.html
----QUICK USAGE GUIDE TO AUTOSSH:
It almost doesn't seem right to talk about ssh without talking about autossh.
# You have to have this installed on both sides of the connation.
sudo apt-get install autossh
-- do this on both computers--that is, the local and remote ones (the remote one being a
publically-accessible or other SSH server that can be reached by your local computer)
...on darwin ports: sudo port install autossh
## Do ssh dynamic port forwarding or something else like that:
autossh -M 1300 -D 5333 [email protected]
# Verify that the ports that should be open actually are open:
sgeiger@t42:~\ 17:39:25$ sudo lsof -i :1300
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
ssh 3854 sgeiger 4u IPv4 8024 TCP localhost:wipld (LISTEN)
sgeiger@t42:~\ 17:39:38$ sudo lsof -i :1301
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
autossh 3853 sgeiger 3u IPv4 8018 TCP localhost:1301 (LISTEN)
sgeiger@t42:~\ 17:39:39$
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
ssh 3820 sgeiger 4u IPv4 7426 TCP localhost:wipld (LISTEN)
sgeiger@t42:~\ 17:37:21$ sudo lsof -i :1300
sgeiger@t42:~\ 17:37:42$
You might even have good luck without even using the "-M" option...
So, now I will use this command to do dynamic port forwarding for my gaim IM client to the NY
server.
It seems to work pretty well. If I move between wireless networks, it will log in again to the remote
server. Coupling this with an IM client that will log you in again after being disconnected, you might
never have to log into your private IM server manually again.
12 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
#!/bin/sh
#
# Filename: /usr/bin/hussh
#
# Author: Shane Geiger <[email protected]>
#
# Transfer SSH keys to a remote machine so that you don't have to
# interactively connect to the SSH server, esentially telling it to
# be quiet and simply do what you want it to do. :-) Thus the name.
#
# This script handles not only permissions but also ownership, will
# create the ~/.ssh directory if it doesn't exist, will generate keys for
# you if they don't exist, gives syntax usage info if it is invoked
# incorrectly, and is simple to put onto any machine.
#
# I know of several programs that transfer ssh keys:
# lussh - which is distributed with Lufs ( http://lufs.sourceforge.net/lufs/install.html )
# ssh-copy-id - distributed with openssh nowadays, it appears
# ssh-installkeys - Eric Raymond's, which is found at
http://www.catb.org/~esr/ssh-installkeys/
# hussh - my own, which you can find below.
#
# I like my script better than the others because
# 1) it doesn't have dependencies of any sort (other than /bin/sh), as does ssh-installkeys
# 2) it doesn't require you to enter the password twice (as does lussh)
# 3) you can transfer keys to an ssh server listening on a non-standard port (a failing of perhaps
all the others)
# 4) it balances simplicity with functionality
#
# For more great SSH tricks, see http://www.debian-administration.org/articles/438
#
13 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
Read this article for a great introduction to OpenSSH ControlMaster, which can speed up your
usage of SSH: http://www.linux.com/article.pl?sid=06/05/19/145227
----The above article's commands are summarized here:
# TIME THE CONNECTION. THIS WILL GIVE YOU AN IDEA OF HOW MUCH FASTER
ControlMaster WILL MAKE YOUR CONNECTION
for i in `seq 5`; do time ssh [email protected] "hostname"; done
# NOW ADD THESE THREE LINES TO ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p
# NEXT SET UP THE CONNECTION THAT SUBSEQUENT ACCESSES WILL USE
ssh -M -S ~/.ssh/foo.bar.us [email protected]
# MAKE SURE IT WORKS
ssh -S ~/.ssh/foo.bar.us [email protected]
# NOW TEST AGAIN TO SEE WHAT THE SPEED IS LIKE
for i in `seq 5`; do time ssh -S ~/.ssh/foo.bar.us [email protected] "hostname"; done
Note that X can be very bandwidth greedy. Don't expect visually complex applications to be very
responsive over 256k DSL.
13/10/06 15:30
http://www.debian-administration.org/articles/438
freenx comes to the rescue when doing gui over ssh over adsl...
Also, if servers need to be accessed with a fully qualified name, then you just put
Host red
Hostname red.my.domain.tld
Just realized that ssh ships with a script to do this, called ssh-argv0.
A program called gstm was just added into debian unstable this week, and it looks like a really nice tool!
It allows you to set up ssh port forwarding using a gui, and also store configurations
http://gstm.sourceforge.net/
If you have the latest bashcompletion package installed then tab completion will work both for your
gateway and for the hosts behind the gateway. The last part of the tab completion takes some time
15 of 16
13/10/06 15:30
http://www.debian-administration.org/articles/438
because bash will ssh into the gateway to bing the broadcast and return the available hosts.
Hi,
I have created a tunnel and let someone to connect to Internet through a port. Now, I would like to log
the pages that they visited. What should I do?
Please help.
Thanks
16 of 16
13/10/06 15:30