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?

WSL rootパスワードを忘れた時の対処法

0
Posted at

はじめに

WSL(Windows Subsystem for Linux)を使っていて、sudoしようと思ったら「rootのパスワードってわからない!」となったことはないでしょうか。
こんな時は絶望のがけっぷちにたたされることとなりますがWSLであれば大丈夫です
Windows 側から root 権限で起動できるので、パスワードなしでリセットできます。


対象者

  • WSL の root パスワードを忘れてしまった
  • su -sudo su でパスワードを求められて詰まっている
  • WSL を使い始めたばかりで初期パスワードがわからない

環境

  • Windows 10 / 11
  • WSL2(Ubuntu / Debian など各ディストリビューション共通)

全体の流れ

ディストリビューション名を確認
root ユーザーで WSL を起動
passwd でパスワードを変更
WSL を再起動


手順

Step 1: ディストリビューション名を確認する

PowerShell または コマンドプロンプトで実行:

wsl --list

実行結果の例:

Windows Subsystem for Linux Distributions:
Ubuntu (Default)
Debian

ここで表示される名前(例:Ubuntu)を次のコマンドで使います。


Step 2: root ユーザーで WSL を起動する

wsl -d <ディストリビューション名> -u root

例(Ubuntu の場合):

wsl -d Ubuntu -u root

パスワードなしで root のシェルに入れます。


Step 3: パスワードを変更する

root のプロンプトが表示されたら、passwd コマンドでパスワードを変更します。

root のパスワードを変更する場合:

passwd

一般ユーザーのパスワードを変更する場合:

passwd <ユーザー名>

新しいパスワードを2回入力して完了です。



Step 4: WSL を再起動する

PowerShell に戻って WSL を終了します。

# 特定のディストリビューションのみ終了
wsl --terminate <ディストリビューション名>

# 全ディストリビューションをまとめて終了
wsl --shutdown

その後、通常通り WSL を起動すれば新しいパスワードで使えます。

wsl

まとめ

ステップ コマンド 実行場所
ディストリビューション名確認 wsl --list PowerShell
root で WSL 起動 wsl -d <名前> -u root PowerShell
パスワード変更 passwd または passwd <ユーザー名> WSL 内(root)
WSL 再起動 wsl --terminate <名前> PowerShell

パスワードを忘れても root で入り直せるので、焦らず対処できます。


関連記事

henagineerのプログラミング・開発記事まとめ

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?