Step 0: Create an ssh key pair.
Using ssh-keygen -t rsa -b 4096
Enter the name of the key, and a passphrase if pleased to [ Although, if passphrase is set, you need to enter it at every usage of this key]
Private key stays with you, and you will use it when accessing the server.
The public key is instated in the server’s .ssh folder.
Notes:
- The public key structure:
<key-type> <base64-encoded-key> <optional-comment> - The optional comment is auto added as
username@host. Don’t panic if it’s specific to a host or a user, the key pair still works, and it’s just a comment.
-v verbose and -vv more verbose options are your best friends in analyzing the situation.
Step 1: Pre-requisites
- Ensure Open SSH/SSH agent is running.
- As mentioned above, private key is used from the client side in the connection and public key is used to check against the provided key at the server end.
- SSH has a permission requirement, the user should only have read-only permission and no one else should have any permission on the key files being used.
- So, in client side, we’ll lock down the private key and,
- on the server side, we should lock down the public key.
Step 2: Server side configuration:
- Once you have your public key at the server: [You can copy it from client, or generate them both in server and send both to client, whatever way works.]
- Place it in
~/.sshfolder and copy the public key file into authorized_keys file usingcat your_public_key.pub >> authorized_keys - Once that is done, check if
/.sshhas the permissions as700, if notchmod 700 ~/.ssh - And ensure
authorized_keysalso has600for permissions, else,chmod 600 ~/.ssh/authorized_keys
Step 3: Client side configuration [This one is specific to Windows, but the process stays the same for linux as well]:
Once you have your key pair at the client machine. [Same applies as mentioned in the server above.]
Place them both in
~/.sshfolder.Create/edit file named
~/.ssh/config, update the contents as follows. Replace as required.Host *.cs.purdue.edu # * is the wild card, it resolves for all the subdomains of cs.purdue.edu # For example, the rule is resolved for pete12@mc18.cs.purdue.edu User pete12 # the username which you'd authenticate to the server. IdentityFile C:\Users\pete\.ssh\cs_purdue_ssh_key # The private key location # --- OR --- Host mcxx.cs.purdue.edu HostName mcxx.cs.purdue.edu # If wanted to target a specific host name. User pete12 IdentityFile C:\Users\pete\.ssh\cs_purdue_ssh_keySimilar to the server side, make sure only the current user has permission to the .ssh directory and the private key’s readonly. Use the below commands to make sure it happens:
icacls C:\Users\pete\.ssh\id_rsa /inheritance:r icacls C:\Users\pete\.ssh\id_rsa /grant:r YourUser:(R,W)
You can also make sure this is the permissions structure:
!image.png
Tested and verified by past me.
- That’s it, verify if it works. If not, as said already.
-vand-vvare your best friends.