Administration

SSH user directory setup on Linux

Recommended directories, files, and permissions for each user's SSH config - Blog by SH3LL

SH3LL
· 3 min read
Send by email
Photo by Igor Omilaev / Unsplash

SSH user directory setup on Linux

Connect with me!

SSH SETUP FOR NEW USERS

A quick reference guide for setting up SSH directories, files, and permissions for new linux users. This guide assumes you are currently logged in with your new user and that user has the sudo privilege.


  1. Navigate to your home directory
cd ~/

  1. Check if the ".ssh" directory exists
ls -la

if the .ssh directory already exists, skip to step 4, otherwise, continue with setup


  1. Make the .ssh directory
sudo mkdir ~/.ssh

add correct permissions

sudo chmod 700 ~/.ssh

add correct ownership

sudo chown mynewusername:mynewusername ~/.ssh

  1. Check if there is an "authorized_keys" file
cd ~/.ssh
ls -la

if the authorized_keys file exists, skip to step 6, otherwise, continue with setup


  1. Create the authorized_keys file
touch ~/.ssh/authorized_keys

add correct permissions

sudo chmod 600 ~/.ssh/authorized_keys

add correct ownership

sudo chown mynewusername:mynewusername ~/.ssh/authorized_keys
  1. If you need to create a key pair, on your local system (not the remote ssh target), create a new key pair using the steps listed here and don't forget to add the private key to your authentication agent.

  2. On your remote system, we need to add the key to the authorized_keys file. From your local host, run the following command

Linux

ssh-copy-id -i "path/to/public/key" mynewusername@IP

Windows

ssh-copy-id -i "path\to\public\key" mynewusername@IP

Or manually by copying the contents of your new public key (.pub) and pasting it in the following command on the remote system.

echo "public key goes here in quotes" >> ~/.ssh/authorized_keys

verify the key was added correctly

cat ~/.ssh/authorized_keys
  1. If you have /etc/ssh/sshd_config locked down by allowed users, you will need to add them to the AllowUsers line.
sudo nano /etc/ssh/sshd_config
AllowUsers existingusername mynewusername
  1. Restart ssh
sudo systemctl restart ssh

Thanks for reading!

- SH3LL

CONNECT