我用 LinuxLinuxLinux学习之路

Day09-用户管理2(为用户设置密码,什么是组,su、sudo

2019-08-06  本文已影响39人  郝煜_Hyman

1.为用户添加密码 [root才能执行]

[root@oldboyedu ~]# passwd hyman1
Changing password for user oldxu. 
New password: 
BAD PASSWORD: The password is a palindrome 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@oldboyedu ~]# echo "hy123" | passwd --stdin hyman1
Changing password for user oldxu. 
passwd: all authentication tokens updated successfully.
[root@oldboyedu ~]# cat hy1.sh 
for i in {1..100} 
do    
  useradd test$i    
  echo "123456" | passwd --stdin test$i 
done

2.为用户变更密码


3.密码怎么才算复杂

root@haoyu1[15:35:29]~# echo $RANDOM 
19592
root@haoyu1[15:36:14]~# echo $RANDOM | md5sum
4bdacba7c227b64e08938e5d0e22da39  -
root@haoyu1[15:36:22]~# echo $RANDOM | md5sum | cut -c 5-15
abe0dff187c
root@haoyu1[15:38:49]~# yum install -y expect
-----------------------------------------------------------
root@haoyu1[15:44:52]~# mkpasswd -l 10 -d 2 -c 3 -C 3 -s 2 
3PV:t8/Tbe
root@haoyu1[15:45:29]~# mkpasswd -l 10 -d 2 -c 3 -C 3 -s 2 
EJao]18=zA

总结:


4.用户的创建流程

在用户创建的过程需要参考/etc/login.defs/etc/default/useradd这两 个文件,默认参考。
如果在创建用户时指定了参数,则会覆盖 (默认/etc/login.defs/etc/default/useradd)。
root@haoyu1[15:45:33]~# grep "^[a-Z]" /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
MAIL_DIR  /var/spool/mail #创建的邮箱所在的位置
PASS_MAX_DAYS  99999 #密码最长使用的天数
PASS_MIN_DAYS   0 #密码最短时间的天数
PASS_MIN_LEN    5 #密码的长度
PASS_WARN_AGE  7 #密码到期前7天警告
UID_MIN      1000 #uid 从1000开始
UID_MAX      60000 #uid从6w结束
SYS_UID_MIN    201 #系统用户的uid 从201 开始
SYS_UID_MAX    999 #系统用户的uid最大到 999
GID_MIN      1000
GID_MAX      60000
SYS_GID_MIN     201
SYS_GID_MAX     999
CREATE_HOME    yes #给用户创建家目录,创建 在/home
UMASK        077
USERGROUPS_ENAB yes
ENCRYPT_METHOD SHA512
root@haoyu1[15:49:58]~# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
GROUP=100 #当用户创建用户时不指定组,并 且/etc/login.defs中USERGROUPS_ENAB为no时, 用户默认创建给分 配一个gid为100的组.
HOME=/home #用户默认的家目录
INACTIVE=-1 #用户不失效
EXPIRE= #过期时间
SHELL=/bin/bash #默认登录shell
SKEL=/etc/skel #默认用户拷贝的环境变量
CREATE_MAIL_SPOOL=yes #创建邮箱

5.用户组的管理

1./etc/group配置文件内容解释:
root@haoyu1[16:01:24]~# head -1 /etc/group
root:x:0: #以:作为分隔符,总共4列
root:x:0: #以:作为分隔符,总共4列
*第一列 root 组的名称
*第二列 x 组的密码
*第三列 0 组GID
*第四列 显示附加组但不显示基本成员
2./etc/gshadow配置文件内容解释:
root@haoyu1[16:17:09]~# head -1 /etc/gshadow
root:::   #以:作为分隔符,总共4列
root::: #以:作为分隔符,总共4列
*第一列 root 组的名称
*第二列 x 组的密码
*第三列 0 组管理员
*第四列 显示附加组但不显示基本成员
3.创建组 groupadd [-g GID] groupname
root@haoyu1[16:18:42]~# groupadd haoyu1
root@haoyu1[16:25:12]~# groupadd -g 4567 haoyu2
-----------------------------------------------------------
root@haoyu1[16:25:47]~# grep "4567" /etc/group
haoyu2:x:4567:
4.修改组 groupmod
root@haoyu1[16:26:03]~# groupmod -g 1234 haoyu1
root@haoyu1[16:28:09]~# grep "1234" /etc/group
haoyu1:x:1234:
root@haoyu1[16:28:22]~# groupmod haoyu2 -n hy2
root@haoyu1[16:28:47]~# grep "hy2" /etc/group
hy2:x:4567:
5.删除组如果要删除基本组,需要先删除基本组中的用户才可以删除该组。

实验图:

