現在のユーザーがログイン/アウトしたときにスクリプトをどのように実行できますか?

現在のユーザーがログイン/アウトしたときにスクリプトをどのように実行できますか?

私はラズベリーパイを持っています。起動するたびに自動的にログインするユーザーpiがあります。 piの名前をmyuserに変更する必要があります。これを行うには、次の手順に従います。

  1. sudo passwd root -> rootユーザーにパスワードを割り当てる
  2. sudoの再起動
  3. rootとしてログイン
  4. コマンドの実行usermod -l myuser pi usermod -m -d /home/myuser myuser
  5. sudoの再起動
  6. myuserとしてログイン
  7. ファイルを編集/etc/sudoersし、piをmyuserに変更します。

上記の手順に従って、piユーザーをmyuserに変更します。

これに対してスクリプトを作成する必要があります。しかし、私が直面している問題は、再起動してroot / piとしてログインしている間にスクリプトを実行し続ける方法です。たとえば、次のコマンドを使用してrootとしてログインし、piからログアウトします。

sudo pkill -u pi

これにより、Pyからログアウトし、ユーザー名(root)とパスワードを入力してrootとしてログインできるようになると、ログイン画面が表示されます。しかし、rootとしてログインした後にコマンドを実行できるようにスクリプトを実行し続けるにはどうすればよいですかusermod -l myuser pi usermod -m -d /home/myuser myuser?これを行う方法やユーザー名を変更する方法はありますか?

ありがとうございます。

ベストアンサー1

あなたが経験している問題は、ログインしたユーザーアカウントを変更しようとしているようです。少しトリッキーかもしれません。しかし、次のことを試すことができます。

# Become the root user (if you aren't already)
sudo su

# Set the password for the root user
echo "root:<PASSWORD>" | chpasswd

# Update the system user-datbase files to reflect the name-change
sed -i 's/pi/myuser/g' /etc/passwd
sed -i 's/pi/myuser/g' /etc/shadow
sed -i 's/pi/myuser/g' /etc/group

# Update the sudoers file to reflect the name-change
sed -i /etc/sudoers 's/pi/myuser/g'

# Move the user home directory to the new location (the UID stays the same, we don't need to run chown)
mv -i /home/pi /home/newuser

ユーザーとしてログインしている間にこのスクリプトを非対話式に実行するには、pi次のスクリプトを生成できます。

#!/bin/bash

# update_user.sh

# Set the password for the root user
echo "root:<PASSWORD>" | chpasswd

# Update the system user-datbase files to reflect the name-change
sed -i 's/pi/myuser/g' /etc/passwd
sed -i 's/pi/myuser/g' /etc/shadow
sed -i 's/pi/myuser/g' /etc/group

# Update the sudoers file to reflect the name-change
sed -i /etc/sudoers 's/pi/myuser/g'

# Move the user home directory to the new location (the UID stays the same, we don't need to run chown)
mv -i /home/pi /home/newuser

次に、sudo次のように実行します。

sudo update_user.sh

メモ:私の考えでは、rootパスワードを含むシェルスクリプトを持つことはおそらく良い考えではないようです。考えてみることもできます。いいえこのようにプログラムでルートパスワードを設定します。

私の元の解決策は次のとおりです。


何かが欠けているかもしれませんが、なぜあなたのデバイスを繰り返し再起動する必要があるのか​​わかりません。必要な変更をすべて実行し、完了したら再起動できる必要があります。次のことを試してみてはいかがですか?

# Become the root user (if you aren't already)
sudo su

# Set the password for the root user
passwd

# Change the login name of the "pi" user to "myuser"
usermod -l myuser pi

# Update the "myuser" home directory
usermod -m -d /home/myuser myuser

# Edit the file /etc/sudoers and change pi to myuser
visudo

# Reboot the system
reboot

使用中のコマンドのリストが若干変更されました。sudo suまず、すべてのコマンドを実行し、完了したら再起動します。現在の形式では、ユーザーのやり取り(特にパスワード設定とsudoersファイルの編集)が必要なため、スクリプトはまだ可能ではありません。このジョブを無人で実行するには、両方のジョブを自動化する必要があります。これが目標である場合は、次のようにスクリプトを変更する必要があります。

# Become the root user (if you aren't already)
sudo su

# Set the password for the root user
echo "root:<PASSWORD>" | chpasswd

# Change the login name of the "pi" user to "myuser"
usermod -l myuser pi

# Update the "myuser" home directory
usermod -m -d /home/myuser myuser

# Edit the file /etc/sudoers and change pi to myuser (CAREFUL!)
sed -i /etc/sudoers 's/pi/myuser/g'

# Reboot the system
reboot

プログラムでパスワードを設定または変更する方法の詳細については、次の記事を参照してください。

おすすめ記事