alan

ssh

Every day now and then, I get an scp command wrong. Scp is designed after commands like rcp and works totally fine for local-to-local file copy.

While this can (or could) be useful in some contexts, It's not what I like to do these days; very often, if either hosts is not remote, it's a typo on my part, and results in spurious “somebody@host.example.com” files somewhere in various directories on my disk.

So, what I like to do? I put a small script like this in my path before the actual scp executable in order to pre-check arguments, and prevent accidental local-only copies.

#!/bin/bash
ORIG="/usr/bin/scp"

for var in "$@"
do
    if [[ $var = *":"* ]]; then
        $ORIG "$@"
        exit $?
    fi
done

echo "ERROR: Missing colon. You need to pass at least one remote host specifier in source or dest"
exit 1

#MostlyUnixish #ssh #scp

This is another old trick, but I like to tell the world about such amenities.

I often find people writing command lines such as:

ssh arcane-system-user@someextremelylong.andimpossibiletotype.anderrorprone.hostname.com

or, worse

ssh idliketorememberwhichuser@10.20.30.40

or maybe it was

ssh someuserimashamedof@1.2.3.4

yes, sometimes ssh is simply tedious.

But you can do better. In fact, you can do much better.

your ~/.ssh/config or /etc/ssh/ssh_config can contain Host configuration sections; I have already blogged about such directives when I told the world about stopping key verification compliants – they're very flexible, indeed.

Consider adding Host directives in your configuration for your most used services:

Host myniceserver
    Hostname myniceserver.mylove.com
    Port 12345
    User teddybear

And then you can just

ssh myniceserver

You can override most options on a per-server basis and/or select a certain private key for connections.

#MostlyUnixish #linux #ssh #ancientwisdom

  • UPDATE: modern ssh-copy-id often support a -p parameter for the port. So this trick is unnecessary and won't probably work.

ssh-copy-id is a cool script which is usually distributed along openssh client nowadays; it performs an automatic install of your local public SSH key to a remote server. Its syntax is simply:

ssh-copy-id [-i [identity_file]] [user@]machine

You may have spotted there's no builtin way of telling it what's the port of the remote server; a workaround exists which leverages the way arguments are passed to the script (it's a simple bash file, take a look at it. You can find various versions around the internet).

Just do this:

ssh-copy-id '-p [port_number] [-i [identity_file]] [user@]machine'

And enjoy your newly-added SSH key!

#MostlyUnixish #ssh

If you develop enough software, most probably you'll have a test/development network at your service.

In such networks, which are usually handled through a virtualization infrastructure, machines come and go very quickly. But ssh clients are usually unhappy about that:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
The authenticity of host '192.168.0.208 (192.168.0.208)' can't be established.
RSA key fingerprint is 31:4d:8b:97:c8:57:04:85:6a:1b:72:54:46:ab:04:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.208' (RSA) to the list of known hosts.
root@192.168.0.208's password: 

The first time you connect, you're asked if the fingerprint is correct. The second time you'll just be allowed to connect:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
root@192.168.0.208's password: 

What then if the machine at such ip address changes, because it's rebuilt or modified and the ssh host key is not retained?

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
7b:42:03:5f:5c:8d:5a:2b:6a:a4:1e:d6:0d:05:de:38.
Please contact your system administrator.
Add correct host key in /Users/alan/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/alan/.ssh/known_hosts:111
  remove with: ssh-keygen -f "/Users/alan/.ssh/known_hosts" -R 192.168.0.208
RSA host key for 192.168.0.208 has changed and you have requested strict checking.
Host key verification failed.

From the point of view of openssh this may be a serious security breach, and you should fear such message if it happens in the wild if you haven't touched a server - a MitM attack is probably going on!

What about your local network? ssh-keygen -R 192.168.0.208 will solve that, but you probably didn't need host key authentication since the beginning.

So, just add this:

Host 192.168.0.*
    CheckHostIP no
    UserKnownHostsFile /dev/null
    StrictHostKeyChecking no

at the beginning of your /etc/ssh/ssh_config file, and feel good.

Now, anytime you try connecting inside your network you'll be greeted by this:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
Warning: Permanently added '192.168.0.208' (RSA) to the list of known hosts.
root@192.168.0.208's password: 

No confirmations whatsoever.

#MostlyUnixish #linux #ssh

Bash builtin disown is a not-so-well known [job control command] http://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html), and yet it's an extremely useful one.

So what? You've started a remote ssh session, launched some command which is taking very, very long to complete, and you just want to go home, or maybe you feel your connectivity may drop, and so on... and you forgot to launch screen (possibly with [byobu] https://launchpad.net/byobu)) or tmux before starting your work!

Now you're desperate and you'd just like to go home, but you fear your longstanding jobs will quit if you close your terminal.

Just background your job, launch disown and all the jobs in background will receive no notification whatsoever of your saying goodbye to them; of course you won't be able to read their output to the terminal.

#MostlyUnixish #linux #bash #ssh