[OS-Linux]详解Linux的权限

 本文基于CentOS,详解介绍了Linux权限的管理,chmod,chgrp以及粘滞位。

目录

一、用户

二、Linux权限管理

1. 文件访问者的分类

2.文件类型和访问权限

(1) 文件类型

3. 文件权限值的表示方法

4.文件访问权限的相关设置方法

chmod

chown

chgrp

umask

file指令

四、目录的权限和粘滞位


一、用户

Linux下有两种用户:超级用户(root)、普通用户。 

超级用户:可以再linux系统下做任何事情,不受限制,命令提示符是“#”。
普通用户:在linux下做有限的事情,普通用户的命令提示符是“$”。 

命令:su [用户名]
功能:切换用户。
从root到普通用户user,则使用 su user。

要从普通用户user切换到root用户则使用 su root(root可以省略)。

二、Linux权限管理

1. 文件访问者的分类

文件和文件目录的所有者:u---User

文件和文件目录的所有者所在的组的用户:g---Group

其它用户:o---Others

2.文件类型和访问权限

(1) 文件类型

                d:文件夹
                -:普通文件
                l:软链接(类似Windows的快捷方式)
                b:块设备文件(例如硬盘、光驱等)
                p:管道文件
                c:字符设备文件(例如屏幕等串口设备)
                s:套接口文件

b) 基本权限

读(r/4):Read对文件而言,具有读取文件内容的权限;对目录来说,具有浏览该目录信息的权限;

写(w/2):Write对文件而言,具有修改文件内容的权限;对目录来说具有删除移动目录内文件的权限; 

执行(x/1):execute对文件而言,具有执行文件的权限;对目录来说,具有进入目录的权限;

“-”   : 表示不具有该项权限

3. 文件权限值的表示方法

有两种表示方法,字符表示方法和8进制数值表示方法

字符表示方法:

Linux表示说明Linux表示说明
r--只读-w-仅可写
--x仅可执行rw-可读可写
-wx可写可执行r-x可读可执行
rwx可读可写可执行---无权限

8进制数值表示方法:

权限符号八进制二进制
r4100
w2010
x1001
rw6110
rx5101
wx3011
rwx7111
---0000

4.文件访问权限的相关设置方法
 

chmod


功能:设置文件的访问权限
格式:chmod [参数] 权限 文件名
常用选项:
        R -> 递归修改目录文件的权限(只有文件的拥有者和root才可以改变文件的权限)

chmod命令权限的格式

(1)用户表示符+/-=权限字符

        +:向权限范围增加权限代号所表示的权限
        -:向权限范围取消权限代号所表示的权限
        =:向权限范围赋予权限代号所表示的权限
        用户符号:
                u:拥有者
                g:拥有者同组用
                o:其它用户
                a:所有用户

 实例:

chmod u+w /home/a.txt
chmod o-x /home/a.txt
chmod a=x /home/a.txt

(2)三位8进制数字

chmod 664 /home/a.txt
chmod 640 /home/a.txt

chown


功能:修改文件的拥有者
格式:chown [参数] 用户名 文件名
实例:

chown user1 f1
chown -R user1 filegroup1


chgrp

功能:修改文件或目录的所属组
格式:chgrp [参数] 用户组名 文件名
常用选项:-R 递归修改文件或目录的所属组
实例:

chgrp users /abc/f2

umask


功能:查看或修改文件掩码,新建文件夹默认权限=0666,新建目录默认权限=0777,创建文件或目录的时候还要受到umask的影响。假设默认权限是mask,则实际创建的出来的文件权限是:mask & ~umask
格式:umask 权限值
说明:将现有的存取权限减去权限掩码后,即可产生建立文件时预设权限。超级用户默认掩码值为0022,普通用户默认为0002。
实例:

umask 755
umask //查看
umask 044//设置

file指令

功能说明:辨识文件类型。
语法:file [选项] 文件或目录...
常用选项:
        -c 详细显示指令执行过程,便于排错或分析程序执行的情形。
        -z 尝试去解读压缩文件的内容。
使用 sudo分配权限
(1)修改/etc/sudoers 文件分配文件

# chmod 740 /etc/sudoers
# vi /etc/sudoer


格式:接受权限的用户登陆的主机 =(执行命令的用户) 命令
(2)使用 sudo 调用授权的命令

