LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2014, 05:33 AM   #1
mattjjs
LQ Newbie
 
Registered: Jul 2012
Posts: 16

Rep: Reputation: Disabled
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
 
Old 02-13-2014, 06:12 AM   #2
Isaac Velando
LQ Newbie
 
Registered: Feb 2014
Location: Texas
Distribution: Arch, Ubuntu Server, CentOS
Posts: 29

Rep: Reputation: 21
Well it sounds like you'll want to write a simple loop in bash and use
Code:
cat
to replace the vim steps. Cat prints the contents of a file and we can redirect that to write or append to another file. Also before I go on, is there a specific reason for 755 as the permissions on the .ssh directory? Generally you should be using 700 since only that user should have access to sensitive information like that. I'm also confused by what you mean when you say

Quote:
and then adding their ssh keys into their individual .ssh/authorized_keys file
are you saying you want a user's own public ssh key in their own authorized_keys file, or are you trying to have some other public keys in each user's authorized_keys? I'm just a little confused by the purpose of the former case. I'll leave the explanation generic and you can adjust it to your needs in any event.

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

for user in $( cat ./usernames ); do

mkdir -p /home/$user/.ssh
cat /path/to/pubkey >> /home/$user/.ssh/authorized_keys
chmod 700 /home/$user/.ssh
chmod 644 /home/$user/.ssh/authorized_keys
chown -R $user.$user /home/$user/.ssh

done
Note that you'll probably want the last line to set the ownership. Just replace /path/to/pubkey with whatever public ssh keys you want in the authorized_keys file. The >> appends output to the target file which is probably desired in this case, but note that there is also > which instead would overwrite any contents already in the target file.

Last edited by Isaac Velando; 02-13-2014 at 06:14 AM.
 
1 members found this post helpful.
Old 02-13-2014, 06:23 AM   #3
mattjjs
LQ Newbie
 
Registered: Jul 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-13-2014, 06:36 AM   #4
mattjjs
LQ Newbie
 
Registered: Jul 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
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!
 
Old 02-13-2014, 10:08 AM   #5
Isaac Velando
LQ Newbie
 
Registered: Feb 2014
Location: Texas
Distribution: Arch, Ubuntu Server, CentOS
Posts: 29

Rep: Reputation: 21
Quote:
Originally Posted by mattjjs View Post
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
That should work. In case you're shaky on cat and redirection, you might want to experiment with output redirection a bit to better acquaint yourself with it - it's really one of the 'bread and butter' tools for Linux automation. This might help illustrate how this stuff works; execute these line by line and see if it makes sense:

Code:
echo "123" > testfile1
echo "456" >> testfile1
cat testfile1 # 123 should be on the first line, 456 should be on the second because it was appended

cat testfile1 > testfile2 # Print the contents of testfile1 and redirect this output to a new file, testfile2
cat testfile2

cat testfile1 >> testfile2 # Append the contents of testfile1 to testfile2
cat testfile2

echo "1" > testfile2
cat testfile2 # Since we didn't append, the "1" overwrites the contents of testfile2
Hope this helps.
 
1 members found this post helpful.
Old 02-13-2014, 10:46 AM   #6
mattjjs
LQ Newbie
 
Registered: Jul 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Creating rsa keys for ssh - Having trouble- Still asks for password completelinuxnube Linux - Newbie 6 11-20-2012 08:18 AM
Bash script for adding users is running but not adding users Gren Programming 2 04-27-2012 03:53 AM
Can you verify my syntax for creating multiple users in a batch file? grayceworks Linux - Newbie 5 03-03-2011 02:04 AM
[Solved]Creating same SSH keys on different distros but same machine milomak Linux - Networking 4 02-18-2010 10:51 AM
[SOLVED] problem adding ssh keys to skip password prompt vikas027 Linux - Software 27 09-26-2008 03:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration