Linux Neatly Manage SSH Sessions Like MRemoteNG on Windows

SSH technically works the same anywhere, but on Windows without special tooling, using SSH can be cumbersome as the CLI is not very… elegant?  Generally, on Windows, people use tools like putty or MRemoteNG (also putty based) to make things neater.

If you’re on Linux, you can use SSH out of the box very cleanly by leveraging the config file to create named hosts.

For example, this verbose command:

ssh -i ~/.ssh/prod-key.pem centos@10.20.30.40

Can be replaced by the following command – and the nice “pgadmin-prod” name even auto-completes.  So, if you have 100 servers for different things, you can still find them easily.

ssh pgadmin-prod

This works if you add the following block into your ~/.ssh/config file.

Host pgadmin-prod
User centos
HostName 10.20.30.40
IdentityFile ~/.ssh/prod-key.pem

Where:

  • Host = the user-friendly name I want to use for this connection.
  • User = the user I normally have to log in as.
  • HostName = the IP address of the target host.
  • IdentityFile = the path to the private key used for the connection.

You can have as many named hosts as you like and, again, their names will auto complete after the ssh command – so they’re very easy to use.

This gets even more effective if you combine it with a screen/session management app like tmux or screen.  So, I recommend you look into those too if you don’t use them already (tmux is a little more modern if you’re a new to both).

I hope this helps you be a little more efficient, and thanks for reading.

Docker – Map IP to DNS, or Just Override DNS Name in Linux in General

I was reading a docker script someone else created and came across an interesting blog explaining a parameter it used (–add-host) right here.  I recommend reading that longer blog, but I’m recording the short notes and link here for myself as I’m sure I’ll be using this a lot.

The “–add-host” Parameter

Long story short, you can just add “–add-host=some_dns_name:some_ip_address” to your docker command in order to make your container have any DNS name resolve to any IP address.

This works by having the container put an entry for this DNS/IP pair into the /etc/hosts file.

Use Outside of Docker

I haven’t used the /etc/hosts file in a while.  But this reminded me about it.  The article points out that you can either add a DNS mapping or even override an existing DNS mapping using this file.

So, for example, I could make google.com point at this website from within the given OS instance, if I updated that file properly.

Pretty cool and useful :).

SCP/SSH With Different Private Key

If you need to use SSH or SCP with a different private key file, just specify it with -i.  For example, to copy logs from a remote server using a specific private key file and user, do the following:

scp -i C:\Users\[your-user]\.ssh\pk_file [user]@[ip-addr]:/path/logs/* .

This -i will work regardless of OS, but the example is SSHing to a Linux server from a Windows server assuming you store your private keys in your user .ssh directory.

Bash – Grep (or Run Other Command) Only On Files Created This Week, Day.

Use Case

I just ran into a simple problem where I had to grep files on a server, but the directory had TONS and TONS of files in it.  I just wanted to target files created within the last week or so.

Working Command

It turns out this find command is very handy for this occasion.  It was taken and lightly modified from this unix stack-exchange post after a fair bit of searching.

find . -mtime -7 -exec grep "my_search_string" {} \;

Basically, it finds everything in “.” (the current directory) that was created in the last 7 days (as in 24 hour days, not from-this-morning days), and it executes the grep expression on it.

You can modify the timing however you want with mtime as well as change the target directory or command to execute, and of course you can pipe the output to whatever you want :).