$ sudo –u 用户名 命令


实例:

$sudo -u root /usr/sbin/useradd u2


四、目录的权限和粘滞位

可执行权限: 如果目录没有可执行权限, 则无法cd到目录中.
可读权限: 如果目录没有可读权限, 则无法用ls等命令查看目录中的文件内容.
可写权限: 如果目录没有可写权限, 则无法在目录中创建文件, 也无法在目录中删除文件.

粘滞位

当一个目录被设置为"粘滞位"(用chmod +t),则该目录下的文件只能由

        (1)超级管理员删除
        (2)该目录的所有者删除
        (3)该文件的所有者删除

make[2]: Entering directory ‘/home/bba/work/BBA1_5/MBB_Platform/apps/public/openssl-1.1.1f’ /usr/bin/perl apps/progs.pl apps/openssl > apps/progs.h mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/asn1pars.d.tmp -MT apps/asn1pars.o -c -o apps/asn1pars.o apps/asn1pars.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/ca.d.tmp -MT apps/ca.o -c -o apps/ca.o apps/ca.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/ciphers.d.tmp -MT apps/ciphers.o -c -o apps/ciphers.o apps/ciphers.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/cms.d.tmp -MT apps/cms.o -c -o apps/cms.o apps/cms.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/crl.d.tmp -MT apps/crl.o -c -o apps/crl.o apps/crl.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/crl2p7.d.tmp -MT apps/crl2p7.o -c -o apps/crl2p7.o apps/crl2p7.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/dgst.d.tmp -MT apps/dgst.o -c -o apps/dgst.o apps/dgst.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/dhparam.d.tmp -MT apps/dhparam.o -c -o apps/dhparam.o apps/dhparam.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/dsa.d.tmp -MT apps/dsa.o -c -o apps/dsa.o apps/dsa.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/dsaparam.d.tmp -MT apps/dsaparam.o -c -o apps/dsaparam.o apps/dsaparam.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/ec.d.tmp -MT apps/ec.o -c -o apps/ec.o apps/ec.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/ecparam.d.tmp -MT apps/ecparam.o -c -o apps/ecparam.o apps/ecparam.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/enc.d.tmp -MT apps/enc.o -c -o apps/enc.o apps/enc.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/engine.d.tmp -MT apps/engine.o -c -o apps/engine.o apps/engine.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/errstr.d.tmp -MT apps/errstr.o -c -o apps/errstr.o apps/errstr.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/gendsa.d.tmp -MT apps/gendsa.o -c -o apps/gendsa.o apps/gendsa.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/genpkey.d.tmp -MT apps/genpkey.o -c -o apps/genpkey.o apps/genpkey.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/genrsa.d.tmp -MT apps/genrsa.o -c -o apps/genrsa.o apps/genrsa.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/nseq.d.tmp -MT apps/nseq.o -c -o apps/nseq.o apps/nseq.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/ocsp.d.tmp -MT apps/ocsp.o -c -o apps/ocsp.o apps/ocsp.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/openssl.d.tmp -MT apps/openssl.o -c -o apps/openssl.o apps/openssl.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/passwd.d.tmp -MT apps/passwd.o -c -o apps/passwd.o apps/passwd.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/pkcs12.d.tmp -MT apps/pkcs12.o -c -o apps/pkcs12.o apps/pkcs12.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/pkcs7.d.tmp -MT apps/pkcs7.o -c -o apps/pkcs7.o apps/pkcs7.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/pkcs8.d.tmp -MT apps/pkcs8.o -c -o apps/pkcs8.o apps/pkcs8.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/pkey.d.tmp -MT apps/pkey.o -c -o apps/pkey.o apps/pkey.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/pkeyparam.d.tmp -MT apps/pkeyparam.o -c -o apps/pkeyparam.o apps/pkeyparam.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/pkeyutl.d.tmp -MT apps/pkeyutl.o -c -o apps/pkeyutl.o apps/pkeyutl.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/prime.d.tmp -MT apps/prime.o -c -o apps/prime.o apps/prime.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/rand.d.tmp -MT apps/rand.o -c -o apps/rand.o apps/rand.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/rehash.d.tmp -MT apps/rehash.o -c -o apps/rehash.o apps/rehash.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/req.d.tmp -MT apps/req.o -c -o apps/req.o apps/req.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/rsa.d.tmp -MT apps/rsa.o -c -o apps/rsa.o apps/rsa.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/rsautl.d.tmp -MT apps/rsautl.o -c -o apps/rsautl.o apps/rsautl.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/s_client.d.tmp -MT apps/s_client.o -c -o apps/s_client.o apps/s_client.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/s_server.d.tmp -MT apps/s_server.o -c -o apps/s_server.o apps/s_server.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/s_time.d.tmp -MT apps/s_time.o -c -o apps/s_time.o apps/s_time.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/sess_id.d.tmp -MT apps/sess_id.o -c -o apps/sess_id.o apps/sess_id.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/smime.d.tmp -MT apps/smime.o -c -o apps/smime.o apps/smime.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/speed.d.tmp -MT apps/speed.o -c -o apps/speed.o apps/speed.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/spkac.d.tmp -MT apps/spkac.o -c -o apps/spkac.o apps/spkac.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/srp.d.tmp -MT apps/srp.o -c -o apps/srp.o apps/srp.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/storeutl.d.tmp -MT apps/storeutl.o -c -o apps/storeutl.o apps/storeutl.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/ts.d.tmp -MT apps/ts.o -c -o apps/ts.o apps/ts.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/verify.d.tmp -MT apps/verify.o -c -o apps/verify.o apps/verify.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/version.d.tmp -MT apps/version.o -c -o apps/version.o apps/version.c mipsel-linux-gcc -I. -Iinclude -Iapps -pthread -Wall -Os -DGNU -DNDEBUG -MMD -MF apps/x509.d.tmp -MT apps/x509.o -c -o apps/x509.o apps/x509.c rm -f apps/openssl ${LDCMD:-mipsel-linux-gcc} -pthread -Wall -Os -DGNU -L. -o apps/openssl apps/asn1pars.o apps/ca.o apps/ciphers.o apps/cms.o apps/crl.o apps/crl2p7.o apps/dgst.o apps/dhparam.o apps/dsa.o apps/dsaparam.o apps/ec.o apps/ecparam.o apps/enc.o apps/engine.o apps/errstr.o apps/gendsa.o apps/genpkey.o apps/genrsa.o apps/nseq.o apps/ocsp.o apps/openssl.o apps/passwd.o apps/pkcs12.o apps/pkcs7.o apps/pkcs8.o apps/pkey.o apps/pkeyparam.o apps/pkeyutl.o apps/prime.o apps/rand.o apps/rehash.o apps/req.o apps/rsa.o apps/rsautl.o apps/s_client.o apps/s_server.o apps/s_time.o apps/sess_id.o apps/smime.o apps/speed.o apps/spkac.o apps/srp.o apps/storeutl.o apps/ts.o apps/verify.o apps/version.o apps/x509.o apps/libapps.a -lssl -lcrypto -ldl -pthread ./libcrypto.so: warning: gethostbyname is obsolescent, use getnameinfo() instead. ./libcrypto.so: undefined reference to getcontext' ./libcrypto.so: undefined reference to setcontext’ ./libcrypto.so: undefined reference to `makecontext’ collect2: ld returned 1 exit status Makefile:6187: recipe for target ‘apps/openssl’ failed make[2]: *** [apps/openssl] Error 1 make[2]: Leaving directory ‘/home/bba/work/BBA1_5/MBB_Platform/apps/public/openssl-1.1.1f’ Makefile:183: recipe for target ‘build_programs’ failed make[1]: *** [build_programs] Error 2 make[1]: Leaving directory ‘/home/bba/work/BBA1_5/MBB_Platform/apps/public/openssl-1.1.1f’ make/apps/pubapps/openssl.mk:3: recipe for target ‘openssl’ failed make: *** [openssl] Error 2
最新发布
09-23
你贴出的构建日志和之前的完全一致,说明 **尽管我们已经建议添加 `no-async`,但你还没有重新配置(re-configure)OpenSSL**。 --- ## 🔴 核心问题重申 ```bash undefined reference to `getcontext' undefined reference to `setcontext' undefined reference to `makecontext' ``` 这是因为在 **MIPS + 嵌入式 libc(如 uclibc/musl)环境中,`ucontext` 系列函数不可用**,而 OpenSSL 默认启用了依赖这些函数的 **异步上下文支持(`ASYNC`)**。 > ✅ 解决方法只有一条:**在执行 `./Configure` 时明确禁用 `async` 支持** --- ## ✅ 正确修复步骤(必须完整执行) ### 🛠️ 第一步:彻底清理旧构建状态 ```bash cd /home/bba/work/BBA1_5/MBB_Platform/apps/public/openssl-1.1.1f # 彻底清除之前生成的 Makefile、依赖、对象文件 make distclean ``` > ⚠️ 注意:如果 `make distclean` 失败或不存在,可以直接删除整个目录后解压干净源码。 --- ### 🛠️ 第二步:使用正确的 `./Configure` 命令(含 `no-async`) ```bash CC=mipsel-linux-gcc \ AR=mipsel-linux-ar \ RANLIB=mipsel-linux-ranlib \ ./Configure \ --prefix=/home/bba/work/BBA1_5/MBB_Platform/apps/public/openssl-1.1.1f/install \ --cross-compile-prefix=mipsel-linux- \ shared \ linux-generic32 \ no-zlib \ no-asm \ no-hw \ no-engine \ no-ssl3 \ no-comp \ no-err \ no-async \ # 👈 关键!必须加上这一行 no-srp \ no-dtls \ no-tls1 \ no-weak-ssl-ciphers \ -Os -DGNU -DNDEBUG -DCONFIG_TP_IMAGE ``` 📌 特别注意: - `no-async` 是解决 `getcontext` 错误的根本手段 - 必须在 `./Configure` 阶段加入,不能后期补 - 如果不重新运行 `./Configure`,`Makefile` 中仍会链接 `async.o` 模块 → 继续报错 --- ### 🛠️ 第三步:编译并安装 ```bash make depend # 更新依赖关系(可选) make -j$(nproc) make install ``` ✅ 成功后你会看到: - `install/lib/libcrypto.so` - `install/bin/openssl`(可选工具) - 不再出现 `getcontext` 链接错误 --- ## ❓ 为什么 `no-async` 能解决问题? | 组件 | 说明 | |------|------| | `async.o` / `async_wait.o` | OpenSSL 的异步任务调度模块 | | 它内部调用 | `getcontext`, `setcontext`, `makecontext` | | 这些函数在 glibc 中存在 | 但在 **musl、uclibc、bare-metal libc 中通常缺失** | | 所以链接失败 | 即使代码没用到 async 功能,只要模块被编译进去就会引用 | 👉 加上 `no-async` 后: - OpenSSL 宏定义中禁用 `OPENSSL_USE_ASYNC_JOB` - 相关代码不会被编译 - 不再调用 `ucontext` 函数 - 链接成功! --- ## ✅ 可选进一步裁剪(适合嵌入式设备) 继续减小体积和依赖: ```bash no-stdio # 禁用 stdin/stdout 相关功能(如 apps 模块) no-ui # 禁用用户交互接口 no-dso # 禁用动态加载引擎(避免 dlopen 等依赖) no-tests # 不编译测试程序 no-deprecated # 禁用废弃 API ``` 例如: ```bash no-async no-stdio no-ui no-dso no-tests no-deprecated ``` 如果你不需要 `apps/openssl` 工具本身,甚至可以跳过编译它。 --- ## ✅ 如何验证是否真的修复了? 成功构建后运行: ```bash file install/lib/libcrypto.so ``` 输出应类似: ``` ELF 32-bit LSB shared object, MIPS, MIPS32 version 1, dynamically linked, stripped ``` 确认是 MIPS 架构,并且能正常部署到目标设备。 --- ## 🧩 补充:关于 `gethostbyname` 警告 ```bash warning: gethostbyname is obsolescent, use getnameinfo() instead. ``` 这个只是 **警告(warning)**,不是错误。不影响编译。 它是 OpenSSL 自带代码使用的旧 API,在嵌入式环境下常见,可暂时忽略。 若想消除警告,需打补丁替换为 `getaddrinfo()`,但非必要。 --- ## ✅ 总结 | 问题 | 解法 | |------|------| | `undefined reference to getcontext` | ➕ `no-async` 到 `./Configure` | | 必须重新 configure | ❌ 仅 `make clean` 不够 | | 是否影响功能? | ✅ 普通 TLS 通信无任何影响 | | 推荐做法 | 在所有嵌入式平台都加 `no-async` | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值