Linux creates multiple users and assigns random passwords without using circular statements

Source: Internet
Author: User
Tags echo command stdin

Linux creates multiple users and assigns random passwords that require no loop statements.


Method One

Use basic commands Useradd and passwd to add users and add passwords.

(Note: Random generation of Numbers (3-5). )

Create a user

[Email protected] ~]# Useradd stu01

Creating multiple users is to repeat the above commands multiple times, but one input can be cumbersome, but we could make a sequence of the strings, and then replace the sequence with the above format using the alternate method.

[[email protected] ~]# seq 1012345678910

Then we use the SED command to replace the above sequence with the command we need.

[Email protected] ~]# seq-w 10|sed-r ' s# (. *) #useradd stu\1#g ' useradd stu01useradd stu02useradd stu03useradd Stu04userad D stu05useradd stu06useradd stu07useradd stu08useradd stu09useradd stu10

This allows us to create multiple users at once, simply by streaming the above content to bash to execute the command.

[Email protected]/]# seq-w 10|sed-r "s# (. *) #useradd stu\1#g" |bash[[email protected]/]# [[email protected]/]# [[email] Protected]/]# tail-10/etc/passwdstu01:x:502:502::/home/stu01:/bin/bashstu02:x:503:503::/home/stu02:/bin/ bashstu03:x:504:505::/home/stu03:/bin/bashstu04:x:505:506::/home/stu04:/bin/bashstu05:x:506:507::/home/stu05:/ bin/bashstu06:x:507:508::/home/stu06:/bin/bashstu07:x:508:509::/home/stu07:/bin/bashstu08:x:509:510::/home/ Stu08:/bin/bashstu09:x:510:511::/home/stu09:/bin/bashstu10:x:511:512::/home/stu10:/bin/bash


Add a random password for these 10 users

The basic idea is to create a sequence and replace it with the command we need, just like above.

[Email protected] ~]# seq-w 10|sed-r ' s# (. *) #passwd stu\1#g ' passwd stu01passwd stu02passwd stu03passwd stu04passwd stu0 5PASSWD stu06passwd stu07passwd stu08passwd stu09passwd stu10

It's a hassle to enter a password when you get here, but passwd has a parameter--stdin can directly use the echo command to pass in the password, the problem is to create a random password, here to use a random variable parameter

[[email protected] ~]# echo $ ((random)) 20998[[email protected] ~]# echo $ ((random)) 4479[[email protected] ~]# echo $ ((RAND OM)) 17310[[email protected] ~]# echo $ ((RANDOM)) 23677

It generates 3 to 5 random numbers that we can pass through the pipeline to passwd and then change the password.

But after the password is not seen, set equal to no settings, so we use the Append redirect to the password into a folder, so that you can add both a random password and can see how much password.

[[email protected] /]# seq -w 10|sed -r  ' s# (. *) #jack = "$ ((RANDOM))"  ; echo  $jack |passwd --stdin stu\1;echo  "stu\1: $jack" >>/mima.log#g ' |bashChanging  password for user stu01.passwd: all authentication tokens updated  Successfully. changing password for user stu02.passwd: all authentication tokens  Updated successfully. changing password for user stu03.passwd: all authentication tokens  Updated successfully. changing password for user stu04.passwd: all authentication tokens  Updated successfully. changing password for user stu05.passwd: all authentication tokens  Updated successfully. changing password for user stu06.passwd: all authentication tokens  Updated successfully. changing password for user stu07.passwd: all authentication tokens  Updated successfully. changing password for user stu08.passwd: all authentication tokens  Updated successfully. changing password for user stu09.passwd: all authentication tokens  Updated successfully. changing password for user stu10.passwd: all authentication tokens  Updated successfully. [[email protected] /]# tail -10 /mima.log stu01:24794stu02:11164stu03:12613stu04 : 17306stu05:28956stu06:27385stu07:4530stu08:23984stu09:2428stu10:18143


Method Two
To change a user's password using CHPASSWD

CHPASSWD is a batch change user password command, it is to read a file first, then according to the contents of the file, and then change the password, the file content format is as follows:
Username:pass WORD
The left side of the colon is the user name, and the right is the password to modify.

Create 10 users first

[Email protected]/]# seq-w 10|sed-r ' s# (. *) #userdel stu\1#g ' |bash[[email protected]/]# [[email protected]/]# [[Email Protected]/]# tail-10/etc/passwdstu01:x:502:502::/home/stu01:/bin/bashstu02:x:503:503::/home/stu02:/bin/ bashstu03:x:504:505::/home/stu03:/bin/bashstu04:x:505:506::/home/stu04:/bin/bashstu05:x:506:507::/home/stu05:/ bin/bashstu06:x:507:508::/home/stu06:/bin/bashstu07:x:508:509::/home/stu07:/bin/bashstu08:x:509:510::/home/ Stu08:/bin/bashstu09:x:510:511::/home/stu09:/bin/bashstu10:x:511:512::/home/stu10:/bin/bash

Then write the user name and password to be modified in the file

[[email protected]/]# echo stu{01..10}:$ ((RANDOM)) |tr "" \ n ">mima.log [[email protected]/]# Cat/mima.log stu01:29 906stu02:8080stu03:23509stu04:5472stu05:21851stu06:20113stu07:7789stu08:3386stu09:2396stu10:11108

The TR command above is to change the preceding space to a newline character so that it does not appear on a single line because CHPASSWD is required for the file content format.


Then we execute the CHPASSWD command and test whether we can enter.

[Email protected]/]# Chpasswd<mima.log [[email protected]/]# Su-stu01welcome to M Y computer, with a good day! [Email protected] ~]$ su-stu02password:welcome to my computer, with a good day!


The above content is organized from the Old Boys education 23 "Create multiple users, do not use circular statements", students work sharing.

Objective to use basic commands skillfully to achieve the purpose of the exercise.

Reference Example:

http://user.qzone.qq.com/49000448/blog/1422183723

This article is from the "Rookie Linux History" blog, make sure to keep this source http://jackdady.blog.51cto.com/8965949/1661781

Linux creates multiple users and assigns random passwords without using circular statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.