Thursday, January 17, 2013

Linux - adding a user to multiple servers

When I am not working on VMWare infrastructure, I face administration tasks at OS level. Since I am writing the scripts, I will try to post some on the blog from time to time. Following, a simple way of adding a new user to multiple servers (I am not using dsh, rather a for loop):

for i in `cat server_list`; do ssh $i  "echo 'john:johnpass:30000:30000::/home/john:/bin/bash' | /usr/sbin/newusers;
sed -i '/AllowUsers/s/root/root\ john/' /etc/ssh/sshd_config;
sed -i '/AllowGroups/s/root/root\ john/' /etc/ssh/sshd_config;
service sshd reload;
sed -i -e '/root\tALL=(ALL)/a john\tALL=(ALL)\tALL' /etc/sudoers;
"; done

It adds user information (username, password,UID,GID,home,shell) using newuser command, then it adds user and group in sshd_config (if it is the case). The last sed adds the new user to sudoers file right after it matches a string with tab (\t) in it.

No comments: