Menu
blog.headdesk.me
blog.headdesk.me

Use oathtool to generate 2FA code

Posted on 2020/08/302022/04/20

In this example, I’ll be using the oathtool on Ubuntu to generate 2FA code. I’ll be using this for 2FA authentication against AWS access.

Install oathtool

On the client machine, install oathtool

apt install oathtool

Enable MFA on AWS

On AWS, pick an IAM user and enable MFA.

Here, AWS can show the private key in format of a QR code, or as a string. Use the second option, click the “Show secret key” link.

Use oathtool to generate the MFA code twice.

oathtool -b --totp 'xxxxxxxxxxxxxxxxxxxxxxxx'
sleep 30
oathtool -b --totp 'xxxxxxxxxxxxxxxxxxxxxxxx'

Enter the 2 codes into the MFA code textbox, then click “Assign MFA”

Next time the IAM user logs into AWS, it will prompt for the MFA code, generate a new one using the same command shown above, just like using any hardware or software token.

Secure the private key

Now it is important to secure the private key. Otherwise why MFA? One can use gpg and wrap the commands in a script.

Another way is to put the oathtool command into a bash script and compile it into binary. It’s not secure, but it is easy to do. It’s better to have some security rather than none. First, create a simple script for oathtool:

!/bin/bash
oathtool -b --totp 'xxxxxxxxx'

Then compile it with shc. One can then run aws-oath to generate the MFA code.

shc -f aws-oath.sh -o aws-oath
rm -f aws-oath.sh
./aws-oath
420728

Loading

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Full text search

Recent Posts

  • Generate secure password
  • AWS Compute Savings Plans
  • AWS Zonal Shift
  • Coffee break…
  • Prevent private key from being committed to git
  • aws (14)
  • coffee (2)
  • headfi (1)
  • linux (9)
  • others (61)
  • security (2)
  • tech (41)
  • terraform (3)
  • wordpress (2)

Loading

apache aws awscli azure backup boot cloud coffee docker ec2 EL8 ElasticBeanstalk espresso featured git kernel lelit linux lvm meltdown MFA nat gateway php power proliant python rdp Redhat RHEL rpm Ryzen scp security smartarray smart switch snapshot spectre tech terraform ubuntu ubuntu upgrade vpn windows wordpress workspace

©2026 blog.headdesk.me | Powered by SuperbThemes