文章目录

* 账户与安全 <https://blog.csdn.net/liulong1010/article/details/84721321#_1>
* 创建账户以及组 <https://blog.csdn.net/liulong1010/article/details/84721321#_9>
* 1、useradd
<https://blog.csdn.net/liulong1010/article/details/84721321#1useradd_13>
* 2、groupadd
<https://blog.csdn.net/liulong1010/article/details/84721321#2groupadd_38>
* 3、id <https://blog.csdn.net/liulong1010/article/details/84721321#3id_47>
* 修改账户及组 <https://blog.csdn.net/liulong1010/article/details/84721321#_53>
* 1、passwd
<https://blog.csdn.net/liulong1010/article/details/84721321#1passwd_55>
* 2、gpasswd
<https://blog.csdn.net/liulong1010/article/details/84721321#2gpasswd_76>
* 3、usermod
<https://blog.csdn.net/liulong1010/article/details/84721321#3usermod_84>
* 删除账户及组 <https://blog.csdn.net/liulong1010/article/details/84721321#_105>
* 1、userdel
<https://blog.csdn.net/liulong1010/article/details/84721321#1userdel_107>
* 2、groupdel
<https://blog.csdn.net/liulong1010/article/details/84721321#2groupdel_117>
* 账户与组文件解析 <https://blog.csdn.net/liulong1010/article/details/84721321#_123>
* 1、`etc/passwd`
<https://blog.csdn.net/liulong1010/article/details/84721321#1etcpasswd_125>
* 2、`/etc/shadow`
<https://blog.csdn.net/liulong1010/article/details/84721321#2etcshadow_138>
* 3、`/etc/group`
<https://blog.csdn.net/liulong1010/article/details/84721321#3etcgroup_144>
* 4、`/etc/gshadow`
<https://blog.csdn.net/liulong1010/article/details/84721321#4etcgshadow_156>
* 文件权限 <https://blog.csdn.net/liulong1010/article/details/84721321#_167>
* 修改文档属性 <https://blog.csdn.net/liulong1010/article/details/84721321#_188>
* 1、chmod
<https://blog.csdn.net/liulong1010/article/details/84721321#1chmod_190>
* 2、chown
<https://blog.csdn.net/liulong1010/article/details/84721321#2chown_210>
* ACL访问控制权限
<https://blog.csdn.net/liulong1010/article/details/84721321#ACL_224>
* 1、getfacl
<https://blog.csdn.net/liulong1010/article/details/84721321#1getfacl_228>
* 2、setfacl
<https://blog.csdn.net/liulong1010/article/details/84721321#2setfacl_260>
* 账户管理案例 <https://blog.csdn.net/liulong1010/article/details/84721321#_282>


<>账户与安全


Linux系统对账号与组的管理是通过ID号来实现的,我们在登录系统时,输入用户对应的密码,后台系统会将用户名转化为ID号后再判断该账号是否存在,并对比密码是否匹配。在Linux中,用户ID号被称为UID,组ID被称为GID。其中UID为0,代表超级管理员,也就是通常说的root账号,1~499之间ID号系统会预留下来。这样我们创建的账号会从500算起。


Linux的组有基本组与附加组之分,一个用户只可以加入一个基本组中,但可以同时加入多个附加组。创建用户时,系统默认会自动创建同名的组,并设置用户加入该基本组中。

<>创建账户以及组

创建账户及组需要管理员权限

<>1、useradd

创建新账号 useradd 【选项】 用户名称

* -c 设置账号描述信息,一般为账号全称
* -d 设置账号家HOME_DIR目录
* -e 设置账户的失效日期,格式为YYYY-MM-DD
* -g 设置账户的基本组
* -G 设置账户的附加组,多个附加组中间用逗号隔开
* -M 不创建账户家目录,一般与-s结合使用
* -s设置账户的登录Shell,默认bash
* -u指定账户UID
useradd Frank 创建普通账户Frank

useradd -c adminstrator -d /home/admin -e 2013-12-24 \

​ -g root -G bin,adm,mail admin

\ 是换行输入命令

创建系统账户名称为admin,全名为adminstrator,账户家目录为/home/admin
,账户失效日期为2013年12月24日,账户基本组为root,附加组为bin、adm、mail

<>2、groupadd

创建组账户 groupadd [选项] 组名称

-g 设置组ID号

* group tom 创建tom组
* group -g 1000 jerry 创建GID为1000的组jerry
<>3、id

显示账户及组信息

id root 查看root账户及相关组信息

