How SSH key works with a remote machine

The remote machine (the server you are connecting to) contains the public key in the ~/.ssh/authorized_keys file. This file contains a list of public keys that are authorized to connect to the user’s account on that server. Add your publick key after a new line.

The local machine (the machine you are connecting from) contains the corresponding private key. This private key is kept secure and is not shared. When you initiate an SSH connection from your local machine to the remote machine, the private key is used for authentication. The remote machine checks if the public key corresponding to that private key is present in the authorized_keys file, and if so, access is granted.

You can use the SSH-keygen to create the required public and private keys.

For your Local machine (Connecting from machine)

Or Create a config file in the .ssh folder

in Unix: ~/.ssh

in Windows: C:\Users\xxxx\.ssh

Sample config file to connect from :
Save below config file inside the .ssh folder

Host uni-ssh-key
HostName Your Remote IP
User Your user [ubuntu/root]
IdentityFile Path to the private key

By using above, you can just

or else, You can connect directly to the server with following

Goto the .ssh folder and open the Authorized_keys file

Append the .pub file that you created.

Common issues:

  1. All the time when we are login have to run the env “$(ssh_agent -s)” and ssh-add private_key
    • For this go to the vim ~/.bashrc and add the two commands there to execute all the time when login
      • #Start SSH agent
      • eval “$(ssh-agent -s)”
      • # Add your SSH private key to the agent
      • ssh-add ~/.ssh/uni_git
  2. When I execute the Git command, it gives me an error as “error: cannot open .git/FETCH_HEAD: Permission denied”
    • Change the owner ship of the folder
    • sudo chown -R ubuntu:ubuntu /home/projects/betest/Unilever

Leave a Reply

Your email address will not be published. Required fields are marked *

35 + = 41

Related Post