Linux用戶與組之基本管理

Linux用戶與組之基本管理


  • 簡介Linux用戶與組

  • useradd

  • usermod

  • userdel

  • groupadd

  • groupmod

  • groupdel


環境:CentOS 6.8

簡介Linux用戶與組

Linux系統在靜態視角下,無非就是由諸多文件組成的一個“文件系統”;從動態視角來看,無非就是一個在內核支配之下的一個“文件系統”。這兩種角度是有些區別的:動態角度下,內核是獨立的,內核負責管控文件系統,而在靜態角度下,連內核本身也是文件系統下的一個文件而已。故而對Linux系統的管理無非就是對文件的管理!而 Linux系統中的用戶與組的概念,本質上也是一個文件而已,這兒沒有“虛化”的概念,只有一切皆文件的思想!

Unix, 是一個Multi-tasks, Multi-Users,即多用戶多任務的一個操作系統,Linux當然也繼承了這一優良的思想,在計算機的世界中,很多現象是可以藉助於社會問題而思考的,社會作爲人們共同活動的場所,我們需要不斷地去獲取社會資源,來滿足我們的日常生活,但是每一個人獲取社會資源的能力與渠道都不同,隨着社會生產大分工,人類這個大圈子逐漸有了階級,每個階級都有自己的利益代言人,而在中國古代,大概分爲士、農、工、商四大階層,階層的出現體現在個體身上主要就是身份的不同,身份的不同也意味着其獲取社會財富與資源的能力與權限不同,資源通常都是寶貴的,不可能每個人都可以任意的索取與利用,這一現象照樣在Linux系統中體現的淋漓盡致,於是,爲了更好的分配與利用計算機的能力,我們便有了用戶與組的概念。

一山不容二虎,Linux只有一個超級用戶,它就是管理員,通常是root(UID=0); 除管理員外,其它用戶都稱之爲普通用戶,而普通用戶主要分爲系統用戶與登錄用戶,登錄用戶就是我們平常所見的用戶,其主要動作是交互式登錄從而完成相關工作,而系統用戶是對守護進程與後臺服務獲取資源進行權限分配的用戶,它們是背後的英雄,默默地給爲計算機的正常運行提供強大的後勤保障。

組,就是多個用戶的一個共同集合,不難理解,組的出現就是爲了我們更加高效地管理用戶,也就是對Linux的用戶進行“階級分化”,對於不同的組,賦予不同的權限。而組的分類對應於用戶的分類,有管理員組(GID=0)與普通用戶組,而普通用戶組又分爲系統用戶組與登錄用戶組;除此之外,組的分類又可分爲基本組(主組)與附加組;也可分爲私有組與公共組;另外,在Linux系統中, 一個帳號定有其主組(primary group).主組有且只有一個。而附加組(secondary group)並不是必須要有的;私有組,組名同用戶名,且只包含一個用戶;公共組,組內包含了多個用戶。

沒有規矩,不成方圓。Linux中的用戶也必須要遵循一個最基本的守則,這就是安全上下文(security context)與3A認證:

安全上下文:	
進程以其發起者的身份運行;(進程就是運行中的程序)
進程對文件的訪問權限,取決於發起引此進程的用戶的權限;
每一個進程在訪問資源時,一定是代表某個用戶在運行,進程所擁有的權限,
實際上是發起此進程用戶的權限。3A:Authentication: 認證Authorization: 授權Audition: 審計

在Linux系統中,所有的文件都有其文件描述符fd, 而用戶與組作爲文件的一種,其亦有自己的標識符,俗稱UID與GID,計算機正是通過UID與GID來區分哪類用戶與組的,而root用戶的默認UID爲0,當其UID不爲0時,root便不再是“rooter"了!所以,這裏面就存在一個名稱解析的問題,即將標識符轉換爲用戶名或組名,反之亦然。

小結一下:

Linux用戶:Username/UID
管理員:0普通用戶:1-65535
	系統用戶:1-999(CentOS 7)			 
	         1-499(CentOS 5\6)
	登錄用戶:1000-60000(CentOS 7)			  
	         500-60000(CentOS 5\6)
			  
Linux組:Groupname/GID
管理員組:0
	普通用戶組:1-65535
	系統用戶組:1-999(CentOS 7)				
	           1-499(CentOS 5\6)
	登錄用戶組:1000-60000(CentOS 7)				
	           500-60000(CentOS 5\6)

關於認證:簡單的說就是通過比對事先存儲的識別信息,在登錄時看是否一致

這期間,密碼是繞不過的一關,且年密碼的使用策略:

1、使用隨機密碼;
2、最短長度不要低於8位;
3、應用使用大寫字母、小寫字母、數字和標點符號四類字符中至少三類;
4、定期更換;

密碼意味着加密算法,簡單瞭解下加密算法的各類:

