Is production: true
#migrated

Title: How to add google authenticator to ssh [ubuntu]

Created: 19 Mar 2023 Modified: 19 Mar 2023

Description:



[Legacy Link]

[Link 1] Not fully useful

[Link 2] Not fully useful

[Link 3] Useful

Instructions

  1. Install the rpm

    sudo apt install libpam-google-authenticator
    
  2. Install google-authenticator

    google-authenticator
    
  3. Initialize google authenticator (How to add google authenticator to ssh [centos] )

    google-authenticator
    Do you want authentication tokens to be time-based (y/n) y
    Do you want to disallow multiple uses of the same authentication
    token? This restricts you to one login about every 30s, but it increases
    your chances to notice or even prevent man-in-the-middle attacks (y/n) y
    
    By default, a new token is generated every 30 seconds by the mobile app.
    In order to compensate for possible time-skew between the client and the server,
    we allow an extra token before and after the current time. This allows for a
    time skew of up to 30 seconds between authentication server and client. If you
    experience problems with poor time synchronization, you can increase the window
    from its default size of 3 permitted codes (one previous code, the current
    code, the next code) to 17 permitted codes (the 8 previous codes, the current
    code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
    between client and server.
    Do you want to do so? (y/n) n
    
    If the computer that you are logging into isn't hardened against brute-force
    login attempts, you can enable rate-limiting for the authentication module.
    By default, this limits attackers to no more than 3 login attempts every 30s.
    Do you want to enable rate-limiting? (y/n) y
    
  4. Before starting step 5 and later step, please ensure you kept at least one logged in session. The reason for keeping session is to reset the setting if any unexpected issue happened. If missing the session, maybe never get back to server. Try to login to the ssh with new putty process and flowing step 5 and 6, until the the login behaviour match our target (publickey + google authenticator).

  5. Edit pam.d/sshd

    sudo vim /etc/pam.d/sshd
    
    #####################
    ###Comment below line
    
    #@include common-auth
    
    #Append below line
    auth required pam_google_authenticator.so nullok
    
    #nullok means the pam_google_authenticator.so is no mandatory
    #if all user using totp then the nullok can be removed
    
    //Restart sshd service
    sudo systemctl restart sshd
    
  6. Edit ssh_config

    sudo vim /etc/ssh/sshd_config
    
    ####################
    #change ChallengeResponseAuthentication to yes
    ChallengeResponseAuthentication yes
    
    #Seem missing or not mandatory setting
    PubkeyAuthentication yes
    
    #define the authentication method as publickey + keyboard-interactive
    AuthenticationMethods publickey,keyboard-interactive
    ####################
    
    #restart sshd
    sudo systemctl restart sshd