add new ssh key code example
Example 1: ssh add new key
$ eval "$(ssh-agent -s)"
> Agent pid 59566
Example 2: ssh generate key
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
Example 3: create ssh key mac github
$ ssh-keygen -t ed25519 -C "[email protected]"
Example 4: add ssh key to server
Generate ssh keys (Skip this step if you already have one).
To generate new RSA keys, enter this on the command line.
ssh-keygen -t -rsa
Follow the prompts. You do not need to enter a file name but you should enter a password to make your key more secure. The default file name is id_rsa.pub . By default the key file is saved at ~/.ssh/id_rsa.pub .
Copy your public keys to the remote server.
scp ~/.ssh/id_rsa.pub user@your_ip:/
Add your public key to the authorized keys file on remote server
To add you keys to the file, you can use the following command:
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
If you do not have the folder ~/.ssh/authorized_keys , you can create this with the following commands:
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
Restart server for key changes to take effect
Example 5: Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
$ cat ~/.ssh/id_rsa.pub
Example 6: create ssh keys
cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"