-
Notifications
You must be signed in to change notification settings - Fork 1
How To Create SSH Keys
Creating a public and private SSH key pair in Ubuntu is a straightforward process. These keys are used for secure access to remote servers. Below is a step-by-step guide:
-
Open the terminal: You can do this by pressing
Ctrl + Alt + T
. -
Generate the SSH key pair: Run the following command to create a new SSH key pair:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
-
-t rsa
: Specifies the type of key to create, in this case, RSA. -
-b 4096
: Specifies the number of bits in the key, 4096 is recommended for strong security. -
-C "[email protected]"
: Adds a comment to the key, typically your email address, to help identify the key.
-
-
Specify the file to save the key: After running the command, you'll be prompted to choose where to save the key. By default, the key will be saved in the
~/.ssh/id_rsa
file. You can pressEnter
to accept the default location. -
Set a passphrase: You will then be prompted to enter a passphrase for the key. This is optional but recommended for added security. You can leave it empty by pressing
Enter
if you don't want to use a passphrase.
-
List the contents of your
.ssh
directory:ls ~/.ssh
You should see your private key (
id_rsa
) and public key (id_rsa.pub
). -
View the public key: If you want to copy your public key to use on a remote server, you can view it with the following command:
cat ~/.ssh/id_rsa.pub
This will display your public key, which you can then copy.
If you created a passphrase for your key, you'll want to add your key to the SSH agent to avoid having to enter the passphrase every time.
-
Start the SSH agent:
eval "$(ssh-agent -s)"
-
Add your private key to the SSH agent:
ssh-add ~/.ssh/id_rsa
To use your SSH key for authenticating with a remote server, you need to add the public key to the server's authorized_keys
file.
-
Copy the public key to the remote server using
ssh-copy-id
:ssh-copy-id username@remote_host
Replace
username
with your username on the remote server andremote_host
with the server’s IP address or hostname. -
Manually copy the public key (if
ssh-copy-id
is not available):- Log in to the remote server.
- Append your public key to the
~/.ssh/authorized_keys
file on the remote server:echo "your-public-key-content" >> ~/.ssh/authorized_keys
- Ensure the permissions on the
authorized_keys
file are correct:chmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh
Now you can connect to the remote server using SSH without being prompted for a password (if no passphrase was set):
ssh username@remote_host
This process secures your SSH access to remote servers by using a cryptographic key pair rather than a password.