azuresshvirtual-machineconfigsshd

Is there a way to enable password authentication such that user can log in with password or public key in azure vm?


I am trying to figure out a way to let users log into my VM using password if they do not have the correct public key. I do not want to remove public key authentication, so is there a way to enable both depending on the user?

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication no

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
KbdInteractiveAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem   sftp    /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server
ChallengeResponseAuthentication no

This is my sshd_config file. What changes do i make here to get what I want?


Solution

  • I tried to reproduce the same issue in my environment and got the below results

    I have created the linux vm and connected as a root user

    enter image description here

    There are two ways in which we can enable the password authentication in the sshd_config file.

    Match user username
    PasswordAuthentication yes
    

    We can add the above lines like Echo "Match user username\nPasswordAuthentication yes" >> /etc/sshd/sshd_config

    The above command is for single user if we need multiple users Match user u_name1,u_name2,.... with coma separated we can add the multiple users

    After adding the above lines we have to restart the file otherwise it won't reflect

    sudo systemctl restart ssh
    

    Second possible way is, I have created the new script

    sudo su -i
    cd /usr/local/bin
    vi filename.sh
    

    enter image description here

    In that file please write the following script

           #!/bin/bash
            
            while [[ $# -gt 0 ]]; do
              key="$1"
              case $key in
                help)
                  usage
                  shift
                  shift
                  ;;
                -u|--user) SFTP_USER="$2"
                  shift # past argument
                  shift # past value
                  ;;
                -p|--password) THE_PASSWORD="$2"
                  shift # past argument
                  shift # past value
                  ;;
              esac
            done
            
            function usage {
              echo "Usage: $0 --user testuser [ --password <your password> ]" exit 0
            }
            
            if [[ -z ${SFTP_USER} ]]; then
              usage
            fi
            
            LOCAL_SECRET_ACCESS_KEY=$(sudo grep ldap.password /opt/sftpgw/application.properties | cut -d'=' -f2)
            
            if [[ -n "$THE_PASSWORD" ]]; then
              NEW_PASSWORD="${THE_PASSWORD}"
            else
              NEW_PASSWORD=$(head /dev/urandom | tr -dc A-Z0-9 | head -c 12 ; echo '')
            fi
            
            ldapmodify -D "cn=admin" -w ${LOCAL_SECRET_ACCESS_KEY} <<HERE
            dn: uid=${SFTP_USER},ou=People,dc=sftpgateway,dc=com
            changetype: modify
            replace: userPassword
            userPassword: ${NEW_PASSWORD}
            HERE
            
            echo "The password for the new user ${SFTP_USER} has been set to ${NEW_PASSWORD}"
    

    After that run the script and pass the username and password

    /usr/local/bin/password.sh -u <u_name> -p <password>
    

    NOTE: If we don't pass the password it will generate the password