Linux中文件权限操作,系统优化相关操作,超级详细!
命令:chmod
参数:
-c
: 若该文件权限确实已经更改,才显示其更改动作
-f
:若该文件权限无法被更改也不要显示错误讯息
-v
:显示权限变更的详细资料
-R
:对目前目录下的所有文件与子目录进行相同的权限变更(即以递归的方式逐个变更)
u-属主(Owner)
、g-属组(Group)
、o-其他使用者(Other Users)
+
号就可以添加权限,-
号代表取消权限,=
代表唯一设定权限Operator | 说明 |
---|---|
+ | 为指定的用户类型增加权限 |
- | 去除指定用户类型的权限 |
= | 设置指定用户权限的设置,即将用户类型的所有权限重新设置 |
模式 | 对应数字 | 描述 |
---|---|---|
r | 4 | 设置为可读权限 |
w | 2 | 设置为可写权限 |
x | 1 | 设置为可执行权限 |
相关文件:/etc/profile
在Linux中,常用的文件的权限是666,目录的权限是777
创建文件的默认权限是跟umask
值相减,遇到奇数加一,遇到偶数则不变
创建文件夹的权限只和umask相减
查看profile文件中的umask值,及运算关系
[root@localhost ~]# cat /etc/profile
# 查看uid
[root@localhost ~]# id root
uid=0(root) gid=0(root) groups=0(root)
# uid <199,第一步为假了
[root@localhost test]# /usr/bin/id -gn
root
[root@localhost test]# /usr/bin/id -un
root
# 执行结果相同,为真
# False && True = False,所以root的umask = 022
# 那么我们在root用户下创建的用户默认权限就可以计算了,文件权限666和目录权限777与umask相减来验证
# 1、文件默认权限验证
666的每位与022相减:
6-0 =6 # 偶数不用变
6-2 =4 # 偶数不用变
6-2 = 4 # 偶数不用变
# 所以root下创建文件的默认权限为644---->rw-r--r--
# 实际创建验证
[root@localhost test]# touch a.txt
[root@localhost test]# ll -i
total 0
1423023 -rw-r--r--. 1 root root 0 Dec 15 15:48 a.txt
# 2、目录默认权限验证
777的每位与022直接相减,不需要判断奇偶
7 - 0 = 7
7 - 2 = 5
7 - 2 = 5
# 所以root下创建的目录的默认权限为755 ----> rwxr-xr-x
# 实际创建验证
[root@localhost ~]# mkdir test
[root@localhost ~]# ll -i
1423022 drwxr-xr-x. 2 root root 19 Dec 15 15:48 test
[root@localhost test]# ll -ia
1423023 -rw-r--r--. 1 root root 0 Dec 15 15:48 a.txt
# 现在ugo的权限为读写,可读,可读,把ugo的权限扩大,改为读写执行
[root@localhost test]# chmod ugo+rwx a.txt
或
[root@localhost test]# chmod 777 a.txt
[root@localhost test]# ll -i
total 0
1423023 -rwxrwxrwx. 1 root root 0 Dec 15 15:48 a.txt
# 将a.txt ugo的读写执行权限都去掉
[root@localhost test]# chmod ugo-rwx a.txt
或
[root@localhost test]# chmod -777 a.txt
[root@localhost test]# ll -i
total 0
1423023 ----------. 1 root root 0 Dec 15 15:48 a.txt
# 分别给a.txt 的u读写执行,g添加读写,o添加读权限
[root@localhost test]# chmod u+rwx,g+rw,o+r a.txt
或
[root@localhost test]# chmod 764 a.txt
[root@localhost test]# ll -i
total 0
1423023 -rwxrw-r--. 1 root root 0 Dec 15 15:48 a.txt
# 分别给a.txt 的u读写,g执行,o没有任何权限
[root@localhost test]# chmod u+rw,g+x a.txt
或
[root@localhost test]# chmod 610 a.txt
[root@localhost test]# ll -i
total 0
1423023 -rw---x---. 1 root root 0 Dec 15 15:48 a.txt
# 查看test文件夹文件的权限
[root@localhost test]# ll
total 0
----------. 1 root root 0 Dec 15 15:48 a.txt
----------. 1 root root 0 Dec 15 16:25 b.txt
# 没有任何权限,下面给test文件下的所有文件添加读写执行权限
[root@localhost ~]# chmod -R 777 test
[root@localhost ~]# ll test
total 0
-rwxrwxrwx. 1 root root 0 Dec 15 15:48 a.txt
-rwxrwxrwx. 1 root root 0 Dec 15 16:25 b.txt
# 将test目录下所有文件的属组的执行权限,其他使用者的写和执行权限去掉
[root@localhost ~]# chmod -R g-x,o-wx test/
[root@localhost ~]# ll test/
total 0
-rwxrw-r--. 1 root root 0 Dec 15 15:48 a.txt
-rwxrw-r--. 1 root root 0 Dec 15 16:25 b.txt
ps:若用 chmod 4755 filename 可使此程序具有 root 的权限。
# cat /etc/redhat-release
CentOS release 7.5.1804
# uname -r
3.10.0-862.el7.x86_64
# uname -m
x86_64
添加系统普通用户:useradd [用户名]
参数 :-a
[root@localhost ~]# uname
Linux
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
设置用户密码 :passwd [用户名]
免交互模式:echo [密码]|passwd --stdin [用户名]
(一般用在脚本文件中)
切换用户信息:su
和 su -
二者区别就是su
是切换用户,但是切换后的用户缺少相应的文件或环境变量;su -
相当于重新登录,切换后的用户携带环境变量或相应文件
通过pwd
和echo $PATH
两个命令查看超管和普通用户切换后的区别
root用户切换到普通用户不需要输入密码
普通用户切换到root用户需要输入密码
whoami
命令[root@localhost ~]# whoami
root
who
命令[root@localhost ~]# who
root tty1 2021-12-15 08:50
root pts/0 2021-12-15 15:14 (192.168.15.1)
root pts/2 2021-12-15 16:52 (192.168.15.1)
# tty和pts的含义
tty ---- 代表虚拟机开的窗口
pts ---- 代表此时Xshell开的窗口
通过echo $PS1
命令修改显示命令行提示符格式信息
大写方式
[root@localhost ~]# echo $PS1
[\u@\h \W]\$
# 大写方式只能显示路径基名,不显示完全路径
[root@localhost ~]#cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]#
[root@localhost ~]# PS1='[\u@\h \w]\$'
# 通过小写方式这样就能显示完整路径了
[root@localhost ~]#cd /etc/sysconfig/network-scripts/
[root@localhost /etc/sysconfig/network-scripts]#
相关文件: /etc/profile
在profile文件内添加export PS1='[\u@\h \w]\$ '
重载文件命令:source
重载文件:source /etc/profile
,设置成功+
【待续】
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.cnblogs.com/48xz/p/15694592.html
内容来源于网络,如有侵权,请联系作者删除!