在多台服务器上使用相同用户名创建多个相同的用户

在多台服务器上使用相同用户名创建多个相同的用户

下面是我在服务器上用来创建单个用户的脚本(我使用另一个 spect 脚本在服务器上复制以下脚本并运行脚本),但我想创建多个用户。

因此,仅需在第 4 行输入一次用户名,它就会创建多个用户,增量为 user1、user2 等等...


#!/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

答案1

您可以尝试这个:


#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```

相关内容