News for hackers. Stuff that matters.
Re:SSH Kung Fu by patio11 on Monday April 28, 2014 @03:38AM (#7658691)
A trick I learned recently: create .ssh/config

File format: as many of the following blocks as you like

  Host $ALIAS <-- whatever you want here
  Hostname www.example.com
  User someuser
  Port 1234
You can now ssh to that server as that user by doing "ssh $ALIAS" on the command line, without needing to specify the port or user with the usual command line arguments, or necessarily spell out the entire host name.
Re:SSH Kung Fu by hf on Monday April 28, 2014 @03:56AM (#7658742)
What is more, you can specify an abstraction for the tedious double-ssh where you first connect to some internet-facing host in order to gain access to an internal machine:

    Host $ALIAS
        User $USER
        HostName $INTERNAL
        ProxyCommand ssh $USER2@$PUBLIC -W %h:%p
Now

    laptop> ssh [email protected]
    public> ssh dev@myworkstation
becomes

    laptop> ssh work
(I just realized that this slightly confused article seems to accomplish the same by using a convoluted setup of port-forwardings and netcat.)
Re:SSH Kung Fu by Erwin on Monday April 28, 2014 @05:50AM (#7659013)
The ssh -W option -- which replaces netcat -- is relatively new. E.g. Redhat 5.x did not have it, nor did Ubuntu 10.04 LTS. Until OpenSSH 5.4 netcat was the way to do this sort of proxying.
Re:SSH Kung Fu by stock_toaster on Monday April 28, 2014 @06:14AM (#7659053)
I ran into an issue[1] with the combination of -W and control persist -- using openssh versions < 6.0.

netcat worked fine.

  [1]: https://news.ycombinator.com/item?id=4678117
Re:SSH Kung Fu by sampo on Monday April 28, 2014 @06:10AM (#7659047)
And if your router keeps dropping idle connections, add something like:

    ServerAliveInterval 240
    ServerAliveCountMax 5
Re:SSH Kung Fu by voltagex_ on Monday April 28, 2014 @07:28AM (#7659241)
Can help on mobile (3G/4G) connections too.
Re:SSH Kung Fu by sampo on Monday April 28, 2014 @06:16AM (#7659058)
> (I just realized that this slightly confused article seems to accomplish the same by using a convoluted setup of port-forwardings and netcat.)

Yeah, the article sets separately first

    Host bar
      ...
and then

    Host behind.bar
      ...
But it can also be done by just one step:

    host behindbar
      User         <user-behindbar>
      Hostname     behindbar.domain
      ProxyCommand ssh <user-bar>@bar.domain nc %h %p 2> /dev/null
Re:SSH Kung Fu by michaelmior on Monday April 28, 2014 @09:10AM (#7659622)
I use autossh[1] to keep a tunnel open in the background to $PUBLIC which lets me connect faster to $INTERNAL.

[1] http://www.harding.motd.ca/autossh/

Re:SSH Kung Fu by mpapi on Monday April 28, 2014 @09:40AM (#7659835)
A favorite .ssh/config feature of mine is pattern matching on hostnames with "?" and "*". So you can say something like:

    Host bos-??
    HostName %h.mydomain.com
    IdentityFile ~/.ssh/my-boston-key

    Host nyc-??
    HostName %h.mydomain2.com
    IdentityFile ~/.ssh/my-nyc-key
and log in with e.g. "ssh bos-14".
Re:SSH Kung Fu by gatehouse on Monday April 28, 2014 @12:54PM (#7661172)
Yeah, I use a similar thing for ec2:

    Host *.amazonaws.com
      User ec2-user
      IdentityFile ...
And then it is just

    ssh ec2-X-X-X-X.compute-1.amazonaws.com
Re:SSH Kung Fu by voltagex_ on Monday April 28, 2014 @08:10PM (#7664006)
This seems to be relatively new. It doesn't work on a couple of boxes I tried.

Thanks though, I didn't know about the ?? syntax.

Re:SSH Kung Fu by mpapi on Monday April 28, 2014 @09:12PM (#7664260)
I found references to it going back to 2008, and the git repo that has my dotfiles says I've been using it (in Linux) for 3 or so years. Maybe it depends on the OS/distro.

The patterns are similar to shell globs: * matches zero or more characters, ? matches exactly one.

Re:SSH Kung Fu by yen223 on Monday April 28, 2014 @05:11AM (#7658938)
This is excellent advice. The best part is that you can use the same $ALIAS for tools built on SSH, including scp and rsync, like this:

    scp $ALIAS:/var/log/mylogs/logfile ~/backups/logs/
Re:SSH Kung Fu by sampo on Monday April 28, 2014 @06:06AM (#7659039)
The aliases also work when mounting filesystems over the network in the File Browser.

Nautilus / Connect to Server / Server Address: work/home/user

(here 'work' is the alias for the work computer)

Re:SSH Kung Fu by mverwijs on Monday April 28, 2014 @05:35AM (#7658983)
And.... Ansible uses those aliases too, making it trivial to maintain *NIX server that are behind firewalls and/or vpns.
Re:SSH Kung Fu by nnnnni on Monday April 28, 2014 @06:23AM (#7659083)
Well, that sure beats my .bashrc that's full of alias "servername"="ssh user@servername -p portnum"...
Re:SSH Kung Fu by m1crofarmer on Monday April 28, 2014 @04:44PM (#7662886)
What's more, you can specify as many Host aliases (on one line) as you want (with wildcards):

    Host 192.168.* *.foo.*.com *.bar.net
Re:SSH Kung Fu by deanclatworthy on Monday April 28, 2014 @03:49AM (#7658722)
I've been doing this for a while now, but my file is now huge and it's cumbersome to edit. Is there no utility to mange that file?
Re:SSH Kung Fu by datr on Monday April 28, 2014 @04:22AM (#7658807)
I've worked around this by creating a ~/.ssh/config.d directory and splitting my configuration out into multiple files (normally by project). I then use dotdee[1] to watch that directory and automatically rebuild ~/.ssh/config anytime there is a change.

[1] https://launchpad.net/dotdee

Re:SSH Kung Fu by pmh on Monday April 28, 2014 @04:18AM (#7658796)
I don't know of any, but one thing you can do is split up your config into multiple files and then use `cat` to combine them after making a change. There's also https://github.com/markhellewell/sshconfigfs
Re:SSH Kung Fu by batuhanicoz on Monday April 28, 2014 @04:43AM (#7658872)
I think storm[0] is what you are looking for.

[0] https://github.com/emre/storm

Re:SSH Kung Fu by SnacksOnAPlane on Monday April 28, 2014 @10:56AM (#7660319)
Here's what I put in my .bashrc:

  alias compile-ssh-config='echo -n > ~/.ssh/config && cat ~/.ssh/*.config > ~/.ssh/config'
  alias ssh='compile-ssh-config && ssh'
Compiles all your ~/.ssh/*.config files into a single ssh config file. It's simple and stupid and seems to do the trick.
Re:SSH Kung Fu by lexandstuff on Tuesday April 29, 2014 @12:03AM (#7664873)
Aside from the ones already mentioned, there's a library called dot-ssh-config[1] that is useful for generating SSH configs.

[1] https://github.com/aelse/dot-ssh-config

Re:SSH Kung Fu by hibbelig on Tuesday April 29, 2014 @12:26PM (#7668047)
Sadly, this doesn't work always. Some apps which implement their own ssh don't support ~/.ssh/config. For example, the OSX Subversion GUI client Cornerstone doesn't support this.
Re:SSH Kung Fu by opendais on Monday April 28, 2014 @10:48AM (#7660254)
I'm confused how this is better than just using an alias/profile?

Maybe it is just me, but I prefer to dump all my custom commands and aliases into .zshrc so they are easy to backup/track/find.

Re:SSH Kung Fu by mturmon on Monday April 28, 2014 @02:15PM (#7661842)
You have been down voted, but the question is reasonable.

One reason is so that invocations of ssh outside of the context of user invocation of ssh at the command line will also have these customizations included. This is especially important for ssh, which has emerged as a main security interoperability tool for Unix systems.

For example, if you use rsync, the tunneling and host alias conventions you set up in .ssh/ will carry over transparently to the ssh tunnel used by rsync.

Another example would be invocations of ssh in scripts (sh/bash scripts, even) that will not or might not read your .zshrc.

Re:SSH Kung Fu by opendais on Monday April 28, 2014 @05:49PM (#7663338)
Ya, I think the underlying issue is I do things very differently than people on HN.

The idea of creating dependencies on a configuration profile inside a bash script is the exact opposite of what I would do.

I also could not rsync things to my local machine [bandwidth constraints] and would be rsyncing between remote machines, which being a shared environment, I would rely on explicit invocations instead of creating configurations/aliases.

Thank you for telling me how/why other people make different choices. I always do seem to have the blinders of my process is the only process I consider when commenting on HN. :)

Re:SSH Kung Fu by wilmoore on Monday April 28, 2014 @05:26PM (#7663193)
To add to what @mturmon said; you'll want to keep in mind that even GUI tools (i.e. consider your favorite database tool that supports SSH connection) that support SSH connection will pick up this configuration so you don't have to manually plug in all of the pieces for each session.

Just specify the alias host name as configured in ~/.ssh/config and the user, identify file, and anything else you put there will be used as set.

Re:SSH Kung Fu by opendais on Monday April 28, 2014 @05:40PM (#7663287)
This never occurred to me...I don't use GUI tools for database management outside of a local development environment or diagram generation.