Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active May 4, 2024 13:52
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save slowkow/8798394 to your computer and use it in GitHub Desktop.
Save slowkow/8798394 to your computer and use it in GitHub Desktop.
ssh to a server without typing your password

How to ssh to a remote server without typing your password

Save yourself a few keystrokes. Follow the steps below:

  1. Run this Bash script on your laptop:

    #!/usr/bin/env bash
    
    # The hostname of your remote server.
    host=myserver.com
    
    # Create this folder if it does not exist: ~/.ssh
    mkdir ~/.ssh
    
    # Set the correct permissions (required)
    chmod 700 ~/.ssh
    
    # Generate an RSA key pair for identification with the remote server
    ssh-keygen -t rsa
    
    # Copy your public key to the remote server
    cat ~/.ssh/id_rsa.pub | ssh $host 'cat >> ~/.ssh/authorized_keys'
    
    # ssh is very strict about correct permissions
    ssh $host 'chmod g-w,o-w ~; chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys'

    At this point, you can connect to your server without typing a password:

    ssh username@myserver.com
  2. If you would prefer to type jupiter (7 characters) rather than username@myserver.com (21 characters), create a config file ~/.ssh/config on your laptop, as shown below. You'll be able to use the jupiter alias with: rsync, scp, and ssh.

    # ~/.ssh/config
    
    Host jupiter
        User carl
        HostName myserver.com
    

    At this point, you can connect to your server like this:

    ssh jupiter
  3. If you want a single command to do two steps:

    1. First, connect to a login server jupiter
    2. Next, connect to a work server saturn

    Then configure your ~/.ssh/config file as follows:

    # ~/.ssh/config
    
    Host jupiter
        User carl
        Hostname myserver.com
    
    Host saturn
        User carl
        ProxyCommand ssh -qX jupiter nc %h %p
    

    At this point, you can connect to saturn via jupiter like this:

    ssh saturn

Further reading

How to write your own config file:

All available configuration options:

@mbaev
Copy link

mbaev commented May 21, 2019

You haven't explain where password should be stored. Can you describe this?

@AysadKozanoglu
Copy link

AysadKozanoglu commented Apr 21, 2020

You haven't explain where password should be stored. Can you describe this?
hello @mbaev,
I think in this tutorial described steps that were used with a private key without password, and that's why it works without a password

Quick Solution for "passwordless" ssh connection with private key:
first of all call following command one time in your shell (bash):

ssh-add -k ~/.ssh/id_rsa_YOUR_PRIV_key

after this one time command you will never asked for next connection with your private key again in current shell session

@ondrovic
Copy link

Thanks for this great guide

@emnabz
Copy link

emnabz commented Apr 4, 2021

You haven't explain where password should be stored. Can you describe this?
hello @mbaev,
I think in this tutorial described steps that were used with a private key without password, and that's why it works without a password

Quick Solution for "passwordless" ssh connection with private key:
first of all call following command one time in your shell (bash):

ssh-add -k ~/.ssh/id_rsa_YOUR_PRIV_key

after this one time command you will never asked for next connection with your private key again in current shell session

hi i just wanted to know what -k option do ?

@mukhtarhussainbaloch
Copy link

in case you have put a passphrase on ssh key where do we put it?

@AysadKozanoglu
Copy link

Default path for ssh key is under your home path .ssh (hidden folder)

e.g. İf your account is called "user" than:
/home/user/.ssh/

@AysadKozanoglu
Copy link

You haven't explain where password should be stored. Can you describe this?
hello @mbaev,
I think in this tutorial described steps that were used with a private key without password, and that's why it works without a password

Quick Solution for "passwordless" ssh connection with private key:
first of all call following command one time in your shell (bash):
ssh-add -k ~/.ssh/id_rsa_YOUR_PRIV_key
after this one time command you will never asked for next connection with your private key again in current shell session

hi i just wanted to know what -k option do ?

Parameter -k stays for Key. With this parameter you can set the path to your key which will be called to handle.

@mbaev
Copy link

mbaev commented Aug 4, 2023

@AysadKozanoglu sorry that I've asked you the question that way. Thank you for the answer!

@artemptushkin
Copy link

it works, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment