管理用户和组账号;管理目录和文件的属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了管理用户和组账号;管理目录和文件的属性相关的知识,希望对你有一定的参考价值。

一、管理用户和组账号
二、管理目录和文件的属性

一、管理用户和组账号
1、概述
Linux中通过用户账户来设置资源的访问控制
用户账号类型:
超级用户:root uid为0
普通用户: uid从500开始
程序用户;维护一个程序/服务的正常运行,通常不允许登录到系统中

组账号类型:
基本组(私有组):
附加组(公共组):

2、用户账号的管理
a、用户账号
存放在/etc/passwd中,每一行表示一个用户账号。
每一行有7个字段组成,每一个字段的含义是:
第一段:用户名
第二段:密码占位符
第三段:UID编号
第四段:GID编号
第五段:用户备注
第六段:用户宿主目录
第七段:登录的shell 若为/sbin/nologin则不能登录

  创建账号:
格式:useradd [选项]  用户名称
选项:
  -d:指定宿主目录,缺省默认为/home/用户名
   例如:useradd -d /ftphome/bob   bob
  -u:指定UID标记号
  -g:指定用户的基本组名或GID号
  -G:指定用户的附加组名或GID号
  -M:不为用户建立初始化宿主目录
  -e:指定账户失效时间
  -s:指定用户登录的shell

[[email protected] ~]# 创建用户mike,基本组为mike,附加组为ftpuser。宿主目录为/ftphome/mike,不允许登录当前服器
分析:要想创建mike,则必须先创建mike组及ftpuser组同时必须保证/ftphome目录存在
[[email protected] ~]# groupadd mike
[[email protected] ~]# groupadd ftpuser
[[email protected] ~]# mkdir /ftphome
[[email protected] ~]# useradd -u 510 -g mike -G ftpuser -d /ftphome/mike -s /sbin/nologin mike
[[email protected] ~]# tail -2 /etc/passwd
bob:x:500:500::/home/bob:/bin/bash
mike:x:510:501::/ftphome/mike:/sbin/nologin
[[email protected] ~]# su - mike //验证mike无法登陆服务器
This account is currently not available.
[[email protected] ~]# su - bob
[[email protected] ~]$ whoami
bob
[[email protected] ~]$ exit
logout
[[email protected] ~]#

   每一个普通用户的宿主目录下都会有一些隐藏文件(初始配置文件),这些文件都来自于/etc/skel
~/.bash_profile: 用户登录时执行
~/.bashrc:用户切换shell时执行
~/.bash_logout:用户退出时执行

b、用户账号的密码
  存放在/etc/shadow中,每一行表示一个用户账号的密码。
  每一行有9个字段组成,每一个字段的含义是:
   第一段:
   第二段:
   第三段:
   ……
   第九段:

  创建账号:
格式:passwd [选项]  用户名称
选项:
  -d:清空用户密码
   例如:useradd -d    bob
  -l:锁定用户账户
  -u:解锁用户账户
  -S:查看用户状态

======================================================创建用户并设置密码==============================================================================
[[email protected] ~]# useradd tom
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/bin/bash
[[email protected] ~]# tail -1 /etc/shadow
tom:!!:17290:0:99999:7:::
[[email protected] ~]# passwd tom
更改用户 tom 的密码 。
新的 密码:
无效的密码: 过于简单化/系统化
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/bin/bash
[[email protected] ~]# tail -1 /etc/shadow
tom:$6$EwXMoNdu$cmZR2/jhqtV3MfetcYJt7S1vKBe05O5Wtw49Su/lQ02U/0olG7wZi3.JBe.cCnRjuI8MLTqEwcTB.80UFKIAb/:17290:0:99999:7:::
[[email protected] ~]#

====================================================锁定用户tom=======================================================================================
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/bin/bash
[[email protected] ~]# tail -1 /etc/shadow
tom:$6$EwXMoNdu$cmZR2/jhqtV3MfetcYJt7S1vKBe05O5Wtw49Su/lQ02U/0olG7wZi3.JBe.cCnRjuI8MLTqEwcTB.80UFKIAb/:17290:0:99999:7:::
[[email protected] ~]# passwd -l tom
锁定用户 tom 的密码 。
passwd: 操作成功
[[email protected] ~]# passwd -S tom
tom LK 2017-05-04 0 99999 7 -1 (密码已被锁定。)
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/bin/bash
[[email protected] ~]# tail -1 /etc/shadow
tom:!!$6$EwXMoNdu$cmZR2/jhqtV3MfetcYJt7S1vKBe05O5Wtw49Su/lQ02U/0olG7wZi3.JBe.cCnRjuI8MLTqEwcTB.80UFKIAb/:17290:0:99999:7::: //密码字段前加有两个!则为密码锁定

