2025年4月30日 星期三

剛裝好linux base OS,怎麼設定root的密碼,之後用root登入

 在 Raspberry Pi 3 上設定 root 密碼並允許用 root 用戶登入,你需要按照以下步驟操作:


步驟:

  1. 開機並登入你的 Raspberry Pi

    通常你會用 pi 用戶登入 Raspberry Pi,默認密碼是 raspberry

  2. 開啟 root 帳戶

    你可以通過以下步驟啟用 root 帳戶:

    • 使用 sudo 設定 root 密碼:

      sudo passwd root
    • 系統會提示你輸入新密碼(輸入後需再確認一次)。

      Enter new UNIX password:
      Retype new UNIX password:

      這樣就成功設定了 root 密碼。


  1. 啟用 root 登入(可選)

    預設情況下,Raspberry Pi 可能會禁用 root 直接登入(例如通過 ssh 或本地登入),為了讓 root 可以登入,請修改 sshd_config 設定檔(如果你打算通過 ssh 登入)。

    • 編輯 sshd_config

      sudo nano /etc/ssh/sshd_config
    • 找到 PermitRootLogin 這一行,並確認它是設定為 yes

      PermitRootLogin yes
    • 如果這一行被註解掉或設定為 no,請將其修改為 yes 並儲存。

  2. 重新啟動 SSH 服務(如果修改了 SSH 設定)

    sudo systemctl restart ssh

    這樣就可以允許 root 用戶透過 ssh 登入了。


  1. 使用 root 登入

    • 若是本地登入,你可以直接執行:

      su -
    • 如果是透過 ssh 登入 Raspberry Pi,則使用:

      ssh root@<RPI_IP>

    然後輸入設定的 root 密碼。

如果 ssh 出現 error message
ssh: connect to host 192.168.1.201 port 22: Connection refused

表示 SSH 服務沒有啟動沒有允許 root 登入,下面是一些常見的原因及解決方法:


檢查並解決問題的步驟

1. 確認 SSH 服務是否啟動

首先,確保 Raspberry Pi 上的 SSH 服務 已經啟動。

  1. 在 Raspberry Pi 上使用 pi 用戶登入,並執行以下指令來檢查 SSH 服務狀態:

    sudo systemctl status ssh

    如果顯示 ssh.service is not activessh.service not found,表示 SSH 服務沒有啟動。

  2. 若 SSH 沒有啟動,使用以下命令啟動它:

    sudo systemctl start ssh
  3. 為了讓 SSH 在開機時自動啟動,執行以下指令:

    sudo systemctl enable ssh

2. 檢查 sshd_config 設定檔

如果 SSH 已經啟動,但 root 仍然無法登入,可能是 sshd_config 設定檔限制了 root 用戶的登入。

  1. 編輯 SSH 配置檔:

    sudo nano /etc/ssh/sshd_config
  2. 確認以下設定項:

    • PermitRootLogin 應該設為 yes

      PermitRootLogin yes

      如果這一行被註解掉或設為 no,修改為 yes,然後儲存檔案。

  3. 重新啟動 SSH 服務使更改生效:

    sudo systemctl restart ssh

3. 檢查防火牆設置

如果 Raspberry Pi 使用了防火牆,可能會阻擋 SSH 的連線。可以檢查防火牆設定:

  1. 如果是使用 ufw 防火牆,檢查 SSH 是否被允許:

    sudo ufw status

    如果沒有允許 SSH,可以添加規則來開啟端口 22:

    sudo ufw allow ssh
    sudo ufw enable
  2. 若是使用其他防火牆工具,檢查並確保端口 22 沒有被封鎖。


4. 重新檢查網路連線

確保你的 Raspberry Pi 真的有 192.168.1.201 這個 IP 地址,並且它連接到網路上。

  1. 在 Raspberry Pi 上使用 hostname -I 檢查 IP 地址:

    hostname -I
  2. 檢查 Raspberry Pi 是否能夠接收外部連線,並確保它在正確的子網路下。


沒有留言:

張貼留言