Joshua's Docs - Ssh

Logging In

For creating key-pairs, see section on generating keys

  • Logging in with a username and password

    • ssh {username}@{ssh_host_address}
  • Logging in with private key

    • ssh -i {path_to_private_key} {username}@{ssh_host_address}

Setting up SSH with a New User Account

If you have just created a new user account, to move away from using root as your login, you will need to provision it for SSH by A) copying existing keys to the user's new .ssh directory (if applicable), and B) granting that user ownership over the .ssh directory.

These steps have multiple paths they can take, depending on your setup:

Before these steps, the user has to actually exist; use adduser or useradd to create them first.

  1. Create the user's .ssh directory under their home directory

    • If you are currently logged in as root and not the user

      • If you want to copy all existing root keys to the user

        • rsync --archive --chown={USERNAME}:{USERNAME} ~/.ssh ~{USERNAME}
      • If you don't want to copy root keys to user

        • mkdir --parents "~{USERNAME}/.ssh"
    • If you are currently logged in as the user itself

      • If you don't want to copy root keys to user

        • mkdir --parents "~/.ssh"
  2. Set file permissions and ownership (where {SSH_DIR} is the directory created above)

    1. Set file permissions

      • chmod 700 {SSH_DIR}
      • chmod 600 {SSH_DIR}/authorized_keys

        • Many guides will recommend 644 instead of 600, but either should work
        • If the file does not exist yet, either copy it from root, or create it with touch, then apply permissions
    2. Give ownership to the new user

      • As root:

        • chown --recursive {USERNAME}:{USERNAME} {SSH_DIR}

          • Example: chown --recursive joshua:joshua ~joshua/.ssh
      • As the user

        • Should be same as above, but prefix with sudo
  3. (OPTIONAL): Add additional keys

    • You can use a text editor:

      • nano ~{USERNAME}/.ssh/authorized_keys
    • You can dump local files, adding their contents to the end of the list

      • Example: echo >> ~{USERNAME}/.ssh/authorized_keys
    • You can use the ssh-copy-id tool
Markdown Source Last Updated:
Fri Jun 04 2021 10:17:29 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Thu Jun 03 2021 20:36:14 GMT+0000 (Coordinated Universal Time)
© 2021 Joshua Tzucker, Built with Gatsby