Before Proceeding β³π If you're interested in learning how to retrieve a username and IP, be sure to check out my first post for a detailed explanation! ππ‘
How to log in to a remote server using SSH with a username and password
1οΈβ£ Start the SSH Server on the Host Machine
π Run the following command on your host machine to start the SSH server:
sudo systemctl start ssh
π To check the status, run:
sudo systemctl status ssh
2οΈβ£ Generate an SSH Key on the Client Machine
β
Before proceeding, check if any key is present in the host machine's .ssh
folder:
cd ~/.ssh
ls
If there are any keys present, like id_rsa
, id_ed25519
, or any other name, choose a different file name unless you want to overwrite it.
π» Now, on your client machine (yes, you heard it right), generate an SSH key pair by running:
sudo ssh-keygen -b 4096
βΉοΈ Note: The
-b
flag specifies the key length in bits (4096 in this case).
β
When prompted for the filename, hit enter to use the default location (~/.ssh/id_rsa
) if no existing keys are present.
For a passphrase, if you donβt want one, hit Enter β π
β οΈ If a file already exists in ~/.ssh/
, it's recommended to specify a custom filename (don't forget to provide the full path, or it will be created in your current directory):
~/.ssh/customFileName
π― Press enter to continue.
3οΈβ£ Copy the Public Key to the Host Machine
π‘ Tip: It doesn't matter where you generate the key. What matters is that the private key should remain on the client machine, and the public key should be on the host machine. If needed, you can also transfer it manually.
π€ To copy the public key to the host machine from the client machine, run:
ssh-copy-id -i ~/.ssh/customFileName.pub username@ip
π This command specifies which public key to send to the host machine. In turn, it will be added to ~/.ssh/authorized_keys
.
π Alternatively, if you want to automatically pick the default public key from ~/.ssh/
, use:
ssh-copy-id username@ip
4οΈβ£ Verify the Key on the Host Machine (Optional)
π οΈ On your host machine, check if the key was copied successfully:
cat ~/.ssh/authorized_keys
The key in this file should be exactly the same as the public key on your client machine. If there are multiple keys, look for the one that matches your public key.
5οΈβ£ Log in to the Host Machine from the Client Machine
π Now, try logging into your host machine from the client machine:
ssh username@ip
π The first attempt will still ask for a password.
π On the second attempt, key-based authentication should work.
β οΈ Troubleshooting: Still Asking for Password?
If SSH still prompts for a password after the first attempt:
1οΈβ£ On your host machine, edit the SSH configuration file:
sudo nvim /etc/ssh/sshd_config
2οΈβ£ π Search for the following settings and update them if necessary:
-
PasswordAuthentication β Change
yes
tono
-
Public Key Authentication β Change
no
toyes
- AuthorizedKeysFile β Uncomment this line
3οΈβ£ πΎ Save and exit (:wq
in nvim
).
4οΈβ£ π Restart the SSH server:
sudo systemctl restart ssh
π You're Done!
β Now, you should be able to log in without a password using SSH key-based authentication. π
Thank you all for taking the time to read this post! π
I truly appreciate your support and hope you found it helpful. π
Top comments (0)