centOS 7 添加刪除用戶和用戶組


1、添加新用戶

由於日常使用時root用戶權限過大,所以添加一個用戶供日常使用,或者供他人使用。

  • 1 新增用戶

adduser [用戶名]


 [root@centos ~]# adduser dex

  • 2 設置密碼

passwd [用戶名]

[root@centos ~]# passwd dex
Changing password for user dex.
New password: 
Retype new password: 
sswd: all authentication tokens updated successfully.

  • 3 授權
    新創建的用戶並不能使用sudo命令,需要給他添加授權。

個人用戶的權限只可以在本home下有完整權限,其他目錄要看別人授權。而經常需要root用戶的權限,這時候sudo可以化身為root來操作。我記得我曾經sudo創建了文件,然后發現自己並沒有讀寫權限,因為查看權限是root創建的。

新創建的用戶並不能使用sudo命令,需要給他添加授權。

sudo命令的授權管理是在sudoers文件里的。可以看看sudoers:

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

找到這個文件位置之后再查看權限:


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

是的,只有只讀的權限,如果想要修改的話,需要先添加w權限:

chmod -v u+w /etc/sudoers #添加sudoers文件可寫權限


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

然后就可以添加內容了,在下面的一行下追加新增的用戶:

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

## Allow root to run any commands anywher 
root    ALL=(ALL)       ALL  
linuxidc  ALL=(ALL)       ALL  #這個是新增的用戶

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

chmod -v u-w /etc/sudoers # 收回寫權限

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

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


[root@centos ~]$ 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。

2、增加用戶組

  • 1 新建test工作組

 [root@centos ~]# groupadd testgroup 

  • 2 新建dex用戶並增加到testgroup工作組

 [root@centos ~]# useradd -g testgroup testuser

注::-g 所屬組 -d 家目錄 -s 所用的SHELL

  • 3 給已有的用戶增加工作組

usermod -G groupname username


 [root@centos ~]# usermod -G testgroup dex

3、刪除用戶

  • 1 臨時關閉

關閉用戶賬號:

[root@centos ~]$ passwd dex –l 

重新釋放:

[root@centos ~]$ passwd dex –u
  • 2 永久性刪除用戶賬號
[root@centos ~]$ userdel dex 
groupdel testgroup 
usermod –G testgroup testuser //(強制刪除該用戶的主目錄和主目錄下的所有文件和子目錄)
  • 3 刪除用戶組
[root@centos ~]$ groupdel testgroup 
# 或者 (強制刪除該用戶的主目錄和主目錄下的所有文件和子目錄)
[root@centos ~]$ usermod –G testgroup dex 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM