1. 程式人生 > 其它 >在centos7中新增一個新使用者,並授權

在centos7中新增一個新使用者,並授權

前言

筆記本裝了一個centos,想要讓別人也可以登入訪問,用自己的賬號確實不太好,於是準備新建一個使用者給他。

建立新使用者

建立一個使用者名稱為:zhangbiao

[root@localhost ~]# adduser zhangbiao

為這個使用者初始化密碼,linux會判斷密碼複雜度,不過可以強行忽略:

[root@localhost ~]# passwd zhangbiao
更改使用者 zhangbiao 的密碼 。
新的 密碼:
無效的密碼: 密碼未通過字典檢查 - 過於簡單化/系統化
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。

授權

個人使用者的許可權只可以在本home下有完整許可權,其他目錄要看別人授權。而經常需要root使用者的許可權,這時候sudo可以化身為root來操作。我記得我曾經sudo建立了檔案,然後發現自己並沒有讀寫許可權,因為檢視許可權是root建立的。

新建立的使用者並不能使用sudo命令,需要給他新增授權。

sudo命令的授權管理是在sudoers檔案裡的。可以看看sudoers:

[root@localhost ~]# sudoers
bash: sudoers: 未找到命令...
[root@localhost ~]# whereis sudoers
sudoers: /etc/sudoers /etc/sudoers.d /usr/libexec/sudoers.so /usr/share/man/man5/sudoers.5.gz

找到這個檔案位置之後再檢視許可權:

[root@localhost ~]# ls -l /etc/sudoers
-r--r----- 1 root root 4251 9月  25 15:08 /etc/sudoers

是的,只有只讀的許可權,如果想要修改的話,需要先新增w許可權:

[root@localhost ~]# chmod -v u+w /etc/sudoers
mode of "/etc/sudoers" changed from 0440 (r--r-----) to 0640 (rw-r-----)

然後就可以新增內容了,在下面的一行下追加新增的使用者:

[root@localhost ~]# vim /etc/sudoers


## Allow root to run any commands anywher  
root    ALL=(ALL)       ALL  
zhangbiao  ALL=(ALL)       ALL  #這個是新增的使用者

wq儲存退出,這時候要記得將寫許可權收回:

[root@localhost ~]# chmod -v u-w /etc/sudoers
mode of "/etc/sudoers" changed from 0640 (rw-r-----) to 0440 (r--r-----)

這時候使用新使用者登入,使用sudo:

[zhangbiao@localhost ~]$ sudo cat /etc/passwd
[sudo] password for zhangbiao: 

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

第一次使用會提示你,你已經化身超人,身負責任。而且需要輸入密碼才可以下一步。如果不想需要輸入密碼怎麼辦,將最後一個ALL修改成NOPASSWD: ALL

參考

Centos 7新增使用者