Skip to content

Instantly share code, notes, and snippets.

@chrisgalard
Last active June 23, 2023 09:52
Show Gist options
  • Save chrisgalard/0a06e415d140ca9a39d413c84f0bfdda to your computer and use it in GitHub Desktop.
Save chrisgalard/0a06e415d140ca9a39d413c84f0bfdda to your computer and use it in GitHub Desktop.
Creating user with SSH access in Linux

#Creating a new user with SSH access in Linux

##1. Log in as root ssh root@SERVER_IP_ADDRESS

##2. Create a new user adduser username

##3. Add the newly created user to the sudo group gpasswd -a username sudo

##4. Add public key authentication Generate a key pair in your local machine with ssh-keygen and copy the generated public key to the server using ssh-copy-id username@SERVER_IP_ADDRESS

####Alternative: manual install Generate a key pair with the same method above and print public key with cat ~/.ssh/id_rsa.pub. In the server use su - username to switch to the new user. Then create a directory called .ssh and restrict its permissions:

mkdir .ssh
chmod 700 .ssh

Now paste the public key in a file called authorized_keys inside .ssh and and then restrict its permissions using chmod 600 .ssh/authorized_keys.

##5. Restrict root access Look for the line PermitRootLogin in the file /etc/ssh/sshd_config and change it from yes to no.

##6. Restart the SSH daemon service ssh restart

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