{"id":47209,"date":"2024-09-11T16:19:38","date_gmt":"2024-09-11T16:19:38","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=47209"},"modified":"2024-09-17T11:57:01","modified_gmt":"2024-09-17T11:57:01","slug":"one-time-password-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/09\/one-time-password-ubuntu\/","title":{"rendered":"Enable One Time Password in Ubuntu 24.04 for SSH or Local Login"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/password-keyring-seahorse.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-46620\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/password-keyring-seahorse-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/password-keyring-seahorse-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/password-keyring-seahorse-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/password-keyring-seahorse-700x700.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/password-keyring-seahorse-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/password-keyring-seahorse.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This tutorial shows how to enable One Time PassWord in Ubuntu 24.04 for either local or remote SSH login.<\/p>\n<p>One Time PassWord, OTPW in short, is a PAM module which is useful for allowing a user to login public or shared computer\/server using a single-use password, that works only for one time.<\/p>\n<p>By generating a list OTPW passwords, and configuring your system to allow OTPW logins, it will ask random one of the OTPW passwords on every login. And, that password will never work again once logged in successfully with it.<\/p>\n<p><!--more--><\/p>\n<h3>Step 1: Install OTPW package<\/h3>\n<p>The OTPW package is available in the universe repository for all current Ubuntu releases.<\/p>\n<p><b>To install it<\/b>, open terminal (Ctrl+Alt+T) or connect to remove server and run command:<\/p>\n<pre>sudo apt install libpam-otpw otpw-bin<\/pre>\n<p>Here, the <code>libpam-otpw<\/code> package includes the PAM module that can enable OTPW password login. While <code>otpw-bin<\/code> offers command to generate OTPW passwords.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apt-otpw.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47210\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apt-otpw-700x246.webp\" alt=\"\" width=\"610\" height=\"214\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apt-otpw-700x246.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apt-otpw-300x105.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apt-otpw.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 2: Generate a list of OTPW passwords<\/h3>\n<p>After installing the packages above, you can now run command below to generate random passwords:<\/p>\n<pre>otpw-gen &gt; ~\/otpw_passwords<\/pre>\n<p>The command will generate a file called &#8220;<code>otpw_passwords<\/code>&#8221; in user home directory, which includes 280 random generated passwords.<\/p>\n<p>And, it will ask you to set a <b>prefix password<\/b>. When login with OTPW module, you need to type this <b>prefix password<\/b> + <b>OTPW password<\/b>. So, others cannot access to your account even if you lost the password list.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-generate.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47211\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-generate-700x436.webp\" alt=\"\" width=\"610\" height=\"380\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-generate-700x436.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-generate-300x187.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-generate-768x479.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-generate.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>At any time, you may re-run the last command to re-create the password list. Which, will override both the previous prefix and one-time passwords.<\/p>\n<p><b>The passwords work only for the user account who generated them.<\/b> For any other user, either run command <code>su username<\/code> to switch to that user and re-run the <code>otpw-gen<\/code> command, or use command below instead:<\/p>\n<pre>su -c \"otpw-gen &gt; ~\/otpw_passwords\" username<\/pre>\n<p>In command, replace <code>username<\/code> with the target account name.<\/p>\n<p>And, here&#8217;s an example password list. For yours, either take a photo using your phone, or print it out for later use.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-passwd-list.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47212\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-passwd-list-700x490.webp\" alt=\"\" width=\"610\" height=\"427\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-passwd-list-700x490.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-passwd-list-300x210.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-passwd-list-768x538.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-passwd-list.webp 1032w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 3: Tell your system to allow OTPW password login<\/h3>\n<p><b>1.<\/b> First, run the command below to create a PAM config file and edit with nano command line text editor:<\/p>\n<pre>sudo nano \/etc\/pam.d\/ssh-otpw<\/pre>\n<p>When it opens, past the lines below, which tell to authenticate with otpw module, and permit access immediately if succeed.<\/p>\n<pre>auth sufficient pam_otpw.so\r\nsession optional pam_otpw.so<\/pre>\n<p>Finally, press Ctrl+S to save and Ctrl+X to exit.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-pam.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47213\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-pam-700x508.webp\" alt=\"\" width=\"610\" height=\"443\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-pam-700x508.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-pam-300x218.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-pam-768x558.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-pam.webp 866w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h4>Enable OTPW for SSH Login<\/h4>\n<p><b>2. To enable OTPW PAM module for SSH<\/b>, then edit the <code>sshd<\/code> PAM config file via command:<\/p>\n<pre>sudo nano \/etc\/pam.d\/sshd<\/pre>\n<p>When file opens, include the config you just made, by adding <b>@include ssh-otpw<\/b>. NOTE that, you need to:<\/p>\n<ul>\n<li>either disable <b>@include common-auth<\/b> (add # at the beginning), so the classic static password login will NO longer work (for all users)! Be sure you have alternative authentication method in case of running out of OTPW passwords.<\/li>\n<li>or, make sure <b>@include common-auth<\/b> is below <b>@include ssh-otpw<\/b>, or it will ask for static password first!<\/li>\n<\/ul>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-sshlogin.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47214\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-sshlogin-700x468.webp\" alt=\"\" width=\"610\" height=\"408\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-sshlogin-700x468.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-sshlogin-300x201.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-sshlogin-768x514.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-sshlogin.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>3.<\/b> After that, also edit the SSH daemon config file by running command:<\/p>\n<pre>sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>When file opens, find out the lines below and set (or add lines if they NOT exist) to use PAM authentication:<\/p>\n<p><b>UsePAM yes<\/b><\/p>\n<p><b>KbdInteractiveAuthentication yes<\/b><\/p>\n<p>Also set <code>PasswordAuthentication no<\/code>, though the static password will still work unless <code>@include common-auth<\/code> is disabled in the PAM config or <code>KbdInteractiveAuthentication<\/code> is also disabled in this sshd config.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/sshd-usepam.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47215\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/sshd-usepam-700x582.webp\" alt=\"\" width=\"610\" height=\"507\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/sshd-usepam-700x582.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/sshd-usepam-300x250.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/sshd-usepam.webp 714w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>4.<\/b> When done editing the file, press Ctrl+S to save and Ctrl+X to exit. Finally, <b>restart SSH to apply changes<\/b>:<\/p>\n<pre>sudo systemctl daemon-reload\r\n\r\nsudo systemctl restart ssh.socket<\/pre>\n<p>For old Ubuntu 22.04 and earlier, only run <code>sudo systemctl restart ssh<\/code> should work.<\/p>\n<p><b>5.<\/b> Next, you may try to start a SSH connection to this computer or server.<\/p>\n<p>It should ask for password with 3 digital numbers. In the screenshot, it asked for password 004. While I set <b>prefix password<\/b> to &#8220;ade564&#8221;, and the password file shows me <b>004 Zh63 7Qyi<\/b>. So, I need to input <b>ade564Zh637Qyi<\/b> to get pass.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/ssh-otpw-login.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47216\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/ssh-otpw-login-700x292.webp\" alt=\"\" width=\"610\" height=\"254\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/ssh-otpw-login-700x292.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/ssh-otpw-login-300x125.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/ssh-otpw-login.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>NOTE 1:<\/b> If you left <code>@include common-auth<\/code> enabled in <code>\/etc\/pam.d\/sshd<\/code> config file, type user&#8217;s static password will also let you in!<\/p>\n<p><b>NOTE 2: If someone else try to login at the same time when you login, it MAY ask for 3 passwords instead of one. The prompt may look like &#8220;Password 004\/027\/134&#8221;. In the case, you need to type prefix + the 3 passwords in the order. After login, you need to delete <code>~\/.otpw.lock<\/code> or it always ask for 3 passwords.<\/b><\/p>\n<h4>(optional) Enable One Time Password for local desktop login<\/h4>\n<p>If you want to use one time password to log in Ubuntu Desktop, then, edit the <code>common-auth<\/code> PAM config file instead by running command:<\/p>\n<pre>sudo nano \/etc\/pam.d\/common-auth<\/pre>\n<p>When file opens, add <b>@include ssh-otpw<\/b> at top and save.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-login.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47217\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-login-700x508.webp\" alt=\"\" width=\"610\" height=\"443\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-login-700x508.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-login-300x218.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-login-768x558.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-login.webp 866w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>Next time in the login screen, when you click the user who has generated OTPW password list, it will show you &#8220;Password XXX&#8221; asking for the corresponding OTPW password (don&#8217;t forget to add prefix password while typing).<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-gnome-login.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47218\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-gnome-login-700x435.webp\" alt=\"\" width=\"610\" height=\"379\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-gnome-login-700x435.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-gnome-login-300x187.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-gnome-login-768x478.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/otpw-gnome-login.webp 831w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>Also, input the static user password will also let you in!<\/p>\n<h3>Undo:<\/h3>\n<p>To disable one time password, simply remove the <b>@include ssh-otpw<\/b> line from either <code>\/etc\/pam.d\/sshd<\/code> or <code>\/etc\/pam.d\/common-auth<\/code> file depends on which one you enabled.<\/p>\n<p>Optionally, you may remove the module package to save few disk space:<\/p>\n<pre>sudo apt remove --autormove libpam-otpw<\/pre>","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to enable One Time PassWord in Ubuntu 24.04 for either local or remote SSH login. One Time PassWord, OTPW in short, is a PAM module which is useful for allowing a user to login public or shared computer\/server using a single-use password, that works only for one time. By generating a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":46620,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[322,2039,24],"class_list":["post-47209","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-password","tag-security","tag-ssh"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=47209"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47209\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/46620"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=47209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=47209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=47209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}