Skip to content

Instantly share code, notes, and snippets.

@h1dd3nsn1p3r
Last active March 20, 2024 06:31
Show Gist options
  • Save h1dd3nsn1p3r/ec60bb623e42563038c6ef0696445733 to your computer and use it in GitHub Desktop.
Save h1dd3nsn1p3r/ec60bb623e42563038c6ef0696445733 to your computer and use it in GitHub Desktop.
Add new user and enable SSH access.

Add user.

adduser anujsubedi

Mod user to sudo.

usermod -aG sudo anujsubedi

Switch to user.

su - anujsubedi

Create .ssh directory and create authorized_keys file.

mkdir ~/.ssh

touch ~/.ssh/authorized_keys

Generate SSH key in your system.

ssh-keygen -t rsa -b 4096 -C "[email protected]"  # Replace with your email.

Add public key to authorized_keys file.

echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDZ ...  anujsubedi@anujsubedi" >> ~/.ssh/authorized_keys  # Replace with your public key.

Set permissions.

chmod 700 ~/.ssh

chmod 600 ~/.ssh/authorized_keys

Disable password authentication.

sudo nano /etc/ssh/sshd_config
  • Change port 22 to 2611 or any other port you want.
  • Change PasswordAuthentication yes to PasswordAuthentication no
  • Change usePAM yes to usePAM no
  • Change ChallengeResponseAuthentication yes to ChallengeResponseAuthentication no

Test the SSH login using the new user.

ssh -i /path/to/private/key anujsubedi@ip -p 2611 # Replace with your private key and ip.

Restart ssh service.

If you were able to connect to the SSH using the new user, restart the ssh service.

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