加密算法:
	對稱加密:加密和解密使用同一個密碼;
	非對稱加密:加密和解密使用的一對密鑰,俗稱密鑰對;
		密鑰對:
			公鑰:public key 
			私鑰:private key 
	單向加密:只能加密,不能解密;提取數據特徵碼;
		定長輸出
		雪崩效應

加密機制:

加密:明文--> 密文
解密:密文--> 明文

常見的加密算法:

md5: message digest, 128bits
sha1: secure hash algorithm, 160bit
ssha224: 224bits
sha256: 256bits
sha384: 384bits
sha512: 512bits

有了加密的各類,意味的加密的方式是可以更改的:

[root@centos6 ~]# authconfig --passalgo=sha256 --update

密碼往往是有期限的, 先上圖,後續詳解:



最近一次更改密碼的日期:密碼的最小使用期限:密碼的最大使用期限:密碼的警告時間段:密碼禁用期:帳戶的過期日期:保留字段

一下子來了這麼多的用戶名與組名,還有UID與GID,密碼等,總得有個地方待吧:且看Linux系統的用戶管理信息庫,後續詳解:

Linux用戶和組的主要配置文件:    
    /etc/passwd:  用戶及其屬性信息(名稱、UID、主組ID等)    
    /etc/group: 組及其屬性信息    
    /etc/shadow: 用戶密碼及其使用信息    
    /etc/gshadow: 組密碼及其相關信息

useradd

主要用途

創建用戶

useradd - create a new user or update default new user information
useradd [options] LOGIN
useradd -D               # 顯示默認設置
useradd -D [options]    # 更改默認設置

useradd命令主要用於創建用戶,useradd、usermod、userdel是三種基本的用戶管理命令,且稱爲爲”用戶管理三劍客“吧!

常用參數

  1. -u: --uid, 指定用戶UID, 其中,[UID_MIN, UID_MAX]定義在/etc/login.defs文件中

  2. -o: --non-unique, 配合-u選項,不檢查UID的唯一性

  3. -g: --gid,指明用戶所屬基本組,可爲組名,可爲GID,必須事先存在

  4. -c: --comment, 用戶的註釋信息

  5. -d: --home-dir,以指定的路徑(不存在)爲用戶的家目錄

  6. -s: --shell, 指明默認的SHELL類型,可用列表在/etc/shells文件中

  7. -G: --groups, 爲用戶指明附加組,組必須事先存在

  8. -N: --no-user-group,不創建私有組作主組,使用users組做主組

  9. -r: --system, 創建系統用戶,CentOS 6: ID<500,CentOS 7: ID<1000

使用示例

  1. 創建一個帳號,指定其uid, 主組,附加組,默認shell,註釋信息,家目錄

[root@centos6 ~]# groupadd zhizhan  # 主組必須事先存在
[root@centos6 ~]# useradd zhizhan -u 510 -g zhizhan -G liansir -s /bin/csh -c "redhatzhizhan"
 -d /testdir/zhizhan
[root@centos6 ~]# 
[root@centos6 ~]# getent passwd zhizhan  # 從名稱服務切換庫裏面獲得所需信息
zhizhan:x:510:502:redhatzhizhan:/testdir/zhizhan:/bin/csh
[root@centos6 ~]# groups zhizhan  # 查看用戶所在組
zhizhan : zhizhan liansir
[root@centos6 ~]#
  1. 創建一個系統帳號,並忽略UID的唯一性

[root@centos6 ~]# useradd cent -r -u 455 -o
[root@centos6 ~]# getent passwd cent 
cent:x:455:455::/home/cent:/bin/bash
[root@centos6 ~]#

相關文件:man useradd

FILES
   /etc/passwd       User account information.    # 用戶帳號信息

   /etc/shadow
       Secure user account information.    # 安全的用戶帳號信息

   /etc/group
       Group account information.     # 組帳號信息

   /etc/gshadow
       Secure group account information.    # 安全的組帳號信息

   /etc/default/useradd
       Default values for account creation.   # 帳號創建的默認值

   /etc/skel/
       Directory containing default files.    # 用戶默認文件目錄

   /etc/login.defs
       Shadow password suite configuration.  
       # 影子口令配置(郵件、密碼日期、UID,系統用戶,是否創建家目錄,UMASK,加密算法等)

認識/etc/skel與/etc/login.defs

[root@centos6 skel]# ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla
[root@centos6 skel]#
[root@centos6 ~]# cat /etc/login.defs  # 只截取了常見的重要信息
MAIL_DIR        
/var/spool/mail

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

UID_MIN                   500
UID_MAX                 60000
GID_MIN                   500
GID_MAX                 60000

CREATE_HOME     yes

UMASK           077

USERGROUPS_ENAB yes      # 開啓空組則刪除功能ENCRYPT_METHOD SHA512

查看或更改創建用戶時的默認信息

