Joshua's Docs - Ssh

Logging In

For creating key-pairs, see my shell 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}
    • If you want this to happen automatically, you need to set up a SSH config file
    • Generally not a good idea to rely on auto-discovery of keys, so either of the above methods is preferred.

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
      • Use -i {PATH_TO_PUB_KEY}.pub for manually specifying the key to upload
      • Extended example: ssh-copy-id -i ~/.ssh/ user@host
      • Make sure to only copy the public key up to the server!

Copying Local Files

If you want to copy local files to a remote location over SSH, there are more than a few options, but the most common is to not use SSH directly, but rather the scp command.

You can also use the rsync command over SSH, which for frequent synchronizations of the same directory, would be preferable given that it optimizes for only syncing what has changed.

Here is a guide for using rsync with a remote destination:

Markdown Source Last Updated:
Fri Aug 27 2021 03:10:01 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