====================================================解锁用户tom=======================================================================================
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/bin/bash
[[email protected] ~]# tail -1 /etc/shadow
tom:!!$6$EwXMoNdu$cmZR2/jhqtV3MfetcYJt7S1vKBe05O5Wtw49Su/lQ02U/0olG7wZi3.JBe.cCnRjuI8MLTqEwcTB.80UFKIAb/:17290:0:99999:7:::
[[email protected] ~]# passwd -S tom
tom LK 2017-05-04 0 99999 7 -1 (密码已被锁定。)
[[email protected] ~]# passwd -u tom
解锁用户 tom 的密码 。
passwd: 操作成功
[[email protected] ~]# passwd -S tom
tom PS 2017-05-04 0 99999 7 -1 (密码已设置,使用 SHA512 加密。)
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/bin/bash
[[email protected] ~]# tail -1 /etc/shadow
tom:$6$EwXMoNdu$cmZR2/jhqtV3MfetcYJt7S1vKBe05O5Wtw49Su/lQ02U/0olG7wZi3.JBe.cCnRjuI8MLTqEwcTB.80UFKIAb/:17290:0:99999:7:::

====================================================清空用户tom的密码===================================================================================
[[email protected] ~]# tail -1 /etc/shadow
tom:$6$EwXMoNdu$cmZR2/jhqtV3MfetcYJt7S1vKBe05O5Wtw49Su/lQ02U/0olG7wZi3.JBe.cCnRjuI8MLTqEwcTB.80UFKIAb/:17290:0:99999:7:::
[[email protected] ~]# passwd -d tom
清除用户的密码 tom。
passwd: 操作成功
[[email protected] ~]# tail -1 /etc/shadow
tom::17290:0:99999:7:::

 c、修改用户账号的属性
 格式:usermod [选项]  用户账户名称
 选项:
    -l:更改用户账户的登录名字
    -L:锁定用户账户
    -U:解锁用户账户
    -d:
    -e:
    -g、-G、-s、-u

修改账户tom的属性,使其不可以登录服务器;账户有效到2017-07-01,密码最少使用3天,密码最长使用30天,密码提5天警告
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/bin/bash
[[email protected] ~]# tail -1 /etc/shadow
tom::17290:0:99999:7:::
[[email protected] ~]# usermo
usermod usermount
[[email protected] ~]# usermod -s /sbin/nologin tom //修改tom的登录shell
[[email protected] ~]# tail -1 /etc/passwd
tom:x:512:512::/home/tom:/sbin/nologin
[[email protected] ~]# su - tom
This account is currently not available.
[[email protected] ~]# usermod -e 2017-07-01 tom //修改tom的失效时间
[[email protected] ~]# tail -1 /etc/shadow
tom::17290:0:99999:7::17348:
[[email protected] ~]# chage -m 3 -M 30 -W 5 tom //-m 指定密码最少使用天数 -M 指定密码最长使用天数 -W 提前多少天发出密码过期警告
[[email protected] ~]# tail -1 /etc/shadow
tom::17290:3:30:5::17348:
[[email protected] ~]#

=======================================================锁定、解锁tom=================================================================================================
[[email protected] ~]# tail -1 /etc/shadow
tom:$6$x7rdnfHv$W4nPwBYnt1rZfMEPFDTaAP0091LztxD7HJeGMPp9nvkd/3YcLyw5CfMAVFSQRNgmn.matyuYO44l3NJCzDNqu.:17290:3:30:5::17348:
[[email protected] ~]# usermod -L tom
[[email protected] ~]# tail -1 /etc/shadow
tom:!$6$x7rdnfHv$W4nPwBYnt1rZfMEPFDTaAP0091LztxD7HJeGMPp9nvkd/3YcLyw5CfMAVFSQRNgmn.matyuYO44l3NJCzDNqu.:17290:3:30:5::17348: //在密码字段前有一个!则为密码锁定
[[email protected] ~]# usermod -U tom
[[email protected] ~]# tail -1 /etc/shadow
tom:$6$x7rdnfHv$W4nPwBYnt1rZfMEPFDTaAP0091LztxD7HJeGMPp9nvkd/3YcLyw5CfMAVFSQRNgmn.matyuYO44l3NJCzDNqu.:17290:3:30:5::17348:

 d、删除用户:
