1. 程式人生 > >Linux用戶、組及權限

Linux用戶、組及權限

ons fin its min tps root separate extend swd

一、Linux 3A+
 通過3A進行資源的分派,所謂3A即:

* Authentication:As the first process, authentication provides a way of identifying a user, typically by having the user enter a valid user name and valid password before access is granted. The process of authentication is based on each user having a unique set of criteria for gaining access. The AAA server compares a user‘s authentication credentials with other user credentials stored in a database. If the credentials match, the user is granted access to the network. If the credentials are at variance, authentication fails and network access is denied.
* Authorization:Following authentication, a user must gain authorization for doing certain tasks. After logging into a system, for instance, the user may try to issue commands. The authorization process determines whether the user has the authority to issue such commands. Simply put, authorization is the process of enforcing policies: determining what types or qualities of activities, resources, or services a user is permitted. Usually, authorization occurs within the context of authentication. Once you have authenticated a user, they may be authorized for different types of access or activity.
* Accounting: The final plank in the AAA framework is accounting, which measures the resources a user consumes during access. This can include the amount of system time or the amount of data a user has sent and/or received during a session. Accounting is carried out by logging of session statistics and usage information and is used for authorization control, billing, trend analysis, resource utilization, and capacity planning activities.

 除了3A,還引入了審計,作用如下:

* Audition:針對操作進行“合法性”檢測

二、安全上下文

 A security context, or security label, is the mechanism used by SELinux to classify resources, such as processes and files, on a SELinux-enabled system. This context allows SELinux to enforce rules for how and by whom a given resource should be accessed. A security context is typically shown as a string consisting of three or four words. Each word specifies a different component of the security context, namely the user, role, type, and level of that file or process. Each word is separated by a colon. e.g.

 The security context for files is stored in the form of an extended file attribute. A file‘s security context may be viewed using ls(1) with the -Z option. It may be modified using chcon(1), whose syntax is intentionally similar to chmod. A file‘s security context is associated with the file‘s inode and not the location or directory; this ensures that security contexts will remain the same regardless of whether a file is moved or renamed.
 Some filesystems do not support extended file attributes. In these cases, the kernel provides a default security context for that filesystem. For example, all files on an NFS filesystem are labeled "system_u:object_r:nfs_t".

refer to: https://fedoraproject.org/wiki/Security_context
https://blog.csdn.net/hongbochen1223/article/details/45176195

三、用戶、組及權限相關文件詳解

用戶:
用戶類型用戶名稱用戶ID管理員用戶root0普通用戶
系統用戶(針對守護進程獲取資源進行權限分配)
登錄用戶(交互式登錄)
1-499(CentOS6 ↓);1-999(CentOS7)
500+(CentOS6 ↓);1000+(CentOS7)

組:
組類型組名稱組ID管理員組root0普通組
系統組
普通組
1-499(CentOS6 ↓);1-999(CentOS7)

500+(CentOS6 ↓);1000+(CentOS7)

組類別:

* 主組:

      用戶有且僅有一個主組
           默認主組為創建用戶時自動創建,組名與用戶名相同,只包含該同名用戶,且作為該用戶的私有組
           如果創建用戶時,使用-g指定主組,則該用戶的主組為指定名稱的組,且僅包含該用戶(此時不會創建同名的組!!)

* 附加組:也稱輔助組,一個用戶可以屬於零個或多個附加組

用戶和組的配置文件

 /etc/passwd:用戶及其屬性信息(名稱、 UID、主組ID等)
 /etc/group:組及其屬性信息
 /etc/shadow:用戶密碼及其相關屬性
 /etc/gshadow:組密碼及其相關屬性
 /etc/default/useradd:useradd defaults configuration file
 /etc/skel/*:用戶home目錄的默認配置文件
 /etc/login.defs:shadow password suite configuration,用戶默認密碼屬性配置文件

 文件字段詳解:

* /etc/passwd

①用戶名:②密碼占位符:③用戶ID:④主組ID:⑤用戶描述:⑥HOME目錄:⑦SHELL類型

* /etc/shadow

①用戶名:②用戶密碼(加密:a部分為使用的加密算法;b部分為加密密碼):③最近一次密碼更改時間(從1970年1月1日起計算的天數):④密碼更改等待天數(0表示隨時可改):⑤密碼必須更改期限(密碼過期期限,99999表示永不過期):⑥密碼過期提醒天數(密碼過期前幾天提醒,默許為7天):⑦密碼過期鎖定(過期後幾天鎖定):⑧多少天後帳號失效(從1970年1月1日起計算的天數)

* /etc/group

①組名:②密碼占位符:③組ID:④以當前組為附加組的用戶列表(逗號分隔)

* /etc/gshadow

①組名:②組密碼:③組管理員列表:④以當前組為附加組的用戶列表(逗號分隔)

 密碼文件工具

      密碼文件快捷編輯
      vipw:= vi /etc/passwd
      vigr: = vi /etc/group
      密碼文件檢驗
      pwck:檢驗/etc/passwd
      grpck:檢驗/etc/group

 密碼加密

? 加密機制:
加密:明文--> 密文
解密:密文--> 明文
? 單向加密:哈希算法,原文不同,密文必不同
相同算法定長輸出,獲得密文不可逆推出原始數據
雪崩效應:初始條件的微小改變,引起結果的巨大改變(蝴蝶效應)

md5: message digest, 128bits
sha1: secure hash algorithm, 160bits
sha224: 224bits
sha256: 256bits
sha384: 384bits
sha512: 512bits(CentOS7默認)
? 更改加密算法:
authconfig --passalgo=sha256 --update

 密碼復雜度策略

* 使用數字、大寫字母、小寫字母及特殊字符中至少3種
* 足夠長度
* 使用隨機密碼
* 定期更換,不使用近期曾用密碼

四、用戶、組及權限相關命令

useradd:添加用戶(相同命令adduser)
-u:UID,指定用戶ID
-o:配合-u 選項,不檢查UID的唯一性
-g:GID,指明用戶所屬基本組,可為組名,也可以GID
-c:"COMMENT",用戶的註釋信息
-d:HOME_DIR,以指定的路徑(不存在)為家目錄
-s:SHELL,指明用戶的默認shell程序,可用列表在/etc/shells文件中
-G:GROUP1[,GROUP2,...],為用戶指明附加組,組須事先存在
-N:不創建私有組作為主組,使用users組作主組
-r:創建系統用戶(CentOS 6:ID<500;CentOS 7: ID<1000)

-m:創建家目錄
-M:不創建家目錄,用於非系統用戶(/etc/passwd中有,但實際沒有創建)

      -D:顯示創建用戶時,默認參數

passwd:修改用戶口令
-d:刪除指定用戶密碼
-l: 鎖定指定用戶
-u:解鎖指定用戶
-e:強制用戶下次登錄修改密碼,等同於chage -d
-f:強制操作
-n min days:指定最短使用期限
-x max days: 最大使用期限
-w warn days:提前多少天開始警告
-i inactive days:非活動期限
--stdin:從標準輸入接收用戶密碼
echo "PASSWORD" | passwd --stdin USERNAME

usermod:更改用戶屬性
-u:UID: 新UID
-g:GID: 新主組
-G:GROUP1[,GROUP2,...[,GROUPN]]]:新附加組,原來的附加組將會被覆蓋;若保留原有,則要同時使用-a選項
-s:SHELL:新的默認SHELL
-c:‘COMMENT‘:新的註釋信息
-d:HOME: 新家目錄不會自動創建;若要創建新家目錄並移動原家數據,同時使用-m選項
-l:login_name: 新的名字(改變用戶名稱)
-L:lock指定用戶,在/etc/shadow 密碼欄的增加 !
-U:unlock指定用戶,將 /etc/shadow 密碼欄的 ! 拿掉
-e:YYYY-MM-DD: 指明用戶賬號過期日期
-f:INACTIVE: 設定非活動期限

chage
-d LAST_DAY,等同於passwd -e
-E --expiredate EXPIRE_DATE
-I --inactive INACTIVE
-m --mindays MIN_DAYS
-M --maxdays MAX_DAYS
-W --warndays WARN_DAYS
–l 顯示密碼策略
示例:
chage -d 0 tom 下一次登錄強制重設密碼
chage -m 0 –M 42 –W 14 –I 7 tom
chage -E 2016-09-10 tom

userdel:刪除用戶
-r: 一並刪除用戶家目錄

finger:顯示用戶信息

chfn:更改已有用戶相關信息(finger顯示的)

chsh:更改已有用戶的shell

su:
切換用戶的方式:
su UserName:非登錄式切換,即不會讀取目標用戶的配置文件,不改變當前工作目錄
su - UserName:登錄式切換,會讀取目標用戶的配置文件,切換至家目錄,完全切換
root su至其他用戶無須密碼;非root用戶切換時需要密碼

換個身份執行命令:
su [-] UserName -c ‘COMMAND‘

選項: -l --login
su -l UserName 相當於 su - UserName

newusers passwd格式文件:批量創建用戶

chpasswd 批量修改用戶口令

groupadd:添加新組
-g GID:指明GID號
-r:創建系統組
CentOS 6: ID<500
CentOS 7: ID<1000

groupmod:更改組屬性
-n group_name: 新名字(更改組名稱)
-g GID: 新的GID

groupdel:刪除組
groupdel GROUP_NAME

gpasswd:更改組密碼(默認)
-a user:將user添加至指定組中
-d user:從指定組中移除用戶user
-A user1,user2,... :設置有管理權限的用戶列表

newgrp命令:臨時切換主組
如果用戶本不屬於此組,則需要組密碼

groupmems:
-g:--group groupname 更改為指定組 (只有root)
-a:--add username 指定用戶加入組
-d:--delete username 從組中刪除用戶
-p:--purge 從組中清除所有成員
-l: --list 顯示組成員列表
?groups [OPTION].[USERNAME]... 查看用戶所屬組列表

id:顯示用戶和組ID
-u: 顯示UID
-g: 顯示GID
-G: 顯示用戶所屬的組的ID
-n: 顯示名稱,需配合ugG使用

Linux用戶、組及權限