0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

パスワードが分からずzshからbashに変えるのに苦戦した話

Posted at

はじめに

EC2インスタンス上でデフォルトシェルをzshからbashに変更する方法について解説します。通常のchshコマンドがパスワード認証で失敗する場合の対処法も含めています。

問題点

EC2インスタンスでは、通常のパスワード認証が設定されていないことが多く、以下のような場合にchshコマンドが失敗します。

user@ip-xxx-xx-xx-xx% chsh -s /bin/bash
Password: 
chsh: PAM: Authentication failure

これは、ユーザーのパスワードが設定されていないか、PAM(Pluggable Authentication Modules)の設定によって認証が失敗するためです。

解決方法

1. sudoを使ってシェルを変更する

パスワード認証をバイパスするため、sudoを使って変更します:

sudo chsh -s /bin/bash $USER

2. 既存のzsh設定をバックアップする

mkdir -p ~/zsh_backup
mv ~/.zcompdump ~/zsh_backup/
mv ~/.zshrc ~/zsh_backup/

3. bash用の設定ファイルを作成する

echo 'if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi' > ~/.bash_profile

chmod 644 ~/.bash_profile
chown $USER:$USER ~/.bash_profile

4. 一度ログアウトして再接続する

exit

その後、SSHで再接続します。

変更の確認

以下のコマンドで変更を確認できます:

echo $SHELL           # 現在のシェルを表示
ps -p $$              # シェルプロセスを確認
grep $USER /etc/passwd # /etc/passwdエントリを確認

正しく変更されていれば、以下のような出力になります:

user@ip-xxx-xx-xx-xx:~$ echo $SHELL
/bin/bash
user@ip-xxx-xx-xx-xx:~$ ps -p $$
    PID TTY          TIME CMD
  28726 pts/0    00:00:00 bash
user@ip-xxx-xx-xx-xx:~$ grep username /etc/passwd
username:x:1001:1002::/home/username:/bin/bash

まとめ

EC2インスタンスでzshからbashへのシェル変更は、通常のchshコマンドではパスワード認証の問題で失敗することがありますが、sudo chshを使うことで簡単に解決できます。適切なシェル設定ファイルを用意し、権限を設定することで、スムーズに移行することができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?