Linux基礎入門篇之用戶管理
groupadd,groupdel
useradd,usermod,userdel
passwd,chage
1
用戶/組基本概念
Users and groups:
. Every process (running program) on the system runs as a particular user.
. Every file is owned by a particular user.
. Access to files and directories are restricted by user.
. The user associated with a running process determines the files and directories accessible to that process.
查看當前登錄的用戶信息:
[root@tianyun ~]# id
uid=0(root) gid=0(root) groups=0(root)
查看文件的owner:
[root@tianyun ~]# ll /home/
drwx------. 4alicealice 4096 Jun 2 15:59 alice
drwx------. 2rootroot 16384 Jun 1 23:09 lost+found
drwxr-xr-x 2rootroot 4096 Jun 2 15:33 tianyun
查看運行進程的username:
[root@tianyun ~]# ps aux |less
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root1 0.0 0.0 19360 1536 ? Ss 13:29 0:00 /sbin/init
root2 0.0 0.0 0 0 ? S 13:29 0:00 [kthreadd]
root3 0.0 0.0 0 0 ? S 13:29 0:00 [migration/0]
root4 0.0 0.0 0 0 ? S 13:29 0:00 [ksoftirqd/0]
和用戶組相關的一些文件:
/etc/passwdroot:x::0:root:/root:/bin/bash
用戶名:x:uid:gid:描述:HOME:shell
/etc/shadowroot:$1$MYG2NDG6$a1wtyr5GDM2esAPjug0YP0:15636:0:99999:7:::
$id$salt$encrypted
/etc/grouproot:x::
[root@tianyun ~]# man 5 passwd
[root@tianyun ~]# man 5 shadow
[root@tianyun ~]# man 5 group
[root@tianyun ~]# man 3 crypt
加密演算法$id:
$1: MD5
$5: SHA-256
$6: SHA-512
系統約定: RHEL6
uid: 0 特權用戶
uid: 1~499 系統用戶
uid: 500+ 普通用戶
The root user
. uid is 0
. allpower
. This user has the power to override normalprivilegeson the file system
. installing or removing software and to manage system files and directorie
. Most devices can only be controlled by root
2
給普通用戶提權
以下兩種方式都可以將普通用戶提升為root
1.
Switching users with su
[alice@tianyun ~]$ useradd u1
-bash: /usr/sbin/useradd: 許可權不夠
[alice@tianyun ~]$ su - root
password:
[root@tianyun ~]# useradd u1
2.
Running commands as root with sudo
以root身份授權普通用戶
[root@tianyun ~]# vim /etc/sudoers
%wheel ALL=(ALL) NOPASSWD: ALL
[root@tianyun ~]# useradd yangyang -G wheel
[root@tianyun ~]# id yangyang
uid=504(yangyang) gid=504(yangyang) 組=504(yangyang),10(wheel)
[yangyang@tianyun ~]$ useradd gougou10
-bash: /usr/sbin/useradd: 許可權不夠
[yangyang@tianyun ~]$ sudo useradd gougou10
[yangyang@tianyun ~]$ id gougou10
uid=505(gougou10) gid=505(gougou10) 組=505(gougou10)
3
用戶/組管理
用戶組:
[root@tianyun ~]# groupadd hr
[root@tianyun ~]# groupadd sale
[root@tianyun ~]# groupadd it
[root@tianyun ~]# groupadd fd
[root@tianyun ~]# groupadd market
[root@tianyun ~]# groupadd net01 -g 2000 //添加組net01,並指定gid 2000
[root@tianyun ~]# grep net01 /etc/group //查看/etc/group中組net01信息
[root@tianyun ~]# groupdel net01 //刪除組net01
用戶:
useradd creates users
==創建用戶 未指定選項==
[root@tianyun ~]# useradd user01
. 未指定該用戶的主組
. 未指定該用戶的附加組
. 未指定用戶的HOME
. 未指定用戶的SHELL
. 未指定用戶的UID...
[root@tianyun ~]# grep user01 /etc/passwd /etc/shadow /etc/group
/etc/passwd:user01:x:507:512::/home/user01:/bin/bash
/etc/shadow:user01:!!:16589:0:99999:7:::
/etc/group:user01:x:512:
[root@tianyun ~]# id user01
uid=507(user01) gid=512(user01) groups=512(user01)
[root@tianyun ~]# ls /var/spool/mail/user01
/var/spool/mail/user01
小結:
如果創建一個用戶時,未指定任何選項,系統會創建一個和用戶名相同的組作為用戶的Primary Group.
==創建用戶 指定選項==
[root@tianyun ~]# useradd user02 -u 503 //創建用戶usr02,指定uid
[root@tianyun ~]# useradd user03 -d /aaa //創建用戶user03 指定家目錄
[root@tianyun ~]# useradd user05 -s /sbin/nologin //創建用戶並指定shell
[root@tianyun ~]# useradd user07 -G hr,it,fd //創建用戶,指定附加組
[root@tianyun ~]# useradd user10 -u 4000 -s /sbin/nologin
userdel deletes users
==刪除用戶==
[root@tianyun ~]# userdel user10 //刪除用戶user10,但不刪除用戶家目錄和mail spool
[root@tianyun ~]# ll -d /home/user10/
drwx------ 3 506 510 4096 09-01 21:14 /home/user10/
[root@tianyun ~]# ll /var/spool/mail/user10
-rw-rw---- 1 506 mail 0 09-01 21:14 /var/spool/mail/user10
[root@tianyun ~]# userdel -r user2 //刪除用戶user2,同時刪除用戶家目錄和mail spool
==用戶密碼==
[root@tianyun ~]# passwd alice
[zhuzhu@tianyun ~]$ passwd
usermod modifies existing users
==組成員管理==
注意:只針對已存在的用戶
[root@tianyun ~]# usermod -G hr niuniu2 //覆蓋原有的附加組
[root@tianyun ~]# usermod -G fd,it niuniu2
[root@tianyun ~]# usermod -aG hr niuniu2 //增加新的附加組
==其它選項管理==
[root@tianyun ~]# usermod -s /sbin/nologin niuniu2
4
擴展知識
useradd創建用戶時,參照的文件...
[root@tianyun ~]# vim /etc/login.defs
MAIL_DIR /var/spool/mail
PASS_MAX_DAYS 30
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
CREATE_HOME yes
ENCRYPT_METHOD SHA512
[root@tianyun ~]# vim /etc/default/useradd
SHELL=/sbin/nologin
Password aging
# chage -m 0 -M 90 -W 7 -I 14 username
chage -d 0 username will force a password update on next login.
chage -l username will list a username s current settings.
chage -E YYYY-MM-DD will expire an account on a specific day.
手動創建用戶示例:
[root@tianyun~]# useradd maomao100
[root@tianyun~]# echo 123456 |passwd --stdin maomao100
[root@tianyun~]# chage -d 0 maomao100 //強制用戶在下一次登錄時修改密碼
將多個用戶加入到指定的組:
[root@tianyun~]# gpasswd -a jack wheel //usermod -aG hr zhuzhu
[root@tianyun~]# gpasswd -M zhuzhu,maomao100 hr
[root@tianyun~]# gpasswd -d zhuzhu hr
點擊展開全文
※網路爬蟲基本原理
※中興,他就是傳說中的南慕容!
※年薪40萬以上的職位,IT類行業佔比最高
TAG:千鋒教育 |