Adding a user to instance

Problem

Adding a user to AWS instance - ubuntu

Solution

There are different ways of adding a user to ubuntu.

Addin a user in ubuntu

New users, by default, are unprivileged. This means that they will only be able to modify files in their own home directory, which is what we want.

If this is your first new user, and you are currently logged in as the root user, you can use the following syntax to create a new user:

adduser newuser

If you are logged into a user that you added previously and gave sudo privileges, you can create a new user by invoking sudo with the same command:

sudo adduser newuser

Either way, Debian will prompt you for more information about the user you are creating. The first piece of information you need to choose is the password for the new user.

It will ask you to select a password and then confirm it by repeating it (again, the characters you type will not appear in the window, for security purposes).

Afterwards, it will ask you for personal information about the user. You can feel free to fill this out or to leave it blank. The user will operate in entirely the same way regardless of your decision. Type ENTER to skip these prompts and accept the entered values.

Accessing the New User When you have finished these steps, your new user is now available. You can log into the new user by typing:

exit

ssh newuser@your_server_ip_address

This will terminate your current session as root and allow you to log in as the new user through SSH by typing:

ssh newuser@your_server_ip_address

This time, enter the new password you just configured for this user.

Another way to quickly switch to another user without logging out first is to use the su command. This command stands for substitute user and it allows you to enter the user you would like to change to. You can use it like this:

su - newuser

This will ask you for the new user's password. When you've entered it correctly, you will be changed to the new user. When you wish to exit back into your original session, simply issue the exit command again:

exit

Granting Users Administrative Privileges

Now that you have a new user on your system, you need to decide if this user should be able to perform administrative tasks with sudo.

If the user you created will be your primary user on the system, you usually want to enable sudo privileges so that you can do routine configuration and maintenance.

To add these privileges to our new user, we need to add the new user to the sudo group. By default users who belong to the sudo group are allowed to use the sudo command.

As root, run this command to add your new user to the sudo group (substitute the highlighted word with your new user):

usermod -aG sudo sammy

Now, when you are logged in as your regular user, you can execute a certain command with root privileges by typing:

sudo command_name

You will be prompted to enter your user's password (not the root user's password). The command will then be executed with elevated access.

Deleting a User

If more than one person is using your server, you should give them their own user to log in. If there is a user you created that you no longer need, it is very easy to delete it.

As a regular user with sudo privileges, you can delete a user using this syntax:

sudo deluser --remove-home username

The --remove-home option will delete the user's home directory as well.

If you are logged in as root, you do not need to add the sudo before the command:

deluser --remove-home username

Adding user with their public key


cat ~/.ssh/<your_keys>.pub | ssh user@<your-instance>.amazonaws.com"
mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"