[root@centos6 ~]# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@centos6 ~]# 
[root@centos6 ~]# useradd -D -s /bin/csh   # 更改用戶相關的默認信息
[root@centos6 ~]# useradd -D              # 查看用戶相關的默認信息
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/csh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@centos6 ~]# 
[root@centos6 ~]#

usermod

主要用途

用戶屬性修改

usermod - modify a user account
usermod [options] LOGIN

usermod命令主要用於修改用戶的基本屬性,但無法修改正在工作的用戶的屬性。

常用參數

  1. -u UID: 新UID

  2. -s GID: 新的默認SHELL

  3. -g: 新的基本組

  4. -G: 新的附加組,原來的附加組將被覆蓋,若要保留,則加-a,--apend.

  5. -a: --append, 追加

  6. -c: 新的註釋信息

  7. -d: 新家目錄不會自動創建,原家目錄中的文件不會同時移動至新家目錄裏面;若要創建新家目錄並要移動原家目錄的數據到新家目錄中,則加-m選項。

  8. -l: 新的登錄名

  9. -L: lock,鎖定用戶,在/etc/shadow密碼欄中增加 !

  10. -U: unlock, 解鎖用戶,去掉/etc/shadow密碼欄中的 !

  11. -e: --expiredate, format YYYY-MM-DD, 指定用戶帳號過期日期

  12. -f: --inactive, 設定非活動期限

使用示例

全面修改之前創建的zhizhan用戶:

[root@centos6 ~]# groupadd zhizhan
[root@centos6 ~]# useradd zhizhan -u 510 -g zhizhan -G liansir -s /bin/csh -c "redhatzhizhan"
 -d /testdir/zhizhan
[root@centos6 ~]#
[root@centos6 ~]# groupadd caocao     # 修改主組
[root@centos6 ~]# groupadd guanyu     # 追加附加組
[root@centos6 ~]# groupadd zhangfei   # 追加附加組
[root@centos6 ~]# usermod zhizhan -l lian -u 555 -g caocao -aG guanyu,zhangfei 
-s /bin/bash -c "xinsanguo" -d /home/shitou   
[root@centos6 ~]#

 # 查看修改結果
[root@centos6 ~]# getent passwd lian
lian:x:555:503:xinsanguo:/home/shitou:/bin/bash
[root@centos6 ~]# groups lian
lian : caocao liansir guanyu zhangfei
[root@centos6 ~]#

使用戶lian在22天之後處於非活動時間

[root@centos6 ~]# usermod lian -f 22
[root@centos6 ~]# getent shadow lian
lian:!!:17015:0:99999:7:22::
[root@centos6 ~]#

設置用戶lian的帳號過期時間

[root@centos6 ~]# usermod lian -e 2016-08-08
[root@centos6 ~]# getent shadow lian
lian:!!:17015:0:99999:7:22:17021:
[root@centos6 ~]#

給用戶設置密碼

[root@centos6 ~]# echo centos | passwd --stdin lian
Changing password for user lian.
passwd: all authentication tokens updated successfully.

鎖定用戶

[root@centos6 ~]# usermod -L lian


解鎖用戶

[root@centos6 ~]# usermod -U lian


相關文件

FILES
   /etc/group
       Group account information.   # 組帳號信息

   /etc/gshadow
       Secure group account information.    # 安全組帳號信息

   /etc/login.defs
       Shadow password suite configuration.

   /etc/passwd       User account information.

   /etc/shadow
       Secure user account information.

userdel

主要用途

刪除用戶

userdel - delete a user account and related files
userdel [options] LOGIN

userdel命令是一個簡單粗暴的命令,主要用於刪除用戶帳號及與其相關的文件,加參數則刪除用戶的相關文件,不加則只刪除帳號。

常用參數

  1. -r: --remove, 刪除用戶家目錄

使用示例

[root@centos6 ~]# userdel -r lian
userdel: lian home directory (/home/shitou) not found
[root@centos6 ~]#

至此,用戶管理三劍客的基本情況便介紹完成,接下來是組管理三劍客:groupadd, groupmod, groupdel. 其使用思路是類似的!


groupadd

主要用途

創建組

groupadd - create a new groupgroupadd [options] group

groupadd主要用來創建新的工作組,並將新創建組的信息添加到系統文件中。

常用參數

  1. -g GID: 指明GID號;[GID_MIN, GID_MAX]

  2. -r: 創建系統組;CentOS 6: ID<500,CentOS 7: ID<1000

  3. -K: --key KEY=VALUE, 覆蓋配置文件/etc/login.defs

    Example: -K GID_MIN=100 -K GID_MAX=499
  4. -o: 忽略GID

groupmod

主要用途

修改組屬性

常用參數

  1. -n group_name: 新組名

  2. -g GID: 新GID

groupdel

主要用途

刪除組

