4

Linux-day04

用戶管理

[root@qls ~]# head  -1  /etc/passwd
root:x:0:0:root:/root:/bin/bash

以:爲分隔符

root        #用戶名

x           #密碼佔位符

0           #用戶id

0           #用戶組id/GID

root        #用戶註釋信息

/root       #家目錄

/bin/bash   #用戶登陸的shell

[root@qls ~]# head  -1  /etc/shadow
root:$6$PZUVzAeQdrf8bOt5$6fhIZlLOf6EoweOMBbv7JMKaA6hCwfBUAHfNbvXI6eXl2ePXJZmmciXQk3yfcUesYRRjvD.Tk6VXcDl0wkV6d.::0:99999:7:::

[root@qls ~]# head  -1  /etc/shadow

跟用戶相關的命令
useradd
    選項
        -u      #設置uid
        -g      #指定GID,組名稱,前提組要存在
        -c      #設置註釋信息
        -s      #指定登陸shell
        -M      #不建立家目錄
        -G      #指定附加組
        -r      #建立系統用戶,不建立家目錄
        -d      #指定家目錄
        
usermod
    選項
        -u      #修改uid
        -g      #修改組信息
        -c      #修改註釋信息
        -s      #修改登陸shell
        -l      #修改用戶名稱
        -G      #添加附加組
        -a      #追加附加組

userdel     #刪除用戶,默認不刪除家目錄和郵件信息
    -r      #刪除家目錄和郵件信息

CentOS-7
0       #root  超級管理員
1-200   #系統用戶,進程服務運行的用戶
201-999 #系統用戶,服務軟件運行的用戶
1000+   #普通用戶

用戶的建立過程
[root@qls ~]# ll  /etc/login.defs 
-rw-r--r--. 1 root root 2028 Oct 30  2018 /etc/login.defs
[root@qls ~]# ll /etc/default/useradd 
-rw-r--r--. 1 root root 119 Oct 30  2018 /etc/default/useradd

[root@qls ~]# grep  -Ev  '^$|^#'  /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                  1000
UID_MAX                 60000
SYS_UID_MIN               201
SYS_UID_MAX               999
GID_MIN                  1000
GID_MAX                 60000
SYS_GID_MIN               201
SYS_GID_MAX               999
CREATE_HOME yes
UMASK           077
USERGROUPS_ENAB yes
ENCRYPT_METHOD SHA512 

[root@qls ~]# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

#用戶家目錄的環境變量不存在故障
-bash-4.2# pwd
/root
-bash-4.2# cp /etc/skel/.bash*  ./
-bash-4.2#

密碼管理

passwd      #設置密碼

passwd      #針對當前用戶設置密碼
passwd  username  #針對用戶設置密碼

root用戶能夠給任意用戶設置密碼,密碼沒有要求
普通用戶只能針對本身設置密碼,密碼必須最低是8位,且結構不能太簡單

非交互設置密碼
[root@qls ~]# echo  '1'  |passwd   --stdin    root

隨機密碼
[root@qls ~]# echo  $(echo  $RANDOM|md5sum |cut -c  1-10)|tee  pass.txt   |passwd   --stdin root

[root@qls ~]# yum  install -y expect

[root@qls ~]# mkpasswd   -l  24  -d 6  -c 6 -C 6 -s 6
#D4U;jT1D3k%hr@0ty4KP&4~

用戶組

基本組,一個用戶只能有一個基本組
附加組,用戶有多個附加組

用戶組相關配置文件

[root@qls ~]# ll /etc/group
-rw-r--r--. 1 root root 670 Aug 17 11:29 /etc/group
[root@qls ~]# ll /etc/gshadow
----------. 1 root root 524 Aug 17 11:29 /etc/gshadow

[root@qls ~]# cat /etc/group
root:x:0:

root    #用戶組名稱
x       #密碼佔位符
0       #GID
4列      #顯示該用戶屬於哪一個用戶附加組,就顯示這個用戶名稱,

[root@qls ~]# cat  /etc/gshadow
root:::


組相關命令
groupadd

    選項
        -g      #指定gid號
        
[root@qls ~]# groupadd   test-1
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# groupadd   test-2
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# useradd   -u 888  -g test-1 -G test-2  user-1
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# id  user-1
uid=888(user-1) gid=1005(test-1) groups=1005(test-1),1006(test-2)
[root@qls ~]# groupadd   test-3
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# usermod   -aG  test-3  user-1
[root@qls ~]# id  user-1
uid=888(user-1) gid=1005(test-1) groups=1005(test-1),1006(test-2),1007(test-3)
[root@qls ~]# groupadd   test-4
[root@qls ~]# 
[root@qls ~]# usermod  -G  test-4  user-1
[root@qls ~]# id  user-1
uid=888(user-1) gid=1005(test-1) groups=1005(test-1),1008(test-4)

groupmod

    選項
        -g      #修改gid
        -n      #修改組名稱
        
groupdel 
    用戶組不屬於任何用戶的基本組,能夠刪除,不然,不能刪除
    解決刪除用戶組屬於用戶的基本組
        1.修改該用戶的基本組
        2.刪除用戶組下面的用戶。

查看用戶的信息

id      #查看用戶信息

[root@qls ~]# echo  $USER
root
[root@qls ~]# echo $UID
0

w       #查看系統全部登陸的用戶

who     #查看系統登陸用戶

whoami  #查看當前登陸用戶

用戶受權

su  #切換用戶

su 和 su  -  有什麼區別

我的配置
-rw-r--r--. 1 root root  193 Aug 17 10:36 .bash_profile
-rw-r--r--. 1 root root  231 Aug 17 10:36 .bashrc

全局
[root@qls ~]# ll  /etc/profile
-rw-r--r--. 1 root root 1819 Oct 31  2018 /etc/profile
[root@qls ~]# ll  /etc/profile.d/*
[root@qls ~]# ll /etc/bashrc 
-rw-r--r--. 1 root root 2884 Aug 15 16:11 /etc/bashrc

profile     #系統環境變量,別名
bashrc      #本地變量和別名

我的和全局變量有衝突,我的優先全局

交互式shell
非交互式shell
登陸式shell
非登陸式shell

sudo

visudo   #受權

100 root    ALL=(ALL)       ALL
101 user01  ALL=(ALL)       /bin/cat,/bin/yum

    用戶名     主機名=(角色)  權限
user01  ALL=(ALL)       NOPASSWD:/bin/cat,/bin/yum
                        不須要輸入密碼
相關文章
相關標籤/搜索