Creating a batch of new users and adding ssh keys using a script
Hi Guys,
Thanks for taking a look at this post, I was wondering if there was a simple was to take a range of usernames, creating a user with that user name and then adding their ssh keys into their individual .ssh/authorized_keys file. I guess what I need help with most is replacing the username in the below lines with each username, for example of 5 of the users could be: john, dave, mike, matt, justin mkdir /home/username/.ssh vim /home/username/.ssh/authorized_keys chmod 755 /home/username/.ssh chmod 644 /home/username/.ssh/authorized_keys I have about 10-15 users to add on a few servers and it would be very handy to be able to save some time! :) Many thanks! Matt |
Well it sounds like you'll want to write a simple loop in bash and use
Code:
cat Quote:
Let's say you have a file called usernames in the current directory with a name on each line. Then your loop could look something like this: Code:
#!/bin/bash |
Hi Isaac,
Thank you for your reply! Sorry I wasn't as clear as I could have been. When I say "and then adding their ssh keys into their individual .ssh/authorized_keys file" I mean that I already have their workstation public keys and would like to add those to the created users :) So I have individual files for each public key, If I named each public key file with the corresponding username and did something like this : cat /public_keys/$user >> /home/$user/.ssh/authorized_keys and the keys were located correctly would that work? Matt |
Hi again,
Sorry I forgot to mention that I made a mistake on the permissions you are correct, It should have been 640 for the authorized_keys file and 700 for .ssh Much appreciated thank you! |
Quote:
Code:
echo "123" > testfile1 |
Hi Isaac,
That's very handy thank you, I am hoping to start learning how to automate more so this helps a lot. Have a good day! |
All times are GMT -5. The time now is 11:16 PM. |