이 사용자 생성 스크립트에는 비밀번호가 설정되지 않았습니다.

이 사용자 생성 스크립트에는 비밀번호가 설정되지 않았습니다.

다음 스크립트는 Ubuntu 시스템에서 사용자를 생성하고 임의의 비밀번호를 생성해야 합니다.

#!/bin/bash

#Script should execute with sudo/root access
if [[ "${UID}" -ne 0 ]]
then
    echo "Please run with sudo or root"
    exit 1
fi

#User should provide atleast one argument as username else guide him
if [[ "${#}" -lt 1 ]]
then
    echo "Usage: ${0} USER_NAME [COMMENT]..."
    echo "Create a user with name USER_NAME and comments field of COMMENT"
    exit 1
fi

#Store 1st argument as user name
USER_NAME="${1}"

#In case of more than one argument, store it as account comments
shift
COMMENT="${@}"

#Create a password
 PASSWORD=$(date +%s%N)

#Create the user
useradd -c "$COMMENT" -m $USER_NAME

#Check if user is successfully created or not
if [[ $? -ne 0 ]]
then
    echo "The Account could not be created"
    exit 1
fi

#Set the password for the user
echo $PASSWORD | passwd --stdin $USER_NAME

#Check if password is successfully set or not
if [[ $? -ne 0 ]]
then
    echo "The password could not be set"
    exit 1
fi

#Force password change on first login
passwd -e $USER_NAME

#Display username, password and the host where the user was created
echo
echo "Username: $USER_NAME"
echo
echo "Password: $PASSWORD"
echo
echo "Hostname: $(hostname)"

산출:

root@Sengh:/home/sengh/scripts# bash user_create.sh singh this is a
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]

Options:
  -a, --all                     report password status on all accounts
  -d, --delete                  delete the password for the named account
  -e, --expire                  force expire the password for the named account
  -h, --help                    display this help message and exit
  -k, --keep-tokens             change password only if expired
  -i, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -l, --lock                    lock the password of the named account
  -n, --mindays MIN_DAYS        set minimum number of days before password
                                change to MIN_DAYS
  -q, --quiet                   quiet mode
  -r, --repository REPOSITORY   change password in REPOSITORY repository
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix
  -S, --status                  report password status on the named account
  -u, --unlock                  unlock the password of the named account
  -w, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS
  -x, --maxdays MAX_DAYS        set maximum number of days before password
                                change to MAX_DAYS

The password could not be set

답변1

작동하지 않는 이유는 여러 의견에서 이미 지적했듯이 명령 출력에서 ​​분명합니다.

passwd: unrecognized option '--stdin'

유튜브로 성공했다고 하더군요. 음, YouTube(또는 다른 튜토리얼)에서는 많은 기능이 작동할 수 있지만 배포판, 도구 버전 등이 다르기 때문에 작동하지 않을 수 있습니다. 귀하의 특정한 경우에는 다음을 수행할 수 있습니다.쉘 스크립트에서 passwd 명령 사용LinuxOPsys 튜토리얼:

참고: 오류 메시지가 표시되면 passwd: unrecognized option '--stdin'시스템의 passwd 명령이 이 --stdin옵션을 지원하지 않는다는 의미입니다. 이 옵션은 일반적으로 CentOS Stream 또는 RHEL과 같은 일부 배포판에서 사용할 수 있지만 Ubuntu와 같은 다른 배포판에는 없을 수도 있습니다.

따라서 YouTube 동영상에 나오는 사람은 아마도 이 옵션을 지원하는 배포판 중 하나를 사용하고 있을 것입니다. 그러나 배포판에서는 이 옵션이 분명히아니요쓸 수 있는.

대부분의 구현이 이 옵션을 지원하지 않는 이유는 무엇입니까 passwd? 아래에서 이 질문에 대한 답을 찾을 수 있습니다.배쉬 FAQ페이지:

Traditional은 passwd(1)표준 입력에서 읽지 않습니다. 이것은 고의로. 이는 귀하를 보호하기 위한 것입니다. 비밀번호는 프로그램에 넣거나 생성하기 위한 것이 아닙니다.통과프로그램. 정상적인 두뇌를 가진 실제 인간의 손가락으로만 입력할 수 있으며 어디에도 기록되지 않습니다. 따라서 진행하기 전에 passwd(1)작성자의 관점과 귀하가해서는 안 된다스크립트 입력을 작성하려고 합니다 passwd(1).

위의 경고를 읽은 후에도 여전히 스크립트로 자동화를 고집한다면 동일한 LinuxOPsys 튜토리얼에서 답을 찾을 수 있습니다. chpasswd 사용@NicolasFormichella가 제안한 대로 부분적으로답변.

댓글을 달았습니다.이 답변의 경우:

비밀번호를 설정해야 하는 사용자를 언급하는 대신 변수를 사용하여 어떤 사용자를 생성해야 하는지 사용자로부터 읽어오길 원합니다.

글쎄, 여전히 변수를 사용할 수 있습니다. 바꾸다

echo $PASSWORD | passwd --stdin $USER_NAME

사용:

echo "$USER_NAME:$PASSWORD" | chpasswd

하지만 일반적으로 여러분이 접하는 어떤 YouTube 동영상도 신뢰해서는 안 됩니다.

답변2

당신이 원하는 것은chpasswd

man 8 chpasswd상태:

chpasswd - 배치 모드에서 비밀번호 업데이트

사용 예

echo "example:example" | chpasswd

example사용자의 비밀번호를 변경합니다

관련 정보