<>修改账户及组

<>1、passwd

更新账号认证信息 passwd [选项][账号名称]

* -l 锁定账户,仅root可使用此选项
* --stdin 从文件或管道读取密码
* -u 解锁账户
* -d 快速清空账户密码,仅root可使用此选项
passwd 为当前用户设置新密码

passwd 指定修改tom的密码

echo "qwer0987" | passwd --stdin tom 设置tom的密码为qwer0987

passwd -l tom 锁定账户tom

passwd -u tom 解锁账户tom

passwd -d tom 清空账户tom的密码(无密码可登陆)

<>2、gpasswd

设置组密码

gpasswd admin 设置组密码

gpasswd -A mail admin 将mail账户设置组admin的管理员

<>3、usermod

修改账户信息 usermode [选项] 账户名称

* -d 修改账户家目录
* -e修改账户失效日期
* -g 修改账户所属基本组
* -G 修改账户所属附加组
* -s 修改账户登录Shell
* -u 修改账户UID
usermod -d /home/tomcat tom 修改tom的家目录, /home/tomcat目录必须存在

usermod -e 2013-10-01 tom 修改tom的失效日期为2013-10-01

usermode -g mail tom 修改账户tom的基本组为mail

usermode -s /bin/bash user2 修改user2登录Shell为bash

usermode -u 1001 tom 修改tom的UID为1001

<>删除账户及组

<>1、userdel

删除账户及相关文件

-r删除账户及相关文件

userdel tom 删除账户tom,但该账户下的文件不删除

userdel -r tom 删除账户tom,并删除相应的家目录

<>2、groupdel

删除组账户

groupdel jerry 删除组jerry

<>账户与组文件解析

<>1、etc/passwd

账户信息文件
james:x:500:500:普通用户:/home/james:/bin/bash
账户名称:密码占位符:UID:GID:账户的描述信息:账户家目录:账户登录Shell

* /bin/bash 可登录系统Shell
* /sbin/nologin表示账户无法登录系统
<>2、/etc/shadow

账户密码文件

账户名称:密码(如果未设置密码,是为!!):。。。

<>3、/etc/group

组账户信息文件
postfix:x:89: sshd:x:74: tom:x:500:
组账户名称:密码占位符:GID:组成员信息(仅显示基本成员,附加成员不显示)

<>4、/etc/gshadow

组账户密码文件
root::: bin:::bin,daemon
组账户名称:组密码(一般为组管理员密码):组管理员:组成员(与/etc/group第四列相同)

<>文件权限

Linux权限主要分为读r|4、写w|2、执行x|1三种权限如:
[root@localhost ~]# ls -l 总用量 28 -rw-------. 1 root root 1139 11月 22 19:31
anaconda-ks.cfg -rw-r--r--. 1 root root 13642 11月 22 19:31 install.log
-rw-r--r--. 1 root root 3482 11月 22 19:30 install.log.syslog drwxr-xr-x. 2 root
root 4096 12月 1 14:05test
* 第1位文件:-普通文件、d代表目录、l链接文件、b,c设备
* 第2-4位 所有者权限
* 第5-7位 所属组权限
* 第8-10位 其他账户权限
* 最后一为 .没有添加ACL控制,+添加ACL控制
文件权限:链接数量或者子目录个数:文档的所有者:文档的所属组:大小:修改时间:文档名称

<>修改文档属性

<>1、chmod

修改文件或者目录权限 chmod [选项] 权限 文档

* --reference=RFILE 根据参考文档设置权限
* -R 递归将权限应用于所有的子目录与子文件
u代表所有者,g代表所属组,o代表其他用户,a代表所有人
chmod u=rwx,g=rwx,o=rwx install.log chmod a=rw install.log chmod g-x,o-wx
install.logchmod u+w install.log chmod 700 install.log #修改文件权限rwx------ chmod
644 install.log#修改文件权限rw-r--r-- chmod 755 /home # 修改目录权限 rwxr-xr-x chmod
--reference=install.log.syslog install.log # 以install.log.syslog
为标准修改install.log权限
<>2、chown

修改文件或者目录的所有者所属组 chown [选项][所有者][:[所属组]] 文件或目录

*
-R 递归将权限应用于所有的子目录与子文件

chown user2:mail install 修改文件的所有者为user2,所属组为mail

chown :root install 仅修改文件的所属组为root

chown root install 修改文件的所属组为root

<>ACL访问控制权限

由于系统的基本权限是针对文档所有者、所属组或其他账户进行控制的,无法针对某个单独的账户进行控制,所以就有ACLAccount Control List
访问控制列表概念,使用ACL,可以针对单一账户设置文档的访问权限

<>1、getfacl

查看文档的ACL权限
# 下面install.log未设置ACL访问控制条目,仅有基本的文件所有者、所属组、其他账户的访问控制 [root@localhost ~]#
getfacl install.log # file: install.log # owner: root # group: root user::rw-
group::r-- other::r-- [root@localhost ~]# ls -l test.txt -rw-rw-r--+ 1 root
root 0 12月 2 14:00 test.txt[root@localhost ~]# getfacl test.txt # file: test.txt
# owner: root # group: root user::rw- user:user1:rw-
#添加的ACL条目,使用用户user1对test.txt文件可读可写 group::r-- group:user1:r-- #
添加ACL条目,使user1组对test.txt文件可读 mask::rw- other::r--
<>2、setfacl

设置文档访问控制列表setfacl [选项][{-m|-x} acl条目] 文件或目录

* -b 删除所有的附加的ACL条目
* -k # 删除默认的ACL
* -m 添加ACL条目
* -x 删除指定的ACL条目
* -R 递归处理所有的子目文件或子目录
setfacl -m u:user:rw test.txt 添加ACL条目,使用用户user1对test.txt文件可读可写

setfacl -m g:user1:r test.txt 添加ACL条目,使user1组队test.txt文件可读

setfacl -x g:user1 test.txt 删除user1组的ACL条目

setfacl -x u:user1 test.txt 删除账户user1的ACL条目

setfacl -b test.txt 删除所有附加的ACL条目

<>账户管理案例


模拟公司组织结构,EXAMPLE公司是一个从事教育培训的组织,公司结构主要分为教研室(teach)、校长办公室(office)、财务部(finance)、行政部(admin)、市场部(martket)。


部署文件共享服务器,需要为每个部门创建各自独立的共享目录,根据要求为所有的普通员工创建各自独立的账户,这些账户仅要读权限即可,并为每个部门的负责人创建一个管理账户(管理员名称为Op_部门名称),这个账户可以进行读写操作。为了后期管理的方便需要为每个部门创建与部门名称相同的名的组账户。
#创建共享目录 [root@centos6 ~]# mkdir -p /var/{teach,office,finance,admin,market}
#创建组账户 [root@centos6 ~]# groupadd teach [root@centos6 ~]# groupadd finance [
root@centos6 ~]# groupadd admin [root@centos6 ~]# groupadd market [root@centos6
~]# groupadd office #创建组管理员账户 [root@centos6 ~]# useradd -g teach Op_teach [
root@centos6 ~]# useradd -g office Op_office [root@centos6 ~]# useradd -g
finance Op_finance [root@centos6 ~]# useradd -g admin Op_admin [root@centos6 ~]
# useradd -g market Op_market #创建普通员工账户 [root@centos6 ~]# useradd -g teach
endy009 [root@centos6 ~]# useradd -g office lucy030 [root@centos6 ~]# useradd
-g finance jacob060 [root@centos6 ~]# useradd -g admin jerry160 [root@centos6 ~]
# useradd -g market marry001 #添加组管理员 [root@centos6 ~]# gpasswd -A Op_teach teach
[root@centos6 ~]# gpasswd -A Op_office office [root@centos6 ~]# gpasswd -A
Op_finace finance [root@centos6 ~]# gpasswd -A Op_finance finance [root@centos6
~]# gpasswd -A Op_admin admin [root@centos6 ~]# gpasswd -A Op_market market
#给共享文件夹赋予相应的权限控制 [root@centos6 ~]# chown Op_teach.teach /var/teach [
root@centos6 ~]# ll -a /var/teach 总用量 8 drwxr-xr-x. 2 Op_teach teach 4096 12月 2
14:27. drwxr-xr-x. 23 root root 4096 12月 2 14:27 .. [root@centos6 ~]# chown
Op_office:office /var/office [root@centos6 ~]# ll -a /var/office 总用量 8
drwxr-xr-x. 2 Op_office office 4096 12月 2 14:27. drwxr-xr-x. 23 root root 4096
12月 2 14:27.. [root@centos6 ~]# chown Op_finance.finance /var/finance [
root@centos6 ~]# chown Op_admin.admin /var/admin [root@centos6 ~]# chown
Op_market.market /var/market [root@centos6 ~]# chmod 755
/var/{teach,office,finance,admin,market}

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信