Slapping the keyboard until something good happens

Creating an SSH Key on macOS for Automatic Login to Linux

Lately I have been working a lot in a terminal, specifically with Linux VMs in my home lab environment. Logging into multiple VMs over and over again over SSH has become fairly repetitive, that is until I created myself an SSH key for automatic login to my VMs.

To make things easier in the future when it comes to logging in via Terminal, you can setup SSH keys, so you won’t need a password when you login.

In my lab I have been primarily working with “minimal” installations of CentOS 6 and CentOS 7. When you do a “minimal” install of CentOS, the installation doesn’t install services like rsync and scp by default.

To install rsync and scp run the following on your CentOS client:

1

sudo yum-yinstall openssh-clients rsync

While still connected to my CentOS client, I created a directory to store my RSA keys with the following command:

1

mkdir-p/root/.ssh

With the Terminal open on your macOS system, enter the following command to generate an RSA key for login:

1

ssh-keygen-trsa

Press enter three times to accept the default settings.

On my macOS client, I like to tighten up the file system permission via the following example:

1

2

chmod700~/.ssh

chmod600~/.ssh/*

To verify our permissions we can run:

1

ls-ld~/.ssh&ls-l~/.ssh

On my macOS client, I see the following:

Now that we have a key, the next thing to we need to do is copy the key to a directory on the server we intend to SSH to via Terminal. This will allow for password-less logins. With the IP or DNS name of the server we intend to connect to in mind, we can enter a command similar to the following to copy our key to the CentOS VM:

1

scp~/.ssh/id_rsa.pub root@CentOS6:/root/.ssh/authorized_keys

Like my macOS client where we created the RSA key, I will tighten up my CentOS VM SSH directories with the following commands from my macOS Terminal:

1

2

ssh root@CentOS6'chmod 700 /root/.ssh'

ssh root@CentOS6'chmod 600 /root/.ssh/*'

We should now be able to SSH from our macOS client to our CentOS VM without providing a password!

Brad Stevens

Brad Stevens is an enterprise consultant, cloud architect, and technical evangelist with over 5 years of experience providing architecture, development, consultancy and design expertise. He works at CDW, a leading re-seller of IT hardware and software and professional services solution delivery. He is based in Portland, Oregon.