ICTについて使える情報を発信していきます. 遠回りしたり, 自分のペースで...

このブログを検索

ユーザー名, パスワード変更 [RaspberryPi3]

私がしたRaspberryPiの設定は, 「RaspberryPiの設定 まとめリスト」にまとめてあります.

  • 今回の設定のメリット
    • セキュリティ的に安心できる.
    • というかパスワード変更ぐらいは必須.
  • 今回の設定のデメリット
    • 設定がめんどくさい

まず, ユーザーの変更をしなければいけません.
sshを導入したと思いますが, 遠隔操作ができるというメリットは, 他の人もアクセスできる可能性があるというデメリットを生みます.
実際, 「Raspbian install on Raspberry Pi 3」では, 私の手順をなぞればsshができましたよね. 他の人が同じ場所で真似をすれば, 同じことができるということです.

ということで, ユーザー名とパスワードを変更するのはセキュリティ的に大切です. やりましょう.

手順としては,

  1. 新しいユーザーtempを作る.
  2. ユーザーtempにsshし, piの名前を変更する.
  3. ユーザーtempを削除する.
  4. 変更したユーザーのパスワードを変える.

という感じです.

ちなみに, ユーザーtempを使うのはユーザーpiが使われている場合, 名前の変更ができないからです.

pi@raspberrypi:~ $ sudo usermod -l hoge pi 
usermod: user pi is currently used by process 551

piの中で作業しているので, piを使っていると見なされています.

新しいユーザーtempを作る.

次のコマンドを打ちましょう. $から後ろがコマンドです.

$ # ユーザーpiにsshする.
$ ssh pi@raspberrypi.local
Linux raspberrypi 4.14.42-v7+ #1114 SMP Mon May 21 16:39:21 BST 2018 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun May 27 14:53:10 2018 from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx

$ # tempユーザーの作成
$ sudo useradd -M temp

$ # sudoをtempユーザーでも使えるようにする.
$ sudo gpasswd -a temp sudo
Adding user temp to group sudo

$ # tempユーザーのパスワードを設定
$ sudo passwd temp
Changing the password for group temp
New Password: 
Re-enter new password: 

$ # logoutする.
$ exit
logout
Connection to raspberrypi.local closed.

ユーザー名は適当で良いです. 今回はtempというユーザー名にしています. tempという名前は, すぐ削除する一時的なものにつける事が多い, 慣習的な名前です.

「New Password:」は適当なパスワードを設定して下さい. どうせすぐ削除するので.

ユーザーtempにsshし, piの名前を変更する.

先程と同様の手順で, ユーザーtempにsshします. その後,
piの名前を変更します. 好きな名前に変更して良いですが, 今回は例としてhogeというユーザー名にします.

次のコマンドを打ちましょう.

$ # ユーザーtempにsshする.
$ ssh temp@raspberrypi.local
temp@raspberrypi.local's password: # 1で設定したtempのパスワードを入れる
Linux raspberrypi 4.14.42-v7+ #1114 SMP Mon May 21 16:39:21 BST 2018 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun May 27 15:24:36 2018 from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Could not chdir to home directory /home/temp: No such file or directory

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

$ # エラーが出ないように, 一応piのprocessをkillする.
$ sudo pkill -u pi
[sudo] password for temp: # 1で設定したtempのパスワードを入れる

$ # 好きなユーザー名に変更. 今回はhoge.
sudo usermod -l hoge -d /home/hoge -m pi

$ # groupを変更する.
$ sudo groupmod -n hoge pi
$ exit
Connection to raspberrypi.local closed.

確認のため, hogeにsshします.

$ ssh hoge@raspberrypi.local
Linux raspberrypi 4.14.42-v7+ #1114 SMP Mon May 21 16:39:21 BST 2018 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun May 27 14:57:06 2018 from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
hoge@raspberrypi:~ $ 

sshでき, 「hoge@raspberrypi:~ $ 」になっていると思います. これで, ユーザー名の変更は終わりです.

ユーザーtempを削除する.

ユーザーtempを削除します. パスワードを聞かれますが, 「raspberry」にして下さい.

$ ssh hoge@raspberrypi.local
Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed May 23 12:25:05 2018 from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx

$ # ユーザーtempを削除. passwordは「raspberry」にする.
$ sudo userdel temp

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for hoge: # raspberryと入力
hoge@raspberrypi:~ $ 

これでtempが削除されました.

おまけ

ワンライナーみたいな感じで, 新しいユーザーを作らなくてもユーザーが変更できるコマンドを作りました. 上の1~3までこれでできます. sudo suができなければダメですが…

$ ssh pi@raspberrypi.local
$ sudo su ; exit
$ /bin/bash -c 'NEWNAME=hoge; OLDNAME=pi; nohup sleep 3; pkill -u ${OLDNAME}; usermod -l ${NEWNAME} -d /home/${NEWNAME} -m ${OLDNAME}; groupmod -n ${NEWNAME} ${OLDNAME} &' & exit

NEWNAME=新しい名前, OLDNAME=古い名前を入れれば, すぐに変更できると思います.

でも複雑さが増してしまったので, 上の手順を素直にやったほうが良いかと.

変更したユーザーのパスワードを変える.

変更したユーザーのパスワードを変えていきます.
「[sudo] password for hoge:」のパスワードは「raspberry」にして下さい.
その後, 新しいパスワードを打っていきます.

$ ssh hoge@raspberrypi.local
Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed May 23 13:24:35 2018 from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx

$ # hogeのパスワードを変更
hoge@raspberrypi:~ $ sudo passwd hoge
[sudo] password for hoge: # raspberryと入力
Enter new UNIX password: # 新しいパスワードを入力
Retype new UNIX password: # もう一度新しいパスワードを入力
passwd: password updated successfully
hoge@raspberrypi:~ $ 

これで, パスワードの変更は終了です.
以上で, ユーザー名, パスワードの変更は終了になります.

piユーザーの残骸を削除.

最後に, あまり詳しく説明しません(他の方が色々説明なさってます)が, piの残り物を消します.
ssh先で次のコマンドを打ちます.

$ sudo rm /etc/sudoers.d/010_pi-nopasswd

これでpiユーザーの残骸もなくなりました.

これで終了です. 滅多なことがない限り復元しないほうが良いですが, 復元したい場合は以下のコマンドを打って下さい.

sudo sh -c 'echo "pi ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/010_pi-nopasswd'

まとめ

ちょっとめんどくさいですが, やっていたほうが良い設定です. 是非とも頑張って設定してみて下さい.

参考文献

0 コメント:

コメントを投稿

Popular Posts