liunx chmod command

本文深入探讨了Linux/Unix环境中chmod命令的功能、语法、权限设置及应用实例,包括如何通过命令修改文件权限,理解权限表示及其数值表示,并提供常见场景下的使用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Linux / Unix chmod command

Quick links

About chmod
Syntax
Examples
Additional information
Related commands
Linux / Unix main page

About chmod

Changes the permission of a file.

Syntax

chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

-c, --changeslike verbose but report only when a change is made
--no-preserve-rootdo not treat `/' specially (the default)
--preserve-rootfail to operate recursively on `/'
-f, --silent, --quietsuppress most error messages
-v, verboseoutput a diagnostic for every file processed
--reference=RFILEuse RFILE's mode instead of MODE values
-R, --recursivechange files and directories recursively
--helpdisplay this help and exit
--versionoutput version information and exit

Permissions
u - User who owns the file.
g - Group that owns the file.
o - Other.
a - All.
r - Read the file.
w - Write or edit the file.
x - Execute or run the file as a program.

Numeric Permissions:
CHMOD can also to attributed by using Numeric Permissions:

400 read by owner
040 read by group
004 read by anybody (other)
200 write by owner
020 write by group
002 write by anybody
100 execute by owner
010 execute by group
001 execute by anybody

Examples

The above numeric permissions can be added to set a certain permission, for example, a common HTML file on a Unix server to be only viewed over the Internet would be:

chmod 644 file.htm

This gives the file read/write by the owner and only read by everyone else (-rw-r--r--).

Files such as scripts that need to be executed need more permissions. Below is another example of a common permission given to scripts.

chmod 755 file.cgi

This would be the following 400+040+004+200+100+010+001 = 755 where you are giving all the rights except the capability for anyone to write to the file.cgi file(-rwxr-xr-x).

Finally, another common CHMOD permission is 666, as shown below, which is read and write by everyone.

chmod 666 file.txt

Additional information

Below is an example of how a file may be listed when typing ( ls -l ) at the prompt as well as information on how to interpret it.

-rw-rw-r-- 1   hope  123   Feb 03 15:36   file.txt

-rwrw-r--1hope123Feb 03 15:36file.txt
Fileownergroupeveryone elselinksownersizemod datefile name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值