?添加用戶密碼(只有root才能執(zhí)行)
?Passwd設(shè)定密碼
? [root@oldboy ~]# useradd wxb
? [root@oldboy ~]# passwd wxb
? Changing password for user wxb.
? New password:
? BAD PASSWORD: The password is a palindrome
? Retype new password:
? passwd: all authentication tokens updated successfully.
echo "123"|passwd --stdin name
-? [root@oldboy ~]# echo "123"|passwd --stdin gb
? Changing password for user gb.
? passwd: all authentication tokens updated successfully.
批量創(chuàng)建用戶并設(shè)置密碼
- [root@oldboy ~]# cat 12.txt
? for i in {1..20}
? do
? ? ? useradd wxb$i
? ? ? echo "123456" | passwd --stdin wxb$i
? done
- [root@oldboy ~]# bash 12.txt
? Changing password for user wxb1.
? passwd: all authentication tokens updated successfully.
? Changing password for user wxb2.
? passwd: all authentication tokens updated successfully.
? Changing password for user wxb3.
? passwd: all authentication tokens updated successfully.
? Changing password for user wxb4.
? passwd: all authentication tokens updated successfully.
? Changing password for user wxb5.
? passwd: all authentication tokens updated suc
用戶變更密碼
- 1)為自己修改密碼(OK)直接使用passwd注意密碼復(fù)雜程度(8位)
? 2)為別人修改密碼(root)passwd username
如何設(shè)置復(fù)雜密碼
- randow
? [root@oldboy ~]# echo $RANDOM|md5sum|cut -c 5-14
? 62632c5185
- mkpasswd
? [root@oldboy ~]# mkpasswd
? b3-q3GKrc
? [root@oldboy ~]# mkpasswd -l 10 -d 2 -c 3 -C 3 -s 2
? nz(]5Ks5BR
- lastpass
? 在線支持 windows macOS Iphone Ansroid 瀏覽器插件
總結(jié)
- 1.為新用戶添加密碼 只有root權(quán)限才可以
- 2.為用戶變更密碼也只有root才可以
- 3.普通用戶只能修改自己的密碼,..無(wú)法修改其他人的密碼
- 4.密碼的修改方式有兩種,一種是交互式 非交互
用戶創(chuàng)建流程
在用戶創(chuàng)建的過(guò)程中參考/etc/login.defs與/etc/default/useradd文件(默認(rèn))
如果在創(chuàng)建過(guò)程中指定了參數(shù)就不會(huì)默認(rèn)
- [root@oldboy ~]# grep "^[a-Z]" /etc/**login.defs**? ?
? MAIL_DIR /var/spool/mail? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #創(chuàng)建的郵箱所在位置
? PASS_MAX_DAYS 99999? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #密碼最長(zhǎng)使用天數(shù)
? PASS_MIN_DAYS 0? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #密碼最短使用天數(shù)
? PASS_MIN_LEN 5? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #密碼的長(zhǎng)度
? PASS_WARN_AGE 7? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #密碼到期前7天提示
? UID_MIN? ? ? ? ? ? ? ? ? 1000 ? #uid普通用戶開(kāi)始
? UID_MAX? ? ? ? ? ? ? ? 60000 #uid普通用戶最大
? SYS_UID_MIN? ? ? ? ? ? ? 201 ? #系統(tǒng)用戶uid開(kāi)始
? SYS_UID_MAX? ? ? ? ? ? ? 999 ? #系統(tǒng)用戶uid結(jié)束
? GID_MIN? ? ? ? ? ? ? ? ? 1000 ?
? GID_MAX? ? ? ? ? ? ? ? 60000
? SYS_GID_MIN? ? ? ? ? ? ? 201
? SYS_GID_MAX? ? ? ? ? ? ? 999
? CREATE_HOME yes #給用戶創(chuàng)建家目錄開(kāi)關(guān)
? UMASK? ? ? ? ? 077
? USERGROUPS_ENAB yes
? ENCRYPT_METHOD SHA512
- [root@oldboy ~]# cat /etc/**default/useradd**
? useradd defaults file
? GROUP=100 ? #當(dāng)用戶創(chuàng)建不指定組并且CREATE_HOME? no時(shí)默認(rèn)gid為100
? HOME=/home #用戶默認(rèn)家目錄
? INACTIVE=-1 ? #用戶不失效
? EXPIRE= ? #用戶過(guò)期時(shí)間
? SHELL=/bin/bash ? #默認(rèn)登錄shell
? SKEL=/etc/skel ? #默認(rèn)拷貝用戶的環(huán)境變量
? CREATE_MAIL_SPOOL=yes #創(chuàng)建郵箱開(kāi)關(guān)
用戶組管理
/etc/group
- ?? ? root?? ? ? ? ? ?:? ? ? ? ? ? x? ? ? ? ? ? ? ? :? ? ? ? ? ? ? 0? ? ? ? ? ? ? :
? 組的名稱(chēng)? ? ? ? ? ? ? ? 組的密碼? ? ? ? ? ? ? ? ? 組的gid? ? ? ? ? ? ? 顯示組的附加組不顯示基本成員
?/etc/gshadow
- ?? ? root? ? ? ? ? :? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :
? 組的名稱(chēng)? ? ? ? ? ? ? 組的密碼? ? ? ? ? ? ? ? 組的管理員? ? ? ? ? ? ? 顯示附加組成員不顯示基本組成員
?創(chuàng)建組(groupadd)
- [root@oldboyedu ~]# groupadd zhuzhu
? [root@oldboyedu ~]# groupadd -g 6666 gougou
? [root@oldboyedu ~]# grep "6666" /etc/group gougou?x:6666:
? 創(chuàng)建系統(tǒng)組
- [root@oldboyedu ~]# groupadd -r maomao
? [root@oldboyedu ~]# grep "maomao" /etc/group maomao?x:993:
修改組(groupmod)
- -g 修改組gid
? [root@oldboyedu ~]# groupmod -g 7777 gougou
? [root@oldboyedu ~]# grep "7777" /etc/group gougou:x7777:
- -n 修改組名稱(chēng)
? [root@oldboyedu ~]# groupmod gougou -n gg
? [root@oldboyedu ~]# grep "7777" /etc/group gg:7777:
刪除組(groupdel)
如果要?jiǎng)h除基本組,必須刪除組中的用戶才可以刪除該組
- [root@oldboyedu ~]# groupadd dawang
? [root@oldboyedu ~]# groupadd laowang
? [root@oldboyedu ~]# useradd xiaowang
? [root@oldboyedu ~]# useradd gb -g laowang
? [root@oldboyedu ~]# usermod xiaowang -G laowang,dawang
? [root@oldboyedu ~]# id xiaowang
? uid=6775(xiaowang)gid=7778(xiaowang)groups=7778(xiaowang),7779(dawang),7780(laowang)
? [root@oldboyedu ~]# userdel -r xiaowang
? [root@oldboyedu ~]# groupdel dawang
? [root@oldboyedu ~]# groupdel laowang
? groupdel: cannot remove the primary group of user 'gb'
? [root@oldboyedu ~]# userdel -r gb
? [root@oldboyedu ~]# groupdel laowang
用戶提權(quán)
? **su** 切換用戶 如果切換用戶,需要知道用戶的密碼,不是很安全
? **sudo** 提權(quán)( root事先分配好權(quán)限 --> 關(guān)聯(lián)用戶 ) 安全 方便 但是復(fù)雜
?基本概念
- 1.交互式需要不停的交互
- 2.非交互式
- 3.登錄式shell需要用戶名以及密碼開(kāi)啟bash窗口
- 4.非登錄式shell不需要用戶名和密碼即可開(kāi)啟bash窗口
? su - 屬于登陸式shell珍促,su 屬于非登陸式shell,區(qū)別在于加載的**環(huán)境變量**不一樣桶蛔。
- su - username? 屬于登錄式shell? 會(huì)加載全部的環(huán)境變量
? ~/.bashrc ---->/etc/bashrc------> /etc/profile.d
- su? username 屬于非登錄式shell? ? 會(huì)加載部分環(huán)境變量(很有可能就會(huì)出現(xiàn)錯(cuò)誤清空)
? /etc/profile--->/etc/profile.d/1.sh--->~/.bash_profile--->~/.bashrc--->/etc/bashrc
- su 切換有缺點(diǎn)
? 需要知道用戶對(duì)應(yīng)的密碼說(shuō)明不是很安全
- sudo提權(quán)
? 1.預(yù)先分配好權(quán)限
? 2.在關(guān)聯(lián)對(duì)應(yīng)的用戶
?sudo提權(quán)
- [root@bgx ~]# visudo
- 1.使用sudo定義分組,這個(gè)系統(tǒng)group沒(méi)什么關(guān)系
? User_Alias OPS = oldboy,oldgirl
? User_Alias DEV = alex
- 2.定義可執(zhí)行的命令組,便于后續(xù)調(diào)用
? 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
- 3.使用sudo開(kāi)始分配權(quán)限
? OPS? ALL=(ALL) NETWORKING,SOFTWARE,SERVICES,STORAGE,DELEGATING,PROCESSES
? DEV? ALL=(ALL) SOFTWARE,PROCESSES
- 4.登陸對(duì)應(yīng)的用戶使用 sudo -l 驗(yàn)證權(quán)限
- 第二種方式:使用groupadd添加組,然后給組分配sudo的權(quán)限,如果有新 用戶加入,直接將用戶添加到該組.*
- 1.添加兩個(gè)真實(shí)的系統(tǒng)組,? group_dev group_op
? [root@www ~]# groupadd group_dev
? [root@www ~]# groupadd group_op
- 2.添加兩個(gè)用戶,? ? ?
? group_dev(user_a? user_b)?
? group_op(user_c? user_d)
? [root@www ~]# useradd user_a -G group_dev
? [root@www ~]# useradd user_b -G group_dev
? [root@www ~]# useradd user_c -G group_op
? [root@www ~]# useradd user_d -G group_op
- 3.記得添加密碼
? [root@www ~]# echo "1" | passwd --stdin user_a
? [root@www ~]# echo "1" | passwd --stdin user_b
? [root@www ~]# echo "1" | passwd --stdin user_c
? [root@www ~]# echo "1" | passwd --stdin user_d
- 4.在sudo中配置規(guī)則
? [root@www ~]# visudo? ?
? 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_dev ALL=(ALL) SOFTWARE? ??
? %group_op ALL=(ALL) SOFTWARE,PROCESSES
- 5.檢查sudo是否配置有錯(cuò)
? [root@www ~]# visudo -c /etc/sudoers: parsed OK
- 6.檢查user_a,和user_d的sudo權(quán)限
? sudo -l
? User user_a may run the following commands on www:? ?
? (ALL) /bin/rpm, /usr/bin/yum
? sudo -l
? User user_d may run the following commands on www:? ?
? (ALL) /bin/rpm, /usr/bin/yum, /bin/nice, /bin/kill, /usr/bin/kil