RHEL and CentOS v6 and v7用戶運行模式切換以及用戶自動登陸配置

前言

RHEL/CentOS有多種系統運行級別,比較常用的多用戶模式(3-multi-user mode)和圖形界面模式(5-graphical mode),以下爲所有的運行模式。

v6-運行級別 v7-對應的目標 定義
0 poweroff.target 關機
1 rescue.target 單用戶救援模式
2 multi-user.target 文字界面多用戶模式(v6未啓用NFS)
3 multi-user.target 文字界面多用戶文字模式
4 multi-user.target 文字界面多用戶文字模式(v6未用到)
5 graphical.target 圖形界面多用戶模式
6 reboot.target 重啓

本文主要講解v6/v7單次和永久運行模式切換,以及對應模式的自動登陸。

v6運行模式切換以及用戶自動登陸

以下適用於RHEL/CentOS v6版本。

1. 單次用戶模式切換

init 3 切換到文字界面多用戶模式
init 5 切換到圖形界面多用戶模式

2. 永久用戶模式切換

使用指令vim /etc/inittab編輯inittab文件,最後一行id:5:initdefaultid後面的數字即爲開機運行模式,3表示文字界面多用戶模式,5表示圖形界面多用戶模式,如下圖
模式切換

3. 文字界面多用戶模式用戶自動登陸設置

使用命令vim /etc/init/tty.conf編輯tty.conf文件,在exec /sbin/mingetty $TTY後面添加參數--autologin root即表示自動登陸root用戶

文字界面多用戶自動登陸

此處其實是給mingetty命令傳遞了一個參數進去。如下圖是mingetty支持的一些參數,更多參數請查看man page

mingetty參數

4. 圖形界面多用戶模式用戶自動登陸設置

使用命令vim /etc/gdm/custom.conf編輯custom.conf配置文件,在[daemon]後追加如下兩行內容:

AutomaticLoginEnable=True
AutomaticLogin=root

即表示開機後進入圖形界面自動登陸root用戶

圖形界面用戶自動登陸

v7運行模式切換以及用戶自動登陸

以下適用於RHEL/CentOS v7版本。

1. 單次用戶模式切換

init 3 切換到文字界面多用戶模式
init 5 切換到圖形界面多用戶模式

2. 永久用戶模式切換

systemctl set-default multi-user.target 切換到文字界面多用戶模式
systemctl set-default graphical.target 切換到圖形界面多用戶模式

用戶模式永久切換

由上圖可以看出,其實在切換過程中,命令相當於執行了一次鏈接操作,將預設的啓動目標鏈接到/etc/systemd/system/default.target。也就是說其實我們是可以通過手動鏈接啓動模式到這個文件來達到相同的效果。

ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

3. 文字界面多用戶模式用戶自動登陸設置

使用命令vim /etc/systemd/system/getty.target.wants/[email protected]編輯[email protected]文件,在Service group參數ExecStart=-/sbin/agetty --noclear %I $TERM後添加--autologin root參數來實現自動登陸。

文字界面用戶自動登陸

這裏其實和v6 中給mingetty命令傳遞了一個參數類似,v7中實際是給agetty命令傳遞了一個--autologin root的參數,agetty用於初始化用戶終端,支持豐富的參數。如下

Usage:
agetty[options] <line> [<baud_rate>,…] [<termtype>]
agetty [options] <baud_rate>,… <line> [<termtype>]

Open a terminal and set its mode.

Options:
-8, --8bits assume 8-bit tty
-a, --autologin <user> login the specified user automatically
-c, --noreset do not reset control mode
-E, --remote use -r <hostname> for login(1)
-f, --issue-file <file> display issue file
-h, --flow-control enable hardware flow control
-H, --host <hostname> specify login host
-i, --noissue do not display issue file
-I, --init-string <string> set init string
-J --noclear do not clear the screen before prompt
-l, --login-program <file> specify login program
-L, --local-line[=<mode>] control the local line flag
-m, --extract-baud extract baud rate during connect
-n, --skip-login do not prompt for login
-N --nonewline do not print a newline before issue
-o, --login-options <opts> options that are passed to login
-p, --login-pause wait for any key before the login
-r, --chroot <dir> change root to the directory
-R, --hangup do virtually hangup on the tty
-s, --keep-baud try to keep baud rate after break
-t, --timeout <number> login process timeout
-U, --detect-case detect uppercase terminal
-w, --wait-cr wait carriage-return
–nohints do not print hints
–nohostname no hostname at all will be shown
–long-hostname show full qualified hostname
–erase-chars <string> additional backspace chars
–kill-chars <string> additional kill chars
–chdir <directory> chdir before the login
–delay <number> sleep seconds before prompt
–nice <number> run login with this priority
–reload reload prompts on running agetty instances
–help display this help and exit
–version output version information and exit

4. 圖形界面多用戶模式用戶自動登陸設置

使用命令vim /etc/gdm/custom.conf編輯custom.conf配置文件,在[daemon]後追加如下兩行內容:

AutomaticLoginEnable=True
AutomaticLogin=root

這裏配置方法是和v6一樣的。

總結

文字界面多用戶模式和圖形界面多用戶模式自動登陸需執行不同的配置方法,使用於gnome的X11圖形管理。按照上面的方法,可以很方便的切換用戶模式和配置自動登陸。😀

推薦關注博主公衆號,獲取最新的文章😀

服務器測試與運維

📌轉載請註明來源,版權歸作者**@hualong1009**所有, 謝謝

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章