什么是网站易用性,数字营销公司排行榜,西安网站设计建设公司 交通,网站建设注意事项 南京年底了#xff0c;公司接的北京地铁轨道交通的项目做不完了#xff0c;一百多列地铁的设备都得调#xff0c;派我出差了几周#xff0c;这几天才回来#xff0c;出差累死了实在是没办法更新。#xff08;YOASOBI的二开票还没抢到ToT#xff0c;哭死#xff0c;看看回滚… 年底了公司接的北京地铁轨道交通的项目做不完了一百多列地铁的设备都得调派我出差了几周这几天才回来出差累死了实在是没办法更新。YOASOBI的二开票还没抢到ToT哭死看看回滚票能不能抢到吧.. 本篇主要学习一下用户账户管理的命令这个比较重要的Linux里面的权限和用户都是用的比较多的。用户账号的作用主要是为了登录系统在windows系统中也有用户系统但是在Windows系统中的用户系统并没有Linux那么严谨条条框框没那么多。
01 useradd 创建用户 useradd用于创建用户一般的命令格式是useradd[-选项]用户名
常用选项有
-u 指定用户UID-d 指定用户家目录了解-c 用户描述信息-g 指定用户基本组了解-G 指定用户附加组-s 指定用户的解释器程序 一般用法有蛮多的比如 创建用户指定UID 创建用户并指定用户的家目录 创建用户并指定UID和用户描述信息 创建用户组test创建用户指定UID和基本组 如果你想还可以禁止用户登录系统 02 /etc/passwd用户信息文件 用户的基本信息存放在 /etc/passwd文件内。查看此文件信息可以看到以下内容 root: x: 0: 0: root: /root: /bin/bash 用户名: 密码占位符: UID: 基本组GID: 用户描述信息: 家目录: 解释器程序 UID0 超级用户root UID1-999 系统伪用户不能登录系统 UID1000-65535 普通用户管理员创建的用户 组 基本组初始组一个用户只允许有一个基本组 附加组在基本组之外组一个用户可以允许有多个附加组 用户---bash程序解释器---内核---硬件 03 用户默认值信息 /etc/default/useradd 是用来存放用户默认值信息的输入vim /etc/default/useradd 可以看到文件内容 GROUP100 #用户默认组 HOME/home #用户家目录 INACTIVE-1 #密码过期宽限天数/etc/shadow文件第7个字段 EXPIRE #密码失效时间/etc/shadow文件第8个字段 SHELL/bin/bash #默认使用的 SKEL/etc/skel #模板目录 CREATE_MAIL_SPOOLyes #是否建立邮箱 04 用户邮件目录 可以给用户信息里面设置邮箱这样就可以通过登录账户安装查看邮件的工具来查看邮件。 安装mailx工具来查看邮件。 05 账户密码设置 passwd命令用于设置密码命令格式passwd [-选项] [用户名] 密码规范长度不能少于8个字符复杂度数字、字母包含大小写特殊字符 密码规范本次修改的密码不能和上次修改的密码太相近 常用选项 -S 查看密码信息-l 锁定用户密码-u 解锁用户密码-d 删除密码--stdin 通过管道方式设置用户密码 以上是交互式设置密码也可以非交互式直接设置 命令格式echo 密码 | passwd --stdin 用户名 交互式修改用户密码 查看用户的密码 如果你不想其他人修改密码可以选择锁定当前用户的密码。 非交互式修改用户密码 06 用户密码文件 用户的密码存放在/etc/shadow文件中该文件默认任何人都没有任何权限不包括root我们可以去用vim来查看一下此文件 #每个字段含义解释 第一字段用户名 第二字段密码加密字符串加密算法为SHA512散列加密算法如果密码位是“*”或者“!!”表示密码已过期 第三个字段密码最后一次修改日期日期从1970年1月1日起每过一天时间戳加1 第四个字段密码修改的期限如果该字段为0表示随时可以修改密码例如该字段为10代表10天之内不可以修改密 第五个字段密码有效期 第六个字段密码到期前警告时间和第五个字段相比 第七个字段密码过期后的宽限天数和第五个字段相比 第八个字段账号失效时间日期从1970年1月1日起 第九个字段保留 #chage命令用于修改/etc/shadow文件信息修改文件内容第三个字段密码最后一次修改时间 [rootlocalhost ~]# chage -d 0 user2 07 su命令 如果你在某一个非root账户下想要获取更高的权限切换到root用户使用su命令即可。 切换用户用户环境会一起改变。普通用户切换root需要输入密码root用户切换普通用户不需要。
08 usermod修改用户属性
usermod 命令用于修改已存在用户的基本信息 命令格式usermod [-选项] 用户名 常用选项 -u 修改用户UID-d 修改用户家目录了解-g 修改用户基本组了解-c 修改用户描述信息-G 添加用户附加组-s 修改用户shell 修改已存在用户的UID 修改已存在用户的解释器 09 删除用户 前面都是在讲如何创建修改用户那么我不需要这个用户的时候如何删除用户呢
userdel 用于删除给定的用户以及与用户相关的文件该命令若不加选项仅删除用户账号不删除用户的家目录 命令格式userdel [-选项] 用户名 常用选项 -r 删除用户同时并删除用户的家目录 #删除用户仅删除账号不删除家目录 #删除用户删除账号连带删除家目录 10 groupadd 添加组 groupadd 用于创建一个新的工作组新组的信息将被添加到/etc/group文件中 命令格式groupadd [-选项] 组名 常用选项 -g GID #指定组的GID
11 gpasswd组管理命令 gpasswd 是Linux工作组文件/etc/group和/etc/gshadow管理工具用于将用户添加到组或从组中删除。 命令格式gpasswd [-选项] 用户名 组名 常用选项 -a #将用户添加到工作组-d #将用户从工作组中删除 首先创建三个用户a1 a2 a3 创建组a把三个用户添加进组a 12 chmod权限管理 权限的重要性 任何一个系统权限都是非常重要的如果没有权限的话那系统的安全就没办法保障 特别是对于Linux这种多用户的系统来讲通常一台机器有很多个用户都在使用的话那我们 就应该通过权限去控制这些用户在系统的操作。 chmod英文全拼change mode设置用户对文件的权限命令格式chmod [-选项] 归属关系-权限类别 文件...不常用选项 -R 递归修改目录下所有的子文件与子目录的权限与父目录相同归属关系u 所有者 g 所属组 o 其他人权限类别 r 读取 w 写入 x 执行 - 没有权限操作 添加权限 - 去除权限 重新定义权限权限数字表示r ---- 4 w ---- 2 x ---- 1 0 没有权限 #查看文件详细属性 [rootlocalhost ~]# ll hello - rw- r-- r-- . 1 root root 426 3月 28 15:00 hello 文件类型 所有者权限 所属组权限 其他人的权限 所有者 所属组 #为文件所有者添加执行权限 [rootlocalhost ~]# chmod ux hello [rootlocalhost ~]# ll hello -rwxr--r--. 1 root root 426 3月 28 15:00 hello #为文件所属组添加写权限 [rootlocalhost ~]# chmod gw hello [rootlocalhost ~]# ll hello -rwxrw-r--. 1 root root 426 3月 28 15:00 hello #为文件其他人添加写权限 [rootlocalhost ~]# chmod ow hello [rootlocalhost ~]# ll hello -rwxrw-rw-. 1 root root 426 3月 28 15:00 hello #使用逗号可以同时为多个用户授权 [rootlocalhost ~]# chmod gx,ox hello [rootlocalhost ~]# ll hello -rwxrwxrwx. 1 root root 426 3月 28 15:00 hello #去除所有者执行权限 [rootlocalhost ~]# chmod u-x hello [rootlocalhost ~]# ll hello -rw-rwxrwx. 1 root root 426 3月 28 15:00 hello #去除所属组执行权限 [rootlocalhost ~]# chmod g-x hello [rootlocalhost ~]# ll hello -rw-rw-rwx. 1 root root 426 3月 28 15:00 hello #去除其他人执行权限 [rootlocalhost ~]# chmod o-x hello [rootlocalhost ~]# ll hello -rw-rw-rw-. 1 root root 426 3月 28 15:00 hello #同时去除ugo写权限 [rootlocalhost ~]# chmod u-w,g-w,o-w hello [rootlocalhost ~]# ll hello -r--r--r--. 1 root root 426 3月 28 15:00 hello #重新定义所有者权限 [rootlocalhost ~]# chmod urwx hello [rootlocalhost ~]# ll hello -rwxr--r--. 1 root root 426 3月 28 15:00 hello #重新定义所属组权限 [rootlocalhost ~]# chmod grwx hello [rootlocalhost ~]# ll hello -rwxrwxr--. 1 root root 426 3月 28 15:00 hello #重新定义其他人权限 [rootlocalhost ~]# chmod orwx hello [rootlocalhost ~]# ll hello -rwxrwxrwx. 1 root root 426 3月 28 15:00 hello #创建目录并设置目录权限 [rootlocalhost ~]# mkdir /test [rootlocalhost ~]# ll -d /test drwxr-xr-x. 2 root root 6 4月 11 14:30 /test #为目录所属组添加写权限 [rootlocalhost ~]# chmod gw /test [rootlocalhost ~]# ll -d /test drwxrwxr-x. 2 root root 6 4月 11 14:30 /test #为目录其他人添加写权限 [rootlocalhost ~]# chmod ow /test [rootlocalhost ~]# ll -d /test drwxrwxrwx. 2 root root 6 4月 11 14:30 /test [rootlocalhost ~]# #重新定义所有用户权限 [rootlocalhost ~]# chmod urwx,grx,orx /test [rootlocalhost ~]# ll -d /test drwxr-xr-x. 2 root root 6 4月 11 14:30 /test #同时为所有用户定义相同权限 [rootlocalhost ~]# chmod ugorwx /test [rootlocalhost ~]# ll -d /test drwxrwxrwx. 2 root root 21 4月 11 14:37 /test #权限数字定义方式 [rootlocalhost ~]# ll hello -rwxrwxrwx. 1 root root 426 3月 28 15:00 hello 所有者rwx 4217 所属组r 4 其他人r 4 [rootlocalhost ~]# chmod 744 hello [rootlocalhost ~]# ll hello -rwxr--r--. 1 root root 426 3月 28 15:00 hello 所有者rw 426 所属组rw 426 其他人--- 0 [rootlocalhost ~]# chmod 660 hello [rootlocalhost ~]# ll hello -rw-rw----. 1 root root 426 3月 28 15:00 hello 所有者rwx 4217 所属组wx 213 其他人--- 0 [rootlocalhost ~]# touch /hello.txt [rootlocalhost ~]# ll /hello.txt -rw-r--r--. 1 root root 0 4月 11 14:45 /hello.txt [rootlocalhost ~]# chmod 730 /hello.txt [rootlocalhost ~]# ll /hello.txt -rwx-wx---. 1 root root 0 4月 11 14:45 /hello.txt #去除所有用户权限 [rootlocalhost ~]# chmod 000 /hello.txt [rootlocalhost ~]# ll /hello.txt ----------. 1 root student 0 4月 11 14:45 /hello.txt #递归修改目录下所有子文件与子目录权限 [rootlocalhost ~]# ll -d /test drwxrwxrwx. 2 root root 21 4月 11 14:37 /test [rootlocalhost ~]# mkdir /test/xxoo [rootlocalhost ~]# ll -d /test/xxoo/ drwxr-xr-x. 2 root root 6 4月 11 14:54 /test/xxoo/ [rootlocalhost ~]# ll /test/abc.txt -rw-r--r--. 1 root root 0 4月 11 14:37 /test/abc.txt #默认用户在该目录下创建文件权限与父目录不一致 #递归修改目录下所有子文件与子目录权限 [rootlocalhost ~]# chmod -R 777 /test [rootlocalhost ~]# ll /test/abc.txt -rwxrwxrwx. 1 root root 0 4月 11 14:37 /test/abc.txt [rootlocalhost ~]# ll -d /test/xxoo drwxrwxrwx. 2 root root 6 4月 11 14:54 /test/xxoo #深入理解权限 [rootlocalhost ~]# mkdir /test1 [rootlocalhost ~]# chmod 777 /test1 [rootlocalhost ~]# ll -d /test1 drwxrwxrwx. 2 root root 6 4月 11 14:57 /test1 #在该目录下创建文件与目录 [rootlocalhost ~]# touch /test1/root.txt [rootlocalhost ~]# mkdir /test1/rootbak [rootlocalhost ~]# chmod orx /test1 [rootlocalhost ~]# ll -d /test1 drwxrwxr-x. 2 root root 6 4月 11 14:59 /test1 [rootlocalhost ~]# touch /test1/root.txt #普通用户对该目录如果拥有rwx权限是可以删除该目录下任何用户创建的文件包括root [user1localhost ~]$ cd /test1 [user1localhost test1]$ ls root.txt [user1localhost test1]$ ll root.txt -rw-r--r--. 1 root root 0 4月 11 14:57 root.txt [user1localhost test1]$ rm -rf root.txt [user1localhost test1]$ ls rootbak [user1localhost test1]$ rm -rf rootbak/ [user1localhost test1]$ ls [user1localhost test1]$ ll -d /test1 drwxrwxrwx. 2 root root 6 4月 11 14:59 /test1 总结用户对文件拥有rwx权限针对的是文件的内容 r查看文件内容 w对文件内容拥有增删改权限并不能删除文件删除文件取决于对文件所在的目录有没有rwx权限 x可以运行该文件 2.用户对目录拥有rwx权限 r查看目录下内容 w在该目录创建文件修改文件属性删除任何用户的文件包括root x可以切换到该目录 13 umask预设权限 umask用于显示或设置创建目录的权限掩码 命令格式umask [-p] [-S] [mode] rootlocalhost ~]# mkdir /test2 [rootlocalhost ~]# ll -d /test2 drwxr-xr-x. 2 root root 6 4月 11 15:05 /test2 [rootlocalhost ~]# umask --help umask: 用法:umask [-p] [-S] [模式] #查看目录默认权限掩码以数字形式显示 [rootlocalhost ~]# umask -p umask 0022 #查看目录默认权限掩码以字母形式显示 [rootlocalhost ~]# umask -S urwx,grx,orx #设置目录默认权限掩码为所属组添加写权限 [rootlocalhost ~]# umask gw [rootlocalhost ~]# mkdir /test3 [rootlocalhost ~]# ll -d /test3 drwxrwxr-x. 2 root root 6 4月 11 15:09 /test3 #去除目录默认权限掩码 [rootlocalhost ~]# umask g-w [rootlocalhost ~]# mkdir /test4 [rootlocalhost ~]# ll -d /test4 drwxr-xr-x. 2 root root 6 4月 11 15:10 /test4