SSH via Mac Terminal – SSH Settings

I’ve been using my Mac for work lately, using the terminal.app to SSH to the servers I need to. What’s been driving me nuts is that I keep getting kicked off of the server every time I’m away from my computer for more than two minutes. Finally, I did some googling. It turns out that the OSX uses OpenSSH, which epically awesome– especially since I already know how to configure OpenSSH. Since I’ve already had to explain this a handful of times, I figured that I should write a post that I can reference.

First, you need to create an SSH config file. When you open the terminal, you should start off in your Home directory. To verify this, check out what directory you’re in:

$ pwd

You should get something like /Users/, where is your username on your computer. Since this is my work profile, mine is /Users/work (I use fast profile switching to keep my work and play profiles separate).

[edit for pro tip]: On your home directory, create a file named .bash_profile and add this line:

complete -W “$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | sed -e s/,.*//g | uniq | grep -v “\[“`;)” ssh

This will allow you to tab complete your known hosts!! (Thanks xPaul)

If you’ve used SSH on your mac before, there is already a directory for your settings. Let’s look for it.

$ ls -la

You need the -a so that you can see all the files in the directory because we’re looking for a hidden folder called .ssh. If it’s there, change to it:

$ cd .ssh

If it’s not there, you need to create it:

$ mkdir .ssh

After you’re in the .ssh directory, there may or may not be a file in there named config (with no extension). Either way, we’re going to edit it with vim. If the file exists, vim will edit it. If it doesn’t exist, then vim will create it, so our command is:

$ vim config

Now, I don’t have the time or the energy to explain how to use vim. There is a great tutorial on that here: vim basics

As I said before, my profile name on my Mac for work is called ‘work’, so every time I try to ssh to a server, it tries to connect as work@server, which irritates the hell out of me. To fix this, the first thing I have in my config file is:

User boogers

This makes it so that when I ssh to a server, it defaults to the user boogers (my username on most of my work servers). The next two lines I have are for getting logged out every time I have to take a leak:

ServerAliveInterval 60
ServerAliveCountMax 3

This makes it so that every 60 seconds, ssh will send a message to the connected server so I don’t get kicked off. WIN.

For a full list of settings, check out the man page for SSH:

$ man ssh_config

I hope someone out there finds this helpful.

One Comment

  1. Posted July 15, 2011 at 7:42 pm | Permalink

    Pro tip: In your home directory, create a file named .bash_profile and add this line:
    complete -W “$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | sed -e s/,.*//g | uniq | grep -v “\[“`;)” ssh
    This will allow you to tab complete your known hosts!! (Thanks xPaul)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

*