练习
root@haoyu1[16:42:21]~# groupadd dahao
root@haoyu1[16:42:29]~# groupadd laohao

root@haoyu1[16:42:35]~# useradd xiaohao
root@haoyu1[16:42:42]~# useradd hy -g laohao
root@haoyu1[16:42:52]~# usermod xiaohao -G laohao,dahao
-----------------------------------------------------------
root@haoyu1[16:43:12]~# id xiaohao
uid=6669(xiaohao) gid=6671(xiaohao) groups=6671(xiaohao),6669(dahao),6670(laohao)
root@haoyu1[16:43:41]~# userdel -r xiaohao
root@haoyu1[16:43:48]~# groupdel dahao
root@haoyu1[16:43:54]~# groupdel laohao
groupdel: cannot remove the primary group of user 'hy'
root@haoyu1[16:43:59]~# userdel -r hy
root@haoyu1[16:44:37]~# groupdel laohao

6.用户提权

基本概念:

su - username 属于登陆式shell,su username 属于非登陆式shell,区别 在于加载的环境变量不一样。

su - username 属于登录式shell 会加载全部的环境变量
su username 属于非登录式shell 会加载部分环境变量(很有 可能就会出现错误清空)

第一种方式:使用sudo中自带的别名操作,将多个用户定义成一个组。
root@haoyu1[16:59:21]~# visudo
visudo: /etc/sudoers.tmp unchanged
User_Alias OPS = haoyu1,hyman1  
User_Alias DEV = hy1
Cmnd_Alias NETWORKING = /sbin/ifconfig, /bin/ping 
Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/yum 
Cmnd_Alias SERVICES = /sbin/service, /usr/bin/systemctl start 
Cmnd_Alias STORAGE = /bin/mount, /bin/umount 
Cmnd_Alias DELEGATING = /bin/chown, /bin/chmod, /bin/chgrp 
Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
OPS  ALL=(ALL) NETWORKING,SOFTWARE,SERVICES,STORAGE,DELEGATING,PROCESSES 
DEV  ALL=(ALL) SOFTWARE,PROCESSES
第二种方式:使用groupadd添加组,然后给组分配sudo的权限,如果有新用户加入,直接将用户添加到该组。
root@haoyu1[17:12:30]~# groupadd group_1
root@haoyu1[17:12:51]~# groupadd group_2
root@haoyu1[17:14:30]~# useradd hyman_a -G group_1 
root@haoyu1[17:14:30]~# useradd hyman_b -G group_1
root@haoyu1[17:14:30]~# useradd hyman_c -G group_2
root@haoyu1[17:14:30]~# useradd hyman_d -G group_2
root@haoyu1[17:15:50]~# echo "1" | passwd --stdin hyman_a 
Changing password for user hyman_a.
passwd: all authentication tokens updated successfully.
root@haoyu1[17:15:51]~# echo "1" | passwd --stdin hyman_b 
Changing password for user hyman_b.
passwd: all authentication tokens updated successfully.
root@haoyu1[17:15:51]~# echo "1" | passwd --stdin hyman_c 
Changing password for user hyman_c.
passwd: all authentication tokens updated successfully.
root@haoyu1[17:15:51]~# echo "1" | passwd --stdin hyman_d
Changing password for user hyman_d.
passwd: all authentication tokens updated successfully.
Cmnd_Alias NETWORKING = /sbin/ifconfig, /bin/ping
Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/yum
Cmnd_Alias SERVICES = /sbin/service, /usr/bin/systemctl start
Cmnd_Alias STORAGE = /bin/mount, /bin/umount
Cmnd_Alias DELEGATING = /bin/chown, /bin/chmod, /bin/chgrp
Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall

%group_1 ALL=(ALL) SOFTWARE
%group_2 ALL=(ALL) SOFTWARE,PROCESSES

root@haoyu1[17:19:22]~# visudo -c
/etc/sudoers: parsed OK
[hyman_a@haoyu1 ~]$ sudo -l

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.

[sudo] password for hyman_a: 
Matching Defaults entries for hyman_a on haoyu1:
    !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin,
    env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS",
    env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE",
    env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES",
    env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE",
    env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin

User hyman_a may run the following commands on haoyu1:
    (ALL) /bin/rpm, /usr/bin/yum


-----------------------------------------------------------


[hyman_d@haoyu1 ~]$ sudo -l

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.

[sudo] password for hyman_d: 
Matching Defaults entries for hyman_d on haoyu1:
    !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin,
    env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS",
    env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE",
    env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES",
    env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE",
    env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin

User hyman_d may run the following commands on haoyu1:
    (ALL) /bin/rpm, /usr/bin/yum, /bin/nice, /bin/kill, /usr/bin/kill,
        /usr/bin/killall
上一篇下一篇

猜你喜欢

热点阅读