批量创建linux用户?centos7创建新用户
linux一次创建100个用户怎么创建用命令
1、Ubuntu中创建新用户需要先获取root管理员权限,因此先使用sudo su命令获取最高权限。注意获取root权限需要输入你自己的用户密码。
2、批量创建100个以“duanzhang”为用户名前缀的用户,命令为:seq-w 100|sed-r"s#(.*)#useraddduanzhang\1#g"|bash。
3、查看新用户是否创建成功,命令为:tail-100/etc/passwd。这条命令的意思是输出passwd文件的后100行。
4、将要修改的用户名和密码写入pass.log文件中,命令为:echoduanzhang{01..20}:$((RANDOM))|tr"""\n">pass.log。命令里的RANDOM表示生成随机数,以作为随机密码。
5、查看生成的pass.log文件的内容,以确认是否成功写入文件。从图中可以看到生成了100行数据,每一行使用冒号隔开用户名及其对应的密码,可以看到这100个密码并无规则,是随机的。
6、执行chpasswd命令:chpasswd<pass.log。其中chpasswd命令在Ubuntu中用于批量更改用户密码,它需要先读取我们前一步生成的密码文件pass.log,然后根据该文件内容修改密码。
7、根据pass.log文件里记录的这100个用户的密码逐个查看对应的随机密码是否有效。使用su命令切换到新用户并输入pass.log里记录的对应的密码,成功登录,可见用户创建成功。
...用户名为exam1~exam30,用户密码统一为gdlclinux。
写好了,还在测试,测试好了就给你。
写好了,通过测试了。
########################################################################
#注:本脚本中使用了函数模块,以方便修改和调试
#************************************************
# 2011年12月10日初稿
#************************************************
# 2011年12月11日测试问题:密码修改函数尚未实现
#************************************************
# 2011年12月11日定稿密码修改函数实现
#************************************************
# by bmw88661@126.com 12.11.2011
########################################################################
#设置变量
i=1
username=exam
password=gdlclinux
rmso=$(tput rmso)
smso=$(tput smso)
#此处可添加要求输入用户名和密码的项
#错误判断函数
SEA()
{
location=$( which useradd)#查找useradd的绝对路径
if [\("$location"=""\)-o\(-s"$location"\) ]#判断是否找到useradd文件,或着找到的文件为空
then
echo"Useradd was not found,job terminated$smso unsuccessfully$rmso!"
exit
else
if [-x"$location" ]#若useradd文件存在且不为空,判断是否具有可执行权限
then
echo"Command useradd doesn't have excution(x) mode"
exit
else#无法确定问题原因
echo"Script terminated by some unknown problem,Please check it!"
exit
fi
fi
}
#提示信息函数
casemsg()
{
#判断passwd执行的返回值,并进入case分支
case"$y" in
0) echo"Set password for$smso$username$i$rmso successfully"
;;
1) echo"Permission denied when set password for$smso$username$i$rmso"
exit 1;;
2) echo"Invalid combination of options for command passwd"
exit 2;;
3) echo"Unexpected failure, nothing done"
exit 3;;
4) echo"Unexpected failure, passwd file missing"
exit 4;;
5) echo"Passwd file busy, try again"
exit 5;;
6) echo"Invalid argument to option"
exit 6;;
esac
}
#用户创建函数
usercreate()
{
useradd-m$username$i#如果需要其他功能,请参照useradd帮助手册修改此行,如:设置uid
x=$?
}
#密码修改函数
#这里如果你的系统里有chpasswd这个批修改工具的话,就不用这么麻烦了。
MOVpassword()
{
( echo"$password";sleep 1;echo"$password")|passwd$username$i#若useradd执行成功,则调用passwd设置密码
y=$?
}
#主循环
while ["$i"-ge 1-a"$i"-le 30 ]
do
usercreate#调用usercreate函数创建用户
if ["$x"-eq 0 ]#判断useradd是否执行成功
then
echo"User$smso$username$i$rmso have been create successfully!"
MOVpassword#调用MOVpassword修改密码
casemsg#调用casemsg函数
if ["$y"= 0 ]#如果MOVpassword的返回值为0,则将i加1
then
let i++
fi
else
SEA#调用错误判断函数
fi
done
unset i x y location rmso smso username password#释放所有设定的变量
如何使用azure powershell批量创建用户
之前给大家介绍很多关于Windows Azure的内容,基本上都是通过Windows Azure Management Portal来创建的。这种创建方式虽然直观简单,但是如果IT管理员需要同时创建1000台Azure服务的话,工作的代价是非常巨大的。
其实我们可以通过Windows Azure PowerShell,通过命令行的方式来管理Windows Azure。这样在进行诸如批量创建Azure Virtual Machine的操作会变得非常简单。接下来就开始本章内容。
如果你是第一次运行Azure PowerShell,请按照之前的文章,下载Azure PowerShell Settings文件并上传至云端。
了解Azure的帮助命名
1.我们首先运行命令: help azure,来看看Windows Azure提供了哪些命令行。我只截取了部分内容,如下图:
在上图中我们可以看到,Azure PowerShell提供了非常多的命令。比如我们之前讲解过的Add-AzureVhd等命令。
2.我们再运行命令: Get-AzureVMImage,这条命令是列出所有的Azure Virtual Machine镜像,其中包含Windows和 Linux的。我只截取了部分内容,如下图:
开始创建Azure虚拟机
1.指定当前的存储
Set-AzureSubscription-SubscriptionName'<SubscriptionName>'-CurrentStorageAccount'<StorageAccount>'
比如我想指定订阅为'Windows Azure MSDN- Visual Studio Ultimate',存储账户为'leivms'。
执行以下命令:
Set-AzureSubscription-SubscriptionName'Windows Azure MSDN- Visual Studio Ultimate'-CurrentStorageAccount'leivms'
2.如果我想通过Azure PowerShell新建Virtual Machine
-VM Name为 LeiAzureVM
-VM Size为 ExtraSmall
-VM Image为 Windows Server 2012 Datacenter
-Windows用户名为 leizhang,密码为 Pass@word1
-DNS Name为 LeiAzure
-高可用组为 AvbSet
-数据中心选择 East Asia
3.先要设置镜像为Windows Server 2012 Datacenter,
$imageList= Get-AzureVMImage `
| where{$_.ImageName-eq"a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-201407.01-en.us-127GB.vhd"}
$image=$imageList[0]
执行结果如下:
上图的$image命令可以显示我们需要的Windows Server 2012 DataCenter镜像的相关信息
如果我想模糊查询AzureImage的话,可以通过星号(*)通配符,来模糊查询。
比如笔者只想查询Windows Server 2012 Datacenter的镜像,PowerShell如下:
$imageList= Get-AzureVMImage `
| where{$_.ImageName-like"*Windows-Server-2012-Datacenter*"}
$image=$imageList[0]
4.创建虚拟机
New-AzureVMConfig-Name'LeiAzureVM'-InstanceSize'ExtraSmall'-ImageName$image.ImageName-AvailabilitySetName'AvbSet' `| Add-AzureProvisioningConfig-Windows-AdminUsername'leizhang'-Password'Pass@word1' `| New-AzureVM-ServiceName'LeiAzure'-Location'East Asia'
执行结果如下图:
5.查询执行结果
我们在PowerShell中看到Create Succeeded,其实创建Azure VM的过程是异步的。这时候我们查看Azure Management Portal,发现VM正在被创建。如下图:
我们还可以通过Management Portal看到创建成功的虚拟机
OK,我们已经创建完第一台虚拟机了,如果需要创建第2台虚拟机LeiAzureVM002加入到之前创建的DNS: LeiAzure.cloudapp.net,
并且需要加入同一个高可用组'AvbSet',这句PowerShell语句按照下面执行:
New-AzureVMConfig-Name'LeiAzureVM002'-InstanceSize'ExtraSmall'-ImageName$image.ImageName-AvailabilitySetName'AvbSet' `| Add-AzureProvisioningConfig-Windows-AdminUsername'leizhang'-Password'Pass@word1' `| New-AzureVM-ServiceName'LeiAzure'
执行结果如下:
创建简单的Linux虚拟机
如果我想创建一个简单的Linux虚拟机,OS为CentOS。
-VM Name为 LeiLinuxVM001
-VM Size为 Medium
-VM Image为
CentOS 6.4
-Windows用户名为 adminus er,密码为Abc@123456
-DNS Name为 LeiLinuxVM001
-高可用组为 AvbSet
-数据中心选择 East Asia
1.获得CentOS虚拟机镜像,通过模糊查询获得CentOS镜像
$imageList= Get-AzureVMImage `
| where{$_.ImageName-like"*CentOS*"}
$image=$imageList[0]
2.创建虚拟机命令:
New-AzureVMConfig-Name'LeiLinuxVM001'-InstanceSize Medium-ImageName$image.ImageName-AvailabilitySetName'AvbSet'| Add-AzureProvisioningConfig-Linux-LinuxUser'adminuser'-Password'Abc@123456'| New-AzureVM-ServiceName'LeiLinuxVM001'-Location'East Asia'
执行结果
观察执行结果: