Skip to content

Instantly share code, notes, and snippets.

@GabLeRoux
Last active September 5, 2018 18:49
Show Gist options
  • Select an option

  • Save GabLeRoux/ad3fb942d788e6f49e6ce3fe3c310cb8 to your computer and use it in GitHub Desktop.

Select an option

Save GabLeRoux/ad3fb942d788e6f49e6ce3fe3c310cb8 to your computer and use it in GitHub Desktop.

Revisions

  1. GabLeRoux revised this gist Aug 31, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions how-to-add-ssh-key-to-remote-host.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ ssh-add ~/.ssh/EXAMPLE_rsa
    ## 2. Add your public key to the remote server

    ```bash
    ssh-copy-id -i ~/.ssh/my_key_rsa -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    ssh-copy-id -i ~/.ssh/EXAMPLE_rsa -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    ```

    ## 3. Add an entry to your `~/.ssh/config`
    @@ -20,7 +20,7 @@ ssh-copy-id -i ~/.ssh/my_key_rsa -o PreferredAuthentications=password -o PubkeyA
    Host example-alias example.com
    Hostname example.com
    User user
    IdentityFile ~/.ssh/my_key_rsa
    IdentityFile ~/.ssh/EXAMPLE_rsa
    IdentitiesOnly yes
    ```

  2. GabLeRoux revised this gist May 24, 2018. 4 changed files with 32 additions and 22 deletions.
    7 changes: 0 additions & 7 deletions 1-generate-ssh-key.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +0,0 @@
    #!/usr/bin/env bash

    # Generate new ssh key (from github)
    # https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/EXAMPLE_rsa
    4 changes: 0 additions & 4 deletions 2-add-ssh-key-to-authorized_keys.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +0,0 @@
    #!/usr/bin/env bash

    # run this on your system
    ssh-copy-id -i ~/.ssh/my_key_rsa -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    11 changes: 0 additions & 11 deletions 3-home-ssh-config.sh
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    # Add entries like this to your ~/.ssh/config file

    Host example-alias example.com
    Hostname example.com
    User user
    IdentityFile ~/.ssh/my_key_rsa
    IdentitiesOnly yes

    # use these instead of 'IdentitiesOnly yes' for your first connection
    #PreferredAuthentications=password
    #PubkeyAuthentication=no
    32 changes: 32 additions & 0 deletions how-to-add-ssh-key-to-remote-host.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    ## 1. Generate yourself a new ssh key

    ```bash
    # Generate new ssh key (from github)
    # https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/EXAMPLE_rsa
    ```

    ## 2. Add your public key to the remote server

    ```bash
    ssh-copy-id -i ~/.ssh/my_key_rsa -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    ```

    ## 3. Add an entry to your `~/.ssh/config`

    ```
    Host example-alias example.com
    Hostname example.com
    User user
    IdentityFile ~/.ssh/my_key_rsa
    IdentitiesOnly yes
    ```

    Note: you can use these instead of `IdentitiesOnly yes` for your first connection

    ```bash
    PreferredAuthentications=password
    PubkeyAuthentication=no
    ```
  3. GabLeRoux revised this gist May 24, 2018. 3 changed files with 3 additions and 0 deletions.
    1 change: 1 addition & 0 deletions 1-generate-ssh-key.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #!/usr/bin/env bash

    # Generate new ssh key (from github)
    # https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    2 changes: 2 additions & 0 deletions 2-add-ssh-key-to-authorized_keys.sh
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,4 @@
    #!/usr/bin/env bash

    # run this on your system
    ssh-copy-id -i ~/.ssh/my_key_rsa -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    File renamed without changes.
  4. GabLeRoux revised this gist May 24, 2018. 4 changed files with 3 additions and 17 deletions.
    2 changes: 2 additions & 0 deletions 2-add-ssh-key-to-authorized_keys.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    #!/usr/bin/env bash
    ssh-copy-id -i ~/.ssh/my_key_rsa -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    2 changes: 0 additions & 2 deletions 2-connect-ssh-with-password.sh
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    # Connect without using ssh keys
    ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    14 changes: 0 additions & 14 deletions 3-add-ssh-key-to-authorized_keys.sh
    Original file line number Diff line number Diff line change
    @@ -1,14 +0,0 @@
    #!/usr/bin/env bash
    # run this on the remote server

    # Append ssh key to authorized_keys (replace public key with yours)
    export TEMP_SSH_USERNAME=$(whoami)
    export SSH_TEMP_PUBLIC_KEY="PASTE YOUR PUBLIC KEY HERE"

    mkdir ~/.ssh
    chmod 700 ~/.ssh
    touch ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    echo $SSH_TEMP_PUBLIC_KEY >> ~/.ssh/authorized_keys

    sudo chown -R $TEMP_SSH_USERNAME:$TEMP_SSH_USERNAME /home/$TEMP_SSH_USERNAME/.ssh
    2 changes: 1 addition & 1 deletion home-ssh-config → 3-home-ssh-config
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    Host example-alias example.com
    Hostname example.com
    User user
    IdentityFile ~/.ssh/EXAMPLE_rsa
    IdentityFile ~/.ssh/my_key_rsa
    IdentitiesOnly yes

    # use these instead of 'IdentitiesOnly yes' for your first connection
  5. GabLeRoux created this gist May 14, 2018.
    6 changes: 6 additions & 0 deletions 1-generate-ssh-key.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #!/usr/bin/env bash
    # Generate new ssh key (from github)
    # https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/EXAMPLE_rsa
    2 changes: 2 additions & 0 deletions 2-connect-ssh-with-password.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Connect without using ssh keys
    ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
    14 changes: 14 additions & 0 deletions 3-add-ssh-key-to-authorized_keys.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #!/usr/bin/env bash
    # run this on the remote server

    # Append ssh key to authorized_keys (replace public key with yours)
    export TEMP_SSH_USERNAME=$(whoami)
    export SSH_TEMP_PUBLIC_KEY="PASTE YOUR PUBLIC KEY HERE"

    mkdir ~/.ssh
    chmod 700 ~/.ssh
    touch ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    echo $SSH_TEMP_PUBLIC_KEY >> ~/.ssh/authorized_keys

    sudo chown -R $TEMP_SSH_USERNAME:$TEMP_SSH_USERNAME /home/$TEMP_SSH_USERNAME/.ssh
    11 changes: 11 additions & 0 deletions home-ssh-config
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # Add entries like this to your ~/.ssh/config file

    Host example-alias example.com
    Hostname example.com
    User user
    IdentityFile ~/.ssh/EXAMPLE_rsa
    IdentitiesOnly yes

    # use these instead of 'IdentitiesOnly yes' for your first connection
    #PreferredAuthentications=password
    #PubkeyAuthentication=no