Sometime we need to add a user on Linux environment from command line or from a shell/bash script and give it sudo access. Linux has command useradd which can be used to this purpose and sudo permission can be given using /etc/sudoers.d directory. Here are the steps for this on Ubuntu Linux (will work on Amazon linux and other linux flavours also):

Add a user with default shell as bash

To add user (say user1) with default shell bash, without password and create home directory if needed, run the following command as root:

$ useradd user1 -m -s /bin/bash

In case you want to create a user with password use -p. This will prompt you for a password.

Authorizing user’s key to ssh

It is a good idea to use user’s key to ssh to machine as that user. Run the following commands for that as root: