SSH Tricks
serversforhackers.com
serversforhackers.com
i.e. we intentionally do not enable (human) ssh access to the production hosts. In an autoscaling AWS world, logging into an individual machine by hand is the last thing you want to be doing. So we are learning the (sometime difficult!) lessons of how to rely only on what our logging, tracing, monitoring, and deployment automation (including snapshotting) can afford us. I suspect sooner or later we will break down and swap in a login-enabled image to diagnose some sticky problem, but -- as much as I resented the idea when he presented it -- it's an interesting discipline.
Anyone else living by that principle?
I'd love to hear any instances where you have SSH'ed in, or what kind of bugs would prompt you to want to.
Maybe because you didn't have some monitoring on a specific aspect of the server, or an application bug where you needed to use strace - something like that?
Host hosta hostb hostc
User usera
Host *
User userb
Since my local system username was different than many of the remote systems I could wildcard to a different default username then had a list of servers that would use my other username.The bad thing is, as the blog post shows, "Host" above is really just an alias. So if I have an entry like:
Host hosta.mycompany.com
User usera
and then try to do "ssh hosta", even if hosta resolves to hosta.mycompany.com, it won't match the config entry, as config entry data is all used prior to DNS lookup.EDIT: thanks for all the suggestions below.
Host hosta hostb hostc
HostName %h.mycompany.com
User usera
I believe this isn't a super-new feature -- every ssh version I've run into client-side in the last couple years has supported it. Host hosta* hostb* hostc*
User usera
Assuming you don't have two different machines named hosta with different domain names, it should do what you want. Host hosta.mycompany.com
User usera
and then type "ssh hosta<tab>" :)http://undeadly.org/cgi?action=article&sid=20070925181947
Combines well with aliases - prefix all hosts with a common name and use "Host prefix-*" to setup the ProxyCommand.
ControlMaster auto
ControlPath /tmp/%r@%h:%p
ControlPersist yes
Having said that, sometimes I need to remove the entry from /tmp to reconnect if my network settings have changed. > Having said that, sometimes I need to remove the entry
> from /tmp to reconnect if my network settings have changed.
You can also just send the exit command to the conn. ssh somehost -O exit
In addition, I put my controlpath in my .ssh dir. Keeps it out of the global /tmp dir. shrug controlpath ~/.ssh/cp-%r:%h:%pman pages are nice references, but in a lot of cases they seem to be kind of obtuse. They'll have a short technically correct description of every individual flag, but not really what you'd use the flag for, or how it combines with other flags.
Most man pages need a lot more examples. I like blog posts like this, which are pretty example-driven. They lay out an actual use case, and then walk through the flags necessary to achieve that goal. A man page starts with the flags, and assumes you'll know when and why to use them.
I'm not necessarily defending this particular post, just making a general point about the more esoteric features of something like ssh :)
Actually apparently it doesn't have a man page, I had to use less --help.
Anyways, it turns out there's a ton of functionality I never thought to look for. I only ever see "| less" and that's all it ever was to me. There's nothing more I really expected out of it than that, really.
But that's just how it goes, for the most part. You learn the basics about how to use a given tool, enough to serve the purpose you originally sought it out for, and then that's it, everything else is just noise. less lets me easily scroll through whatever output I pipe to it, ssh gets me onto another server, what more would I need or expect?
But then an article like this comes along and prompts me to look more closely at something I had been taking for granted, showing it to be much more versatile than I thought.
Personally my favorite part is the logical the tie-in to Ansible, which I want to expand upon in a near-future edition.
Or use the same syntax to build a playbook that you can run to manage infrastructure with the `ansible-playbook` command. Since Ansible uses SSH as it's transport (in most cases—you can do it other ways), if you can connect to a server via SSH (and who can't?), you can have it completely managed/version-controlled pretty simply.
scp vps:~/www/back<tab>
and it'll autocomplete to scp vps:/home/vhosting/c/vhost12345/www/backup-2014-07-02.tar.gz
Or if there are multiple matches for file or directory names, it'll list them like bash normally would. This seamless integration is so awesome, I can highly recommend ssh keys (and cygwin for Windows users).Not on this web page???
-D #### (Whatever port not being used) and then I just use a proxy extension on my web client and instant privacy.
chmod u=rw,go-rwx /path/to/lol
It creates a virtual network interface and allows for "real" tunneling, which is pretty cool.
No, that's not how it's supposed to work. Ideally, one key per machine per user.
It doesn't really matter how many you have, you still need to protect them. Encrypt your laptop, lock your screen when you get up for a break, etc, etc.
Use IdentityFile to specify which key to use with which remote host and you're golden.
edit: I also have that private SSH key on my personal computers so I can use SSH when working from home. Then when I stop working with that company I can simply remove that key, rather than generating a new SSH key and redistributing public keys to hosts that I use regularly.
edit edit: Using IdentityFile also helps automate the process of redistributing keys when you decide to generate a new one.