編碼的世界 / 優質文選 / 感情

centos創建新用戶


2022年6月11日
-   

創建新用戶
創建一個用戶名為:linuxidc
[root@localhost ~]# adduser linuxidc

為這個用戶初始化密碼,linux會判斷密碼複雜度,不過可以強行忽略:
[root@localhost ~]# passwd linuxidc
更改用戶 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
-rr- 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 (rr-) to 0640 (rw-r-)

然後就可以添加內容了,在下面的一行下追加新增的用戶:
[root@localhost ~]# vim /etc/sudoers
## Allow root to run any commands anywher
root ALL=(ALL) ALL
linuxidc ALL=(ALL) ALL #這個是新增的用戶

wq保存退出,這時候要記得將寫權限收回:
[root@localhost ~]# chmod -v u-w /etc/sudoers
mode of "/etc/sudoers" changed from 0640 (rw-r-) to 0440 (rr-)

這時候使用新用戶登錄,使用sudo:
[linuxidc@localhost ~]$ sudo cat /etc/passwd
[sudo] password for linuxidc:
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
轉載鏈接:https://www.linuxidc.com/Linux/2016-11/137549.htm

熱門文章