Shell script Real-Combat notes (9)--linux automatic batch add users

Source: Internet
Author: User
Tags crypt lenovo



Objective:



Add Linux user account, this is relatively simple, in the face of the cluster, many machines, how do we do and implement? This short article, briefly explain some ideas, as far as possible to involve some of the surrounding knowledge points. It's not just the ops people who are faced with this problem, but sometimes it's a Cluster service or software (such as a Hadoop cluster) that is based on a Linux platform.



Application Scenarios:
is the CentOS 6.4 as a demonstration system, the other systems are similar, there are differences, and to combat the practice, a step to tell the next process.



*) Practical Walkthrough
Check the use and parameter options for Useradd
Useradd--help


-d, --home-dir HOME_DIR home directory of the new account
-m, --create-home create the user‘s home directory
-p, --password PASSWORD encrypted password of the new account
-s, --shell SHELL login shell of the new account


Option-P can specify the password,-d specifies the home directory,-s specifies the user login shell



Attempt to add user name: ThinkPad, Password: Lenovo
Useradd thinkpad-p lenovo-s/bin/bash
Su ThinkPad
Enter Password: Lenovo



The first Su ThinkPad succeeds because the current account is root and Su ThinkPad does not require password verification
The second Su ThinkPad fails, stating that the password is not Lenovo
Why is it? The reason, such as parameter description, the parameter specified by the password is the encrypted password string, that the specific use of the encryption algorithm?



We can go further through the command manual.
Mans Useradd


-p, --passwordPASSWORD
The encrypted password is like the return value of crypt (3). The password is disabled by default.


Crypt is a system function and we continue to check
Mans 3 Crypt


NAME
  crypt, crypt_r - password and data encryption

SYNOPSIS
  #define _XOPEN_SOURCE /* See feature_test_macros(7) */
  #include <unistd.h>

  char *crypt(const char *key, const char *salt);

  #define _GNU_SOURCE /* See feature_test_macros(7) */
  #include <crypt.h>

  char *crypt_r(const char *key, const char *salt,
  struct crypt_data *data);

DESCRIPTION
  key is a user‘s typed password.
  salt is a two-character string chosen from the set [a–zA–Z0–9./].


The key and salt (two bytes) settings are important, so we continue to write our own password generator
Writing a file Crypt_passwd.cpp


#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>

int main ()
{
     const char * key = "lenovo"; // key is the password you want to set
     const char * salt = "xx"; // salt is two bytes, can be freely taken

Char * result = crypt (key, salt);
Printf ("password:% s \ n", result);

Return 0;
}


Compiling crypt_passwd.cpp



g++ Crypt_passwd.cpp-lcrypt-o crypt_passwd



Input XX8FWQTT5IVRQ, that is, Lenovo corresponds to the encrypted string
Let's try to guess whether the previous guesses were correct



Useradd thinkpad-p xx8fwqtt5ivrq-m-d/home/thinkpad-s/bin/bash
Su ThinkPad
Enter Password: Lenovo

Now it's done, oh yeah, isn't it simple



How do I add an account automatically to the cluster? Can be achieved by using the password-free login method.



Another way:



In addition to specifying the-p parameter in Useradd, you can also borrow here document to implement
Write the following script


#! / bin / bash

# Add locked user
useradd thinkpad -m -d / home / thinkpad -s / bin / bash

# Use the here document to interact and set a password, (enter the password twice because passwd requires repeated authentication)
passwd thinkpad <<-EOF
Lenovo
Lenovo
EOF 


With the useradd of a password that is not specified, the account created is locked and requires the administrator to give it a new password with passwd, and this way, by using here document, eliminates the need to enter the password manually.





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.