Connect via SSH without a password from MacOS to Linux with an SSHKEY

Don't panic , it is really easy.

Your Steps will be:
1. Create a key on your Mac
2. Define your key on remote Linux
3. Connect via ssh.

1.  Create the key on your Mac

Open the terminal :)

First make a directory named .ssh on your home directory (mkdir .ssh) If it exist , no problem. Go to the newly created .ssh directory with command cd .ssh

Then, you need to create a ssh key on your MAC.

There will be 2 files for key. If you dont want any passwords , just press enter when passphrase asked.

Filename will be default id_rsa if you just hit enter. But if it already exists , you may want to choose another name.

Hakos-MacBook-Pro:.ssh hakan$ ssh-keygen -t rsa -C ""
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/hako/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/hako/.ssh/id_rsa.
Your public key has been saved in /Users/hako/.ssh/
The key fingerprint is:
The key's randomart image is:
+--[ RSA 2048]----+
|      ..   .     |
|      o.. o      |
|       o.o       |
|      o.o        |
|      .oSo       |
|     . oo=       |
|      *.* .      |
|      Eo O .     |
|      . o.=      |
Hakos-MacBook-Pro:.ssh hako$ 

There should be 2 files , id_rsa(your private key) and your public key to share with remote hosts)

Then you need to this key to your keystore: (make sure you are on the .ssh directory)

Hakos-MacBook-Pro:.ssh hako$ ssh-add id_rsa

That's it. Now you need to copy the contents of your public-key , do not share your private key. It should always be on your computer. 

Hakos-MacBook-Pro:.ssh hako$ cat
ssh-rsa BCAAB3NzaC1yc2EAAAADAQABAAABAQDDQ9NsOquCHBXm+gHl4k4VwA0xmChG0mQhYQPUJrugyNzeYbcwzjWKtCKZiIufRhXP36hHXqxhA1b8adv9AQaFYdNJBA93Dpr9gxxNOKc3Wv1o/WtfEgdC9MflynPcIbliWOc62zZP/RhCeQN+Ucp+H6briI3xTaVzWbJwaP7WbsgCdwKIrRBFtcGERFYs440IJWdYFi5ZeNd20+WrgIRKTMqljAIEhbww7EWE5v3lrbo/bwxr1pNWfYAckH1u4+IXKU91xvhypSsFC1EY8Ef2PtFO7wsBHEfLM4MEBF8o6mPxKTgsOCz2TgYHjJ6RBe3dyCWH4nrQVwdkQgi75J8p

copy all of the gibberish above. Now, you define your key in remote Linux

2. Define The key on Linux

go to .ssh directory from your home directory.

be sure there is a file named authorized_keys

If the file is write-protected, open the write permission:
$ chmod 600 authorized_keys

Then, paste your public key content and press ctrl-D
$ cat > authorized_keys

and make the file write protected.

$ chmod 400 authorized_keys

You are done on the Linux, you can connect from Mac via ssh now.

3. Connect

Hakos-MacBook-Pro:.ssh hako$ ssh your_remote_user@remoteIP

You should connect without a password now. 

For easy connecting, You may want to add the ssh command as an alias.

On your home directory :

Hakos-MacBook-Pro:.ssh hako$ nano .bash_profile

add the line
alias myssh='ssh your_remote_user@remoteIP'

kntrl-X and save the file.

Hakos-MacBook-Pro:.ssh source .bash_profile
Hakos-MacBook-Pro:.ssh myssh

That's it ! :)


Popular posts from this blog

Get Remote IP Address In Axis SOAP Server