0

Below is the script which i use to create single user on server(I use another scpect script to copy below script on server and run script),But i want to create multiple user.

So,only once i will enter username on line 4 and it will create multiple user with increment user1,user2 and so on...


#!/bin/bash

#Script to Add User

tail /etc/passwd | cut -d: -f1
read -p 'Please Enter The Username To Add: ' name
echo "$name" > /tmp/userlist.txt
clear
echo -e "Hello $name\nYour Name Is Added To The List."
userfile=/tmp/userlist.txt
username=$(cat /tmp/userlist.txt | tr 'A-Z' 'a-z')
for user in $username
do
useradd $user -N -s /bin/bash
usermod -aG sudo $user
passwd $user
#echo "AllowUsers ${user}" >> /etc/ssh/sshd_config
sed -i "s/tui/tui $user/g" /etc/ssh/sshd_config
sed -i "s/$user.*$user/$user/g" /etc/ssh/sshd_config
sed -i "s/tui/tui $user/g" /etc/security/access.conf
done
echo "=================================="

echo "User $name Have Been Created."

echo "=================================="
tail /etc/passwd | cut -d: -f1
4
  • 3
    You would make your live a lot easier if you would use a configuration management system like Ansible, Chef, Puppet etc. – Gerald Schneider Sep 15 '20 at 13:18
  • Hey @Gerald, I know about the tools.Tools are being set up till then we have to use script. Kindly help – GauravG Sep 15 '20 at 13:19
  • You can install ansible on your laptop. It doesn't require "setup" that most admins can't handle in a few minutes. Bash is wonderful, but in this case it should be used to invoke ansible. – chicks Sep 15 '20 at 14:21
  • I answered this 2 weeks ago: serverfault.com/questions/1032355/… – Petey_Woof Sep 18 '20 at 16:03
0

You can try with this:


#Script to Add User

tail /etc/passwd | cut -d: -f1
read -p 'Please Enter The Username To Add: ' name

for COUNTER in {1..4};
do
        echo $name$COUNTER >> /tmp/userlist.txt
        echo -e "Hello $name$COUNTER\nYour Name Is Added To The List."
done

clear

userfile=/tmp/userlist.txt
username=$(cat /tmp/userlist.txt | tr 'A-Z' 'a-z')
for user in $username
do
useradd $user -N -s /bin/bash
usermod -aG sudo $user
passwd $user
#echo "AllowUsers ${user}" >> /etc/ssh/sshd_config
sed -i "s/tui/tui $user/g" /etc/ssh/sshd_config
sed -i "s/$user.*$user/$user/g" /etc/ssh/sshd_config
sed -i "s/tui/tui $user/g" /etc/security/access.conf
done
echo "=================================="

echo "User $name Have Been Created."

echo "=================================="
tail /etc/passwd | cut -d: -f1```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.