格式:userdel [-r]  用户账号名称
userdel bob:删除用户账户
userdel -r bob:删除用户的同时删除用户的宿主目录

[[email protected] ~]# tail -2 /etc/passwd
jerry:x:511:511::/home/jerry:/bin/bash
tom:x:512:512::/home/tom:/sbin/nologin
[[email protected] ~]# ls /home/
bob jerry tom
[[email protected] ~]# userdel tom
[[email protected] ~]# tail -2 /etc/passwd
mike:x:510:501::/ftphome/mike:/sbin/nologin
jerry:x:511:511::/home/jerry:/bin/bash
[[email protected] ~]# ls /home/
bob jerry tom
[[email protected] ~]# userdel -r jerry
[[email protected] ~]# tail -2 /etc/passwd
bob:x:500:500::/home/bob:/bin/bash
mike:x:510:501::/ftphome/mike:/sbin/nologin
[[email protected] ~]# ls /home/
bob tom
[[email protected] ~]#

 e、passwd与usermod锁定及解锁有何区别?

3、组账号的管理
组账户存放于/etc/group,每一行表示一个组的信息:
组账号密码存放于/etc/gshadow 中。
a、创建组
格式:groupadd [-g GID] 组名称
groupadd -g 510 test(含义)

创建组TEST并指明id为1200
[[email protected] ~]# groupadd -g 1200 TEST
[[email protected] ~]# tail -1 /etc/group
TEST:x:1200:

b、向组中添加、删除成员
格式:gpasswd [选项] 组名称
选项:
-a:向组内添加一个用户
例如:gpass -a bob test:(含义)
-d:从组内删除一个用户成员
-M:定义组成员列表(添加多个用户),以逗号隔开

创建用户test1、test2、test3,并将这几个用户添加到TEST组中
[[email protected] ~]# useradd test1
[[email protected] ~]# useradd test2
[[email protected] ~]# useradd test3
[[email protected] ~]# grep "TEST" /etc/group
TEST:x:1200:
[[email protected] ~]# gpasswd -a test1 TEST
[[email protected] ~]# grep "TEST" /etc/group
TEST:x:1200:test1
[[email protected] ~]# gpasswd -a test2 TEST
Adding user test2 to group TEST
[[email protected] ~]# grep "TEST" /etc/group
TEST:x:1200:test1,test2
[[email protected] ~]# gpasswd -d test1 TEST
Removing user test1 from group TEST
[[email protected] ~]# grep "TEST" /etc/group
TEST:x:1200:test2
[[email protected] ~]# gpasswd -M test1,test3 TEST
[[email protected] ~]# grep "TEST" /etc/group
TEST:x:1200:test1,test3
[[email protected] ~]# gpasswd -a test2 TEST
Adding user test2 to group TEST
[[email protected] ~]# grep "TEST" /etc/group
TEST:x:1200:test1,test3,test2
[[email protected] ~]#

c、删除组
groupdel 组名称
例如:groupdel test (含义)

[[email protected] ~]# groupdel TEST
[[email protected] ~]# grep "TEST" /etc/group

4、查看用户账号信息
id:查询用户标识
groups:查询客户所属的组
finger:查询账户的详细信息
w:查询已经登录到主机的用户信息
users:
who:查询已经登录到主机的用户

[[email protected] ~]# id test1
uid=511(test1) gid=511(test1) 组=511(test1)
[[email protected] ~]# groups bob
bob : bob
[[email protected] ~]# groups mike
mike : mike ftpuser
[[email protected] ~]# finger
-bash: finger: command not found
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_centos6node1-lv_root 38776280 5757136 31049380 16% /
tmpfs 506176 76 506100 1% /dev/shm
/dev/sda1 495844 34897 435347 8% /boot
/dev/sr0 4363088 4363088 0 100% /media/CentOS_6.5_Final
[[email protected] ~]# rpm -ivh /media/CentOS_6.5_Final/Packages/fin
findutils-4.4.2-6.el6.x86_64.rpm finger-server-0.17-39.el6.x86_64.rpm
finger-0.17-39.el6.x86_64.rpm
[[email protected] ~]# rpm -ivh /media/CentOS_6.5_Final/Packages/finger-0.17-39.el6.x86_64.rpm
warning: /media/CentOS_6.5_Final/Packages/finger-0.17-39.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
1:finger ########################################### [100%]
[[email protected] ~]# finger mike
Login: mike Name:
Directory: /ftphome/mike Shell: /sbin/nologin
Never logged in.
No mail.
No Plan.
[[email protected] ~]#

[[email protected] ~]# users
root root root root root
[[email protected] ~]# who
root tty1 2017-05-04 08:22
root pts/0 2017-05-04 08:47 (192.168.100.15)
root pts/1 2017-05-04 08:47 (192.168.100.15)
root tty7 2017-05-04 11:04 (:0)
root pts/2 2017-05-04 11:04 (:0.0)
[[email protected] ~]# w
11:06:53 up 2:45, 5 users, load average: 0.19, 0.21, 0.09
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root tty1 - 08:22 3:04 0.06s 0.06s -bash
root pts/0 192.168.100.15 08:47 0.00s 0.31s 0.05s w
root pts/1 192.168.100.15 08:47 13:13 0.02s 0.02s -bash
root tty7 :0 11:04 2:44m 2.24s 2.24s /usr/bin/Xorg :0 -br -verbose -audit 4 -auth /v
root pts/2 :0.0 11:04 2:25 0.00s 0.00s /bin/bash

二、管理目录和文件的属性
1、文件或目录的属性
a、权限
r:可读
w:可写
x:可执行

b、归属
属主:默认为文件或目录的创建者(所属者)
属组:所属组
其他:除属组和属主外的用户(其他人)

2、设置文件或目录的归属及权限
a、设置权限
格式1: chmod [ugoa][+-=][rwx] 文件或目录
格式2: chmod nnn 文件或目录 (最常用)
例如:
chmod u+w /root/test.txt
chmod g-w /root/test.txt
chmod 754 /root/test.txt

格式一:
[[email protected] ~]# echo "aaaaaaaaaaaaaaaa" >> aa.txt
[[email protected] ~]# ls -l aa.txt
-rw-r--r--. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]# root用户及root组的成员对aa.txt都有读写执行权限^C
[[email protected] ~]# chmod u+x aa.txt
[[email protected] ~]# ls -l aa.txt
-rwxr--r--. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]# chmod g=rwx aa.txt
[[email protected] ~]# ls -l aa.txt
-rwxrwxr--. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]# chmod g-x aa.txt
[[email protected] ~]# chmod u-x aa.txt
[[email protected] ~]# ls -l aa.txt
-rw-rw-r--. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]#

格式二:
[[email protected] ~]# chmod 644 aa.txt
[[email protected] ~]#
[[email protected] ~]# ls -l aa.txt
-rw-r--r--. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]# chmod 777 aa.txt
[[email protected] ~]# ls -l aa.txt
-rwxrwxrwx. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]# chmod 654 aa.txt
[[email protected] ~]# ls -l aa.txt
-rw-r-xr--. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]# chmod 600 aa.txt
[[email protected] ~]# ls -l aa.txt
-rw-------. 1 root root 17 5月 4 11:24 aa.txt

b、设置归属
格式:
    chown 属主:属组  文件或目录
    chown 属主  文件或目录
    chown :属组  文件或目录
例如:
    ……

[[email protected] ~]# ls -l aa.txt
-rw-------. 1 root root 17 5月 4 11:24 aa.txt
[[email protected] ~]# 将aa.txt文件的所有者改为mike,属组改为ftpuser^C
[[email protected] ~]# chown mike aa.txt
[[email protected] ~]# ls -l aa.txt
-rw-------. 1 mike root 17 5月 4 11:24 aa.txt
[[email protected] ~]# chown :ftpuser aa.txt
[[email protected] ~]# ls -l aa.txt
-rw-------. 1 mike ftpuser 17 5月 4 11:24 aa.txt
[[email protected] ~]# chown mike:ftpuser aa.txt
[[email protected] ~]# ls -l aa.txt
-rw-------. 1 mike ftpuser 17 5月 4 11:24 aa.txt
[[email protected] ~]#

以上是关于管理用户和组账号;管理目录和文件的属性的主要内容,如果未能解决你的问题,请参考以下文章

Linux中的账号和权限管理(理论讲解部分)

账号和权限管理(理论知识铺垫)

账号和权限管理

05-管理用户和组

Linux命令详解 三

linux-账号与权限管理--用户账号和组账号权限及归属