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 10

1

2

3

4

5

6

7

8

9

10

[Email protected] ~]#


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 stu01

Useradd stu02

Useradd stu03

Useradd stu04

Useradd stu05

Useradd stu06

Useradd stu07

Useradd stu08

Useradd stu09

Useradd STU10

[Email protected] ~]#


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] ~]# TAIL-10/ETC/PASSWD

Stu01:x:503:503::/home/stu01:/bin/bash

Stu02:x:504:504::/home/stu02:/bin/bash

Stu03:x:505:505::/home/stu03:/bin/bash

Stu04:x:506:506::/home/stu04:/bin/bash

Stu05:x:507:507::/home/stu05:/bin/bash

Stu06:x:508:508::/home/stu06:/bin/bash

Stu07:x:509:509::/home/stu07:/bin/bash

Stu08:x:510:510::/home/stu08:/bin/bash

Stu09:x:511:511::/home/stu09:/bin/bash

Stu10:x:512:512::/home/stu10:/bin/bash

[Email protected] ~]#


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 stu01

passwd stu02

passwd stu03

passwd stu04

passwd stu05

passwd stu06

passwd stu07

passwd stu08

passwd stu09

passwd STU10

[Email protected] ~]#


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))

2492

[[email protected] ~]# echo $ ((RANDOM))

15380

[[email protected] ~]# echo $ ((RANDOM))

20658

[[email protected] ~]# echo $ ((RANDOM))

24952

[Email protected] ~]#

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 ' |bash

Changing 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:20103

stu02:5338

stu03:12149

stu04:15405

stu05:13777

stu06:27532

stu07:28049

stu08:2620

stu09:17935

stu10:22851

[Email protected] ~]#



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-R stu\1#g ' |bash

[Email protected] ~]# seq-w 10|sed-r "s# (. *) #useradd stu\1#g" |bash

[Email protected] ~]# TAIL-10/ETC/PASSWD

Stu01:x:502:502::/home/stu01:/bin/bash

Stu02:x:503:503::/home/stu02:/bin/bash

Stu03:x:504:504::/home/stu03:/bin/bash

Stu04:x:505:505::/home/stu04:/bin/bash

Stu05:x:506:506::/home/stu05:/bin/bash

Stu06:x:507:507::/home/stu06:/bin/bash

Stu07:x:508:508::/home/stu07:/bin/bash

Stu08:x:509:509::/home/stu08:/bin/bash

Stu09:x:510:510::/home/stu09:/bin/bash

Stu10:x:511:511::/home/stu10:/bin/bash

[Email protected] ~]#

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:7119

stu02:27272

stu03:16673

stu04:24743

stu05:10044

stu06:4649

stu07:19767

stu08:29380

stu09:21922

stu10:13488

[Email protected] ~]#

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-stu01

[Email protected] ~]$ su-stu02

Password:

[Email protected] ~]$ WhoAmI

Stu02

[Email protected] ~]$


This article is from the "eyes engraved with your Smile" blog, please be sure to keep this source http://dengaosky.blog.51cto.com/9215128/1854534

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

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.