# 登出與關機

## 當使用者登入後,要變更使用者可輸入 logout 登出後再以其他的帳號登入, exit 也可登出,但兩者有差異

### logout 登出

#### 登出目前的使用者指令為 logout ,登出後可更換其他的帳號再登入。

#### exit 退出目前的 shell

exit = logout 也是登出,事實上並不正確。**exit 正確來說是『退出目前的 shell』**,因大部分的情況使用者只用一種 shell,所以退出目前的 shell是等於logout,但如果有 子 shell 就不成立了。

例如目前登入是預設的 shell (稱父 shell),但為了某個 shell 才有的功能或要執行某個 shell 所寫的 Script file (腳本語言)需載入其他的 shell (稱子 shell)。 在子 shell 下輸入 exit 是回到父 shell,並不是登出,回到父 shell 後就能用 logout/exit 登出了。

例: ubuntu\@ubuntu:\~$ echo $SHELL<-查詢目前的 shell (父 shell) /bin/bash ←目前為 Bourne shell (bash)

ubuntu\@ubuntu:\~$ cat /etc/shells <-查詢有那些 shell 可用

## /etc/shells: valid login shells

/bin/sh /bin/dash /bin/bash /bin/rbash /usr/bin/tmux /usr/bin/screen

ubuntu\@ubuntu:\~$ dash<-輸入可用的 shell(除了 nologin) $ <-進入不同的shell環境 logout $ logout ←用 logout 登出看看 dash: 1: logout: not found $ exit←退出目前的 shell ubuntu\@ubuntu:\~$ <-提示符號改變,回父 shell 的登入符號 再次logout logout ←此時即可用 logout 登出系統了

關機 poweroff 關機

指令 poweroff 為關掉所有執行中的程序,並將緩衝記憶體資料存到硬碟,主機符合 ACPI (Advanced Configuration and Power Interface,進階組態與電源介面)便可自動關掉 PC 電源。

halt 關機

傳統的 Unix 可沒 poweroff 這指令可在關機時順便關掉 PC 電源,因當時沒ACPI, halt 為 Unix 傳統的關機指令功能與 poweroff 相同但不會自動切斷 PC 電源, halt 只會關掉所有軟體程式，需要手動按 PC 的開關來關電源。

關閉所有程序，但沒有關閉電源

reboot 重開機

同 poweroff,但 reboot 指令會重新開機。

shutdown 關機

傳統的 UNIX,隨時有許多人登入使用,一般的登入者是不可以下關機指令的,要關機只有經授權的帳號或系統管理員才可下 shutdown 指令來關機。

shutdown 的用法

語法:shutdown \[-otpiton]\[時間(hh:mm) 或 (+m) 或 (now)]\['訊息']

-F 重開機時強迫用 fsck 檢查 partition

-k 只送訊息給其他登入的使用者,但不關機&#x20;

-h 關掉所有執行中的程序後關機&#x20;

-r 關機後重開機 (同 reboot 指令) 其中時間的選項可為 hh:mm: 明確的標示時間表示法。&#x20;

如 shutdown -h 10:05 表示 10 點 05 分會自動關機。 +m: 為幾分鐘後,m 為數字。 如 shutdown -r +5 'The system will reboot after 5 minutes' 表示 5 分鐘後重新開機,並傳送 The system will reboot after 5 minutes 給每一登入的使用者 ,+號是可省略的,如 shutdown -h 5 表示 5 分鐘後自動關機。

now:表示立即。

此外,如閞機時間還沒到,後悔了可按  來停止關機哦。

例: shutdown -h now ←立刻關機

shutdown -h +10 ←設 10 分鐘後會關機

Ctrl+C ←如時間還沒到,可按  來停止關機

Shutdown cancelled.←shutdown 指令被 中止了

例: shutdown -h 0 'hello' ←可加其他的訊息通知其他的登入者&#x20;

shutdown -k now 'hello' ←不會真正關機,只是把訊息通知其他的登入者&#x20;

shutdown -r 10:05 ←10:05會重開機

### **runlevel**

| **runlevel** | **模式**                        |
| ------------ | ----------------------------- |
| **0**        | **關機**                        |
| **1**        | **單用戶模式,維護用途故不會讓其他帳號登入**      |
| **2**        | **多人模式,但無網路連線**               |
| **3**        | **多人模式,有網路連線但運行純文字界面**        |
| **4**        | **預留未使用**                     |
| **5**        | **同 3，若有安裝Xwindow以圖形介面開機和操作** |
| **6**        | **重開機**                       |

**ubuntu\@ubuntu:\~$ runlevel**\
**N 5**&#x20;

**第一個字元 N 為之前的 runlevel,第二個字元 5 為目前的 runlevel,其中 runlevel 等級有時會用英文字母代表,如 N 表示 None 不存在的意思,S 表示 Single 單用戶模式)**<br>

**例: (以 root 登入測試)**\
**init 0 ←關機指令用〝init 0〞(即 runlevel =0 來關機)**\
**init 6 ←reboot 重新開機**\
**init 1 ←進入維護模式**<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kawsing.gitbook.io/opensystem/andoid-shou-ji/untitled-4/linux-cli/deng-chu-yu-guan-ji.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
