Category Archives: Command Line

Passwordless SSH Login between OS X and Remote Server

terminal-iconIf you commonly ssh from your OS X computer to a remote server such as a web server it makes sense to set up passwordless ssh login so you can have a secure connection without having to always enter your password. Setting up passwordless login also allows you to run automated scripts to help with managing things like offsite backups. Scripts can’t be automated if they require someone to be present at your machine typing in passwords every time they run.

It’s worth noting that you may first want to check if you already have¬†existing ssh keys on your computer. This can be done by entering¬†cd ~/.ssh [enter] at your terminal. If you get a response back stating “No such file or directory” then you need to proceed with the following steps.

To set up passwordless login between a remote unix/linux server and your OS X machine fire up terminal and…

1. generate an RSA private key

ssh-keygen -t rsa -C “your_email@example.com”

2. copy the key to your clipboard

pbcopy < ~/.ssh/id_rsa.pub

3. ssh into the webserver

ssh username@yoursite.com (login with your password for the last time)

4. make a .ssh directory, if it doesn’t already exist

mkdir .ssh

5. create an authorized_keys file and paste in the key from mac that you just copied to your clipboard

vi .ssh/authorized_keys (paste in key from clipboard, save)

6. set permissions for necessary files and directories on webserver

chmod go-w ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

That’s all there is to it. These steps could all be combined into a single line but the above is much easier to follow.

Fix ‘Unkown host’ Error on Mac

Have you ever had one of your websites become inaccessible on only one of your computers? The site loads up fine on every other machine you try it on but on this one machine, nada. If you try to ping the domain you’ll likely be presented with an ‘unknown host’ error.

If your computer is a Mac you will likely first think to try¬†dscacheutil -flushcache . However, this will not work for you unless you are using an older version of OS X. Now what you’ll have to do is the following:

sudo killall -HUP mDNSResponder

In most cases this is what will work for you. I won’t bore you with a lengthy explanation of why. Just try it.

Command Line Tip: Position Cursor with the Mouse

command line tip - place your cursor with the mouse
Place your cursor with the mouse

When entering long commands in the terminal, making changes or fixing mistakes in the middle of the command can slow you down. Moving your cursor over one character at a time to get the to point where the change is needed takes too much time! Fortunately on a Mac you can actually use your mouse to position the cursor wherever you want in the current line.

The trick is to hold down the ‘Option’ key while clicking. Move your mouse pointer where you want the cursor and ‘Option-Click’. When doing this the cursor will actually move to that point in the line. I didn’t believe it until I tried it. It really works! Another command line time saver.

Command Line Tip: Start of Line – End of Line

If you like to work from the command line there are many keyboard shortcuts that can make your life easier. Two of my favourites are control+a and control+e. When you are have some text entered and you want to quickly get to the start or end of the entered text these little shortcuts will save you some key-presses.

ctrl + a: moves the cursor to the start of the line

ctrl + e: moves the cursor to the end of the line

Many of you will already know about these handy shortcuts but if you haven’t I think you’ll really appreciate them.