[Linux學習--用戶管理]centos中添加一個新用戶,並受權

前言

有時候給root用戶不太方便,新建一個用於並賦予權限這個作法相對好些linux

建立新用戶

建立一個用戶名爲:cmjvim

[root@localhost ~]# adduser cmj

爲這個用戶初始化密碼,linux會判斷密碼複雜度,不過能夠強行忽略:bash

[root@localhost ~]# passwd cmj
更改用戶 cmj 的密碼 
新的 密碼:
無效的密碼: 密碼未經過字典檢查 - 過於簡單化/系統化
從新輸入新的 密碼:
passwd:全部的身份驗證令牌已經成功更新。

受權

我的用戶的權限只能夠在本home下有完整權限,其餘目錄要看別人受權。而常常須要root用戶的權限,這時候sudo能夠化身爲root來操做。我記得我曾經sudo建立了文件,而後發現本身並無讀寫權限,由於查看權限是root建立的。code

新建立的用戶並不能使用sudo命令,須要給他添加受權。three

sudo命令的受權管理是在sudoers文件裏的。能夠看看sudoers:it

[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權限:rust

[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  
cmj  ALL=(ALL)       ALL  #這個是新增的用戶

wq保存退出,這時候要記得將寫權限收回:command

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

這時候使用新用戶登陸,使用sudo:

[cmj@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

相關文章
相關標籤/搜索