Adding SSH accounts for EC2 AIM instance?

I am doing a group project for a class and I am hosting the project on an EC2 AIM instance and have it so I can connect to it from the machine I made it on. I need to create 6 accounts for my group members to use to connect from their machines. If anyone could provide assistance I would greatly appreciate it!


Related discussions:

sudo adduser username

I’ve done this but how do I set their password and ssh key?

passwd username

SSH keys go in /home/username/.ssh/authorized_keys file for each user

“sudo passwd [username]”

And

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/managing-users.html

If you want to re-use the SSH key for everyone.

Separate keys? Then this https://www.howtoforge.com/tutorial/ssh-and-scp-with-public-key-authentication/ should work

If I want to use the same key for everyone will I have to supply them with the .pem file so they can add it to their local .ssh folder on their machines? I’m pretty new to dealing with servers and pretty much have no idea how to do this, so I really appreciate the help.

It’s a pretty bad idea for everyone to use the same key. Are they ssh’ing in from windows or linux?

If they’re connecting from linux, they should run ‘ssh-keygen’ on their local machines, then you should copy the contents of their local ~/.ssh/id_rsa.pub to your EC2 server /home/username/.ssh/authorized_keys file for each different user account.

They can even do this themselves automatically by typing “ssh-copy-id username@ip-of-ec2-server” then typing the password that you give them and it’ll copy the key into authorized_keys automatically for you.

I think everyone besides me is using a Windows machine and connecting via PuTTY.

In that case, do this: https://www.digitalocean.com/community/tutorials/how-to-create-ssh-keys-with-putty-to-connect-to-a-vps

They should generate their own keys with puttygen and they should be installed in each user account.

Thanks for the help, I really appreciate it. I’m meeting with my group tomorrow so I’ll see how it goes.

Good luck! Let us know if you need anything else.

and if you’re inexperienced enough to need to be told that, you probably also need to be told: use the public key, not the private key in the authorized_keys file

Have your members create their own key pairs - preferably password protected key pairs - using this command:

ssh-keygen -t rsa -b 8192

Have them give you their public keys. They should never give you their private keys and/or passphrase.

When you launch an AWS Linux AMI you can use the following script in the User Data field to automatically add the members and disable login. The snippet below shows how to do it for two users - simply add as many members as you need and paste in their public keys:

#!/bin/bash

yum update -y
USER1=member1
USER2=member2

adduser $USER1 && mkdir /home/$USER1/.ssh && chmod 700 /home/$USER1/.ssh
echo "member1 public ssh key goes here)" > /home/$USER1/.ssh/authorized_keys
chmod 600 /home/$USER1/.ssh/authorized_keys
chown -R $USER1:$USER1 /home/$USER1/.ssh
echo "$USER1 ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

adduser $USER2 && mkdir /home/$USER2/.ssh && chmod 700 /home/$USER2/.ssh
echo “” > /home/$USER2/.ssh/authorized_keys
chmod 600 /home/$USER2/.ssh/authorized_keys
chown -R $USER2:$USER2 /home/$USER2/.ssh
echo “$USER2 ALL=(ALL) NOPASSWD:ALL” >> /etc/sudoers

Once the instance is launched and assuming that you have your security groups configured correctly they should be able to login to the instance using their username and key pair.

You coud use Cloudinit (https://cloudinit.readthedocs.io/en/latest/)

While creating your instance you can provide Userdata, This text file can be used to provision users with a password (or even better an ssh key)

#cloud-config
hostname: <hostname>
repo_update: true
repo_upgrade: all
users:
  - name: <username2>
    sudo: ALL=(ALL) NOPASSWD:ALL  #Alows them to sudo
    shell: /bin/bash
    ssh-authorized-keys:
      - ssh-rsa <Key>
  - name: <username1>
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    ssh-authorized-keys:
      - ssh-rsa <Key>

This will update the packages (apt-get update && apt-get upgrade on debian based machines) and provision the users with keys.
Once the instance is booted the users will be able to login using ssh.

Hope this is enough information :wink:

I’ve just dealt with this today for a proof of concept. The trouble with userdata is it is intransigent. To update the users you need to update your launch configuration and recycle the instance.

What I have done for a proof of concept environment that doesn’t yet need CI tooling or central management is have the userdata install git and ansible, then clone a repo with users and ssh keys in a common role, and then apply the playbook locally. The playbook can then be run on demand, automatically or by a hook to update users without cycling the instance.

Ya, but in this case this wasn’t needed in this case.

I’m using Cloudinit to provision the ansible user + key (and some other minor stuff). After cloudinit is complete we use the Phone home (http://cloudinit.readthedocs.io/en/latest/topics/examples.html#call-a-url-when-finished) to call our Ansible master. That one will then push the correct playbook (with all user provisioning included) to the new box.

If someone leaves, we can update our ansible playbook and push the new changes to all boxes.

I’m interested in your POC, do you use ansible-pull to do this the normal ansible way but against the localhost?

This yaml text is valid to use in user data as long as you declare #cloud-config?

Can you also add bash scripts if you declare #!/bin/bash farther down too?

I havn’t tested it yet. But if i could guess running a bash script afterwards would not work.

A workaround for this would be a combination of write-file and run-command modules of cloud-init

First thing is to write the a file

write_files:
- content: |
#!/bin/bash

echo “Hello World”
owner: root:root
path: /somewhere
permissions: ‘0755’

then to invoke it with run-command.

Not tested, but quite confident this should work

Ah, ok. Quick skim of the docs leads me to believe it’s one or the other. I’ll also bet your method here works to use both. Thanks for sharing!