ssh public key code example

Example 1: get public ssh key

ssh-keygen
cat ~/.ssh/id_rsa.pub

Example 2: ssh - use public key to login

# generate ssh key first on your source host:
ssh-keygen -t rsa

# copy it to the remote/target host:
ssh-copy-id remote_username@server_ip_addressCopy

# if for some reason the above method unavailable, use:
cat ~/.ssh/id_rsa.pub | ssh remote_username@server_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Example 3: what is role of public key in ssh

A public key that is copied to the SSH server(s). 
Anyone with a copy of the public key can encrypt data which 
can then only be read by the person who holds the corresponding private
key.

Tags:

Misc Example