0%

[User] Linux 新增使用者並賦予其使用 sudo 的權限

前言

多人多工是 Linux 本身就具備的功能,因此新增或移除使用者帳號與更改權限是管理員必備的技能。這篇文章記錄如何在 Ubuntu、CentOS 上新增 sudoer。

環境

  • Ubuntu 18.04 LTS
  • CentOS 8.2

新增帳號

開啟 Terminal,新增帳號叫做 test,並填寫相關資料

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
user@pc:~$ sudo adduser test        # test 是使用者帳號
Adding user `test' ...
Adding new group `test' (1003) ...
Adding new user `test' (1003) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: # 這裡是輸入密碼
Retype new UNIX password: # 這裡是確認密碼
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []: test # 使用者全名
Room Number []: # 使用者房號
Work Phone []: # 使用者工作電話
Home Phone []: # 家用電話
Other []: # 備註
Is the information correct? [Y/n] Y # 確認上述資料無誤後給Y

開啟 Terminal,新增帳號叫做 test,並填寫相關資料

1
2
[user@localhost ~]$ sudo adduser test  # 新增帳號 test
[user@localhost ~]$ sudo passwd test # 修改 test 的密碼

賦予使用 sudo 指令的權限

需將該使用者增加至 sudo 群組,才可使用 sudo 指令

1
user@pc:~$ sudo adduser test sudo

此時登入 test 帳號,開啟 Terminal,輸入以下指令驗證有使用 sudo 的權限,若看到有列出 /root 目錄下的檔案資訊,則表示 test 帳號擁有可以使用 sudo 的權限了

1
2
3
4
5
6
7
8
test@pc:~$ sudo ls -al /root
[sudo] password for test:
total 20
drwx------ 3 root root 4096 八 6 2019 .
drwxr-xr-x 24 root root 4096 三 6 13:55 ..
-rw-r--r-- 1 root root 3106 四 9 2018 .bashrc
drwx------ 2 root root 4096 八 6 2019 .cache
-rw-r--r-- 1 root root 148 八 17 2015 .profile

接下來要將使用者加到 sudoer 群組,與 Ubuntu 不同的是在 CentOS 是直接去更改文件中的內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[user@localhost ~]$ sudo visudo     # visudo 就是 vi sudoers啦!

# 以下為 suduers 檔案內容

# Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL # 這行是預設
user ALL=(ALL) ALL # 這行是用「root」身份加的,若沒有這行,就無法以「user」執行sudo
test ALL=(ALL) ALL # 這個步驟要加上這行

此時登入 test 帳號,開啟 Terminal,輸入以下指令驗證有使用 sudo 的權限,若看到有列出 /root 目錄下的檔案資訊,則表示 test 帳號擁有可以使用 sudo 的權限了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[test@localhost ~]$ sudo ls -al /root
[sudo] password for test:
total 32
dr-xr-x---. 5 root root 205 Sep 10 08:18 .
dr-xr-xr-x. 17 root root 224 Sep 9 16:48 ..
-rw-------. 1 root root 1369 Sep 9 16:57 anaconda-ks.cfg
-rw-------. 1 root root 13 Sep 10 08:18 .bash_history
-rw-r--r--. 1 root root 18 May 11 2019 .bash_logout
-rw-r--r--. 1 root root 176 May 11 2019 .bash_profile
-rw-r--r--. 1 root root 176 May 11 2019 .bashrc
drwx------. 3 root root 19 Sep 9 19:50 .cache
drwx------. 3 root root 18 Sep 9 19:50 .config
-rw-r--r--. 1 root root 100 May 11 2019 .cshrc
drwx------. 3 root root 25 Sep 9 19:50 .dbus
-rw-r--r--. 1 root root 1479 Sep 9 19:50 initial-setup-ks.cfg
-rw-r--r--. 1 root root 129 May 11 2019 .tcshrc

移除使用 sudo 指令的權限

adduser 相對的指令是 deluser,將使用者 testsudo 群組中移除,必須注意一定要加上群組名稱 sudo,否則會直接將該使用者移除

1
user@pc:~$ sudo deluser test sudo

與加入使用者進 sudoer 群組的步驟相反,將使用者從文件中移除,即可移除使用者使用 sudo 指令的權限囉!

很高興能在這裡幫助到您,歡迎登入 Liker 為我鼓掌 5 次,或者成為我的讚賞公民,鼓勵我繼續創造優質文章。
以最優質的內容回應您的鼓勵