groupdel - delete a group
groupdel [options] GROUP

groupdel命令用來刪除指定的用戶組,該命令要修改的系統文件包括/etc/group與/etc/shadow,若要刪除的組中還包括一些用戶,則須刪除這些用戶之後再刪除相應的組,換言之,groupdel只能刪除空組,不能刪除非空組。

使用示例

[root@centos6 ~]# groupadd -g 550 fengcai    # 創建組fengcai
[root@centos6 ~]# useradd feng -g fengcai    # 創建用戶並加入組
[root@centos6 ~]# groupdel fengcai           # 刪除非空組
groupdel: cannot remove the primary group of user 'feng'
[root@centos6 ~]# userdel feng
[root@centos6 ~]# groupdel fengcai
[root@centos6 ~]#

練習

  1. 創建一個testuser,uid 1234,主組:bin, 輔助組:root,ftp,shell:/bin/csh home:/testdir/testuser

[root@centos6 ~]# useradd testuser -u 1234 -g bin -G root,ftp -s /bin/csh -d /testdir/testuser
[root@centos6 ~]#
[root@centos6 ~]# getent passwd testuser
testuser:x:1234:1::/testdir/testuser:/bin/csh
[root@centos6 ~]# id testuser
uid=1234(testuser) gid=1(bin) groups=1(bin),0(root),50(ftp)
[root@centos6 ~]# groups testuser
testuser : bin root ftp
[root@centos6 ~]#
  1. 修改testuser uid:4321,主組:root,輔助組:nobody,loginname:test,home:/home/test 家目錄的數據要遷移

[root@centos6 ~]# usermod testuser -u 4321 -g root -G nobody -l test -d /home/test
[root@centos6 ~]#
[root@centos6 ~]# getent passwd testtest:x:4321:0::/home/test:/bin/csh
[root@centos6 ~]# id test
uid=4321(test) gid=0(root) groups=0(root),99(nobody)
[root@centos6 ~]# groups test
test : root nobody
[root@centos6 ~]#
  1. 批量創建帳號:user1...user10,uid:3000-3009, shell:/bin/csh, home:/testdir/username,要求其密碼爲passwd:usernamepass格式

1. 複製/etc/skel文件到/home/testdir目錄下

[root@centos6 ~]# cp -r /etc/skel/ /home/testdir/
[root@centos6 ~]# cd /home/testdir/
[root@centos6 testdir]# ls -a.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla
[root@centos6 testdir]# 

2. 按照固定格式創建生成用戶的文件,如下:

[root@centos6 ~]# cat user.txt 
user1:x:3000:3000::/home/testdir/user1:/bin/csh
user2:x:3001:3001::/home/testdir/user2:/bin/csh
user3:x:3002:3002::/home/testdir/user3:/bin/csh
user4:x:3003:3003::/home/testdir/user4:/bin/csh
user5:x:3004:3004::/home/testdir/user5:/bin/csh
user6:x:3005:3005::/home/testdir/user6:/bin/csh
user7:x:3006:3006::/home/testdir/user7:/bin/csh
user8:x:3007:3007::/home/testdir/user8:/bin/csh
user9:x:3008:3008::/home/testdir/user9:/bin/csh
user10:x:3009:3009::/home/testdir/user10:/bin/csh
[root@centos6 ~]# 

3. 批量創建帳號命令:newusers
[root@centos6 ~]# newusers user.txt 
[root@centos6 ~]#

4. 按照固定格式創建生成密碼的文件,如下:

[root@centos6 ~]# cat passwd.txt 
user1:user1
user2:user2
user3:user3
user4:user4
user5:user5
user6:user6
user7:user7
user8:user8
user9:user9
user10:user10
[root@centos6 ~]# 

5. 批量爲用戶添加密碼:cat passwd.txt | chpasswd

[root@centos6 ~]# cat passwd.txt | chpasswd 
[root@centos6 ~]#

檢查是否符合要求:

[root@centos6 ~]# tail /etc/passwd
user1:x:3000:3000::/home/testdir/user1:/bin/csh
user2:x:3001:3001::/home/testdir/user2:/bin/csh
user3:x:3002:3002::/home/testdir/user3:/bin/csh
user4:x:3003:3003::/home/testdir/user4:/bin/csh
user5:x:3004:3004::/home/testdir/user5:/bin/csh
user6:x:3005:3005::/home/testdir/user6:/bin/csh
user7:x:3006:3006::/home/testdir/user7:/bin/csh
user8:x:3007:3007::/home/testdir/user8:/bin/csh
user9:x:3008:3008::/home/testdir/user9:/bin/csh
user10:x:3009:3009::/home/testdir/user10:/bin/csh[
root@centos6 ~]#

看是否能夠正常登錄:


注意點:家目錄相關配置,使用戶正常登錄

止戰


2016.8.2




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章