Knowledge Base

Browse our knowledge base for free solutions to common problems

Setup Basic Key Based Authentication In Linux

Created On: 14 September 2022
Written by: Ben

1) Create a new user to access the machine besides root and give them sudo:

useradd -m -c "John Doe" john.doe
usermod -aG wheel john.doe
mkdir /home/john.doe/.ssh
chown john.doe:john.doe /home/ben.padgett/.ssh
chmod 700 /home/john.doe/.ssh
passwd john.doe

2) Locally create a key to access the newly created users account:

ssh-keygen -t ecdsa -b 521
ssh-copy-id -i id_ecdsa.pub john.doe@IP_ADDRESS -p PORT NUMBER

3) Replace the running SSH config with the following (replace ListenAddress with the IP Address of the server and AllowUsers to match the newly created user):

##########################################################################################
# SSH NETORK SETTINGS
##########################################################################################
Port 1337
ListenAddress ETH0_IP_HERE
 
 
##########################################################################################
# SSH HOST KEY LOCATION SETTINGS
##########################################################################################
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#HostKey /etc/ssh/ssh_host_dsa_key
 
 
##########################################################################################
# SSH CIPHERS AND KEYRING
##########################################################################################
#RekeyLimit default none
 
 
##########################################################################################
# SSH LOGGING
##########################################################################################
SyslogFacility AUTHPRIV
#SyslogFacility AUTH
#LogLevel INFO
 
 
##########################################################################################
# SSH AUTHENTICATION SETTINGS
##########################################################################################
LoginGraceTime 1m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
 
 
##########################################################################################
# PUBLIC KEY AUTHENTICATION
##########################################################################################
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
#HostbasedAuthentication no
#IgnoreUserKnownHosts no
#IgnoreRhosts yes
 
##########################################################################################
# TUNNELED CLEAR TEXT PASSWORDS
##########################################################################################
PermitEmptyPasswords no
PasswordAuthentication no
 
 
##########################################################################################
# Change to no to disable s/key passwords
##########################################################################################
ChallengeResponseAuthentication no
 
 
##########################################################################################
# GSSAPI OPTIONS
##########################################################################################
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
 
 
##########################################################################################
# PAM AUTHENTICATION SETTINGS
##########################################################################################
UsePAM yes
 
 
##########################################################################################
# LIMIT USERS ALLOWED TO LOGIN
##########################################################################################
AllowUsers john.doe
DenyUsers root
 
 
##########################################################################################
# FORWARDING OPTIONS & OTHERS
##########################################################################################
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
 
 
##########################################################################################
# SSH BANNER OPTIONS
##########################################################################################
#Banner none
 
 
##########################################################################################
# SSH ENVIRONMENT SETTINGS
##########################################################################################
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
 
##########################################################################################
# OVERRIDE DEFAULT OF NO SUBSYSTEMS
##########################################################################################
Subsystem sftp /usr/libexec/openssh/sftp-server

4) Restart SSH

service sshd restart

5) Confirm running status

service sshd status

 

ICTU LTD is a company registered England and Wales (Company No. 09344913) 15 Queen Square, Leeds, West Yorkshire, England, LS2 8AJ
Copyright © 2025 ICTU LTD, All Rights Reserved.
exit