シェルスクリプトを使用してLinuxにユーザーを追加する方法

シェルスクリプトを使用してLinuxにユーザーを追加する方法

私が作成する必要があるシェルスクリプトは新しいユーザーを作成し、自動的にグループに割り当てる必要があります。これは私のコードです。

echo -n "Enter the username: "
read text
useradd $text

新しいユーザーを追加するためのシェルスクリプトを入手できました(/etc/passwdエントリを確認しました)。しかし、これまでこれを試しましたが、新しく作成されたユーザー(ユーザーが入力したもの)のパスワードを取得できませんでした。誰かが新しく作成したユーザーにパスワードを割り当てるのに役立つことができれば、大きな助けになります。

ベストアンサー1

「man 1 passwd」の出力:

--stdin
      This option is used to indicate that passwd should read the new
      password from standard input, which can be a pipe.

したがって、あなたの質問に答えるには、次のスクリプトを使用してください。

echo -n "Enter the username: "
read username

echo -n "Enter the password: "
read -s password

adduser "$username"
echo "$password" | passwd "$username" --stdin

read -s入力時にパスワードが表示されないようにパスワードを使用しています。

編集する:Debian / Ubuntuユーザーには-stdin機能しません。passwd使用する代わりにchpasswd

  echo $username:$password | chpasswd

おすすめ記事