批量添加用户脚本--Linux bash

脚本实例

批量用户添加

for 在读取文件时,任何空白字符都可以自动作为分隔符、while的按行读取使用的是换行符作为标记,所以在这使用while更好

majun@instance-zqtg07w6:~/bash_scripts$ vim useradd_while02.sh
majun@instance-zqtg07w6:~/bash_scripts$ vim users.txt
majun@instance-zqtg07w6:~/bash_scripts$ cat users.txt
username001 password001
username002 password002
username003 password003
username004 password004
usernmae005 paswword005
majun@instance-zqtg07w6:~/bash_scripts$ cat useradd_while02.sh
#!/bin/bash
while read LINES
do
        USERNAME=`echo $LINES|cut -f1 -d ' '`
        PASSWORD=`echo $LINES|cut -f2 -d ' '`
        echo -n "Username:$USERNAME  PASSWORD:$PASSWORD"
        echo
done<users.txt

majun@instance-zqtg07w6:~/bash_scripts$ bash useradd_while02.sh
Username:username001  PASSWORD:password001
Username:username002  PASSWORD:password002
Username:username003  PASSWORD:password003
Username:username004  PASSWORD:password004
Username:usernmae005  PASSWORD:paswword005
majun@instance-zqtg07w6:~/bash_scripts$

linux如何避免交互修改用户密码

如何一条命令修改linux密码

一般linux修改密码方式
echo "password" |passwd --stdin user
ubuntu 和debian 废弃了passwd --stdin的方式,采用chpasswd命令进行非交互修改密码
echo username:newpasswd | chpasswd
PS:初学linux不要用Ubuntu!

linux如何修改密码(ubuntu实例)

root@instance-zqtg07w6:/home/majun/bash_scripts# tail -n 4 /etc/shadow
debian-tor:*:18225:0:99999:7:::
majun:$6$G15Ql/K0$0Q5NKV/TJ1XpPNDJebwUHIUc72xb6E6JXunRwesKgiFFnks8D1dkGCDgUePOPDdSX3QzVs1GM.6BvELErulM.:18255:0:99999:7:::
username002:$6$EvBA/Qr/$uEExJboy63Ds9iZaJItJiKjq8jPijJoPRpx.Trxtq3twpz0vr4eNTisf6Xoqvp06nLaybGlmiDuYgS9jy3iAP/:18268:0:99999:7:::
username003:!:18268:0:99999:7:::
root@instance-zqtg07w6:/home/majun/bash_scripts# echo username003:newoasswd | chpasswd
root@instance-zqtg07w6:/home/majun/bash_scripts# tail -n 4 /etc/shadow
debian-tor:*:18225:0:99999:7:::
majun:$6$G15Ql/K0$0Q5NKV/TJ1XpPNDJebwUHIUc72xb6E6JXunRwesKgiFFnks8D1dkGCDgUePOPDdSX3QzVs1GM.6BvELErulM.:18255:0:99999:7:::
username002:$6$EvBA/Qr/$uEExJboy63Ds9iZaJItJiKjq8jPijJoPRpx.Trxtq3twpz0vr4eNTisf6Xoqvp06nLaybGlmiDuYgS9jy3iAP/:18268:0:99999:7:::
username003:$6$fpqO7Y/Q$SgIFXadP91kacJWOB5vW7EtXFUvUIaJQP7ewwA2x24JIGg7dH2zn6xsUy9rJbu/MFp.pet.cI73StXpBX43f20:18268:0:99999:7:::
root@instance-zqtg07w6:/home/majun/bash_scripts#
root@instance-zqtg07w6:/home/majun/bash_scripts# vim useradd_while03.sh
root@instance-zqtg07w6:/home/majun/bash_scripts# cat useradd_while03.sh
#!/bin/bash
while read LINES
do
        USERNAME=`echo $LINES|cut -f1 -d ' '`
        PASSWORD=`echo $LINES|cut -f2 -d ' '`
        echo -n "Username:$USERNAME  PASSWORD:$PASSWORD"
        echo
        useradd $USERNAME
        #echo $PASSWORD | passwd --stdin $USERNAME#normal linux
        echo $USERNAME:$PASSWORD | chpasswd #ubuntu-linux
done<users.txt

root@instance-zqtg07w6:/home/majun/bash_scripts# bash useradd_while03.sh
Username:username002  PASSWORD:password002
Username:username003  PASSWORD:password003
root@instance-zqtg07w6:/home/majun/bash_scripts# tail -n 3 /etc/shadow
majun:$6$G15Ql/K0$0Q5NKV/TJ1XpPNDJebrwUHIUc72xb6E6JXunRwesKgiFFnks8D1dkGCDgUePOPDdSX3QzVs1GM.6BvELErulM.:18255:0:99999:7:::
username002:$6$mA6G6nLK$9sxTVMumhNhaf36tZgtn9l.1t0ROaDqHOisSgpe6lgSEVNZmfo6Ot/MZekfeeSJc39FkJDZyMmlThI43evVQC/:18268:0:99999:7:::
username003:$6$9742a.70$SQ6HxUHcSWEK2Xt2St6.GdoSNqtnI3SfX9vqzpjbRqL7T4w6YwRpI/JZoLdbSWK8VdkSWsts3MSQRV98IY2F51:18268:0:99999:7:::
root@instance-zqtg07w6:/home/majun/bash_scripts#

再运行一次就会,报错,用户存在但同时修改了密码。

root@instance-zqtg07w6:/home/majun/bash_scripts# bash useradd_while03.sh
Username:username002  PASSWORD:password002
useradd: user 'username002' already exists
Username:username003  PASSWORD:password003
useradd: user 'username003' already exists
root@instance-zqtg07w6:/home/majun/bash_scripts#

添加检测用户是否存在

root@instance-zqtg07w6:/home/majun/bash_scripts# vim useradd_while04.sh
root@instance-zqtg07w6:/home/majun/bash_scripts# cat useradd_while04.sh
#!/bin/bash
while read LINES
do
        USERNAME=`echo $LINES|cut -f1 -d ' '`
        PASSWORD=`echo $LINES|cut -f2 -d ' '`
        echo -n "Username:$USERNAME  PASSWORD:$PASSWORD"
        echo
        useradd $USERNAME
        if [ $? -eq 0 ];then
                #echo $PASSWORD | passwd --stdin $USERNAME#normal linux
                echo $USERNAME:$PASSWORD | chpasswd #ubuntu-linux
        else
                echo "$USERNAME is exist , skip change password!"
        fi
done<users.txt

root@instance-zqtg07w6:/home/majun/bash_scripts# bash useradd_while04.sh
Username:username002  PASSWORD:password002
useradd: user 'username002' already exists
username002 is exist , skip change password!
Username:username003  PASSWORD:password003
useradd: user 'username003' already exists
username003 is exist , skip change password!
root@instance-zqtg07w6:/home/majun/bash_scripts#

所有的练习脚本都在:
https://github.com/SaltNego/Learn_linux_bash

发布了61 篇原创文章 · 获赞 22 · 访问量 4254

猜你喜欢

转载自blog.csdn.net/yiqiushi4748/article/details/103870634