Skip to content
lanzai.co
Go back

SSH Key Setup and Passwordless Login

Using SSH keys instead of passwords is both more secure and passwordless.

Generate keys

ssh-keygen -t ed25519 -C "you@example.com"
# choose a path and passphrase as prompted; press Enter for defaults

Two files are created:

Upload the public key

ssh-copy-id user@server    # the easiest way

Manual method:

cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Adjust server config

/etc/ssh/sshd_config:

PasswordAuthentication no
PubkeyAuthentication yes
sudo systemctl reload sshd

Only disable password auth after key login is confirmed working, or you may lock yourself out.

Configure the config file

~/.ssh/config supports aliases and multiple servers:

Host web1
    HostName 123.45.67.89
    User ubuntu
    IdentityFile ~/.ssh/id_ed25519
    Port 22

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github

Now ssh web1 just works.

Passwordless git push

git remote set-url origin git@github.com:user/repo.git
git push

Port forwarding

Local forwarding (map a remote service to a local port):

ssh -L 8080:localhost:8080 user@server

Remote forwarding:

ssh -R 8080:localhost:3000 user@server

Security tips


Share this post:

Previous Post
Linux Server Troubleshooting
Next Post
Nginx Commands and Config Cheat Sheet