|
| 1 | +Chmod 备忘清单 |
| 2 | +=== |
| 3 | + |
| 4 | +这份快速参考备忘单提供了文件权限的简要概述,以及 chmod 命令的操作 |
| 5 | + |
| 6 | +入门 |
| 7 | +-------- |
| 8 | + |
| 9 | +### 语法 |
| 10 | + |
| 11 | +```shell |
| 12 | +$ chmod [options] <permissions> <file> |
| 13 | +``` |
| 14 | + |
| 15 | +#### 示例 |
| 16 | + |
| 17 | +```shell |
| 18 | +$ chmod 755 foo.txt |
| 19 | +$ chmod +x quickref.py |
| 20 | +$ chmod u-x quickref.py |
| 21 | +$ chmod u=rwx,g=rx,o= quickref.sh |
| 22 | +``` |
| 23 | + |
| 24 | +#### 递归更改文件和目录 |
| 25 | + |
| 26 | +```shell |
| 27 | +$ chmod -R 755 my_directory |
| 28 | +``` |
| 29 | + |
| 30 | +`chmod` 命令代表“更改模式” |
| 31 | + |
| 32 | +### Chmod 生成器 |
| 33 | + |
| 34 | +```html |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +Chmod 生成器允许您以数字和符号的形式快速、直观地生成权限。 |
| 39 | + |
| 40 | +### 通用权限 |
| 41 | + |
| 42 | +命令 | s | 含义 |
| 43 | +:-|:-|:- |
| 44 | +`400` | r-------- | 仅所有者可读 |
| 45 | +`500` | r-x------ | 避免改变 |
| 46 | +`600` | rw------- | 可由用户更改 |
| 47 | +`644` | rw-r--r-- | 由用户读取和更改 |
| 48 | +`660` | rw-rw---- | 可由用户和组更改 |
| 49 | +`700` | rwx------ | 只有用户具有完全访问权限 |
| 50 | +`755` | rwxr-xr-x | 只能由用户更改 |
| 51 | +`775` | rwxrwxr-x | 群组共享模式 |
| 52 | +`777` | rwxrwxrwx | 每个人都可以做任何事 |
| 53 | + |
| 54 | +### 解释 |
| 55 | + |
| 56 | +```shell |
| 57 | +$ ls -l |
| 58 | +-rw-r--r-- 1 root root 3 Jun 29 15:35 a.log |
| 59 | +drwxr-xr-x 2 root root 2 Jun 30 18:06 dir |
| 60 | +``` |
| 61 | + |
| 62 | +#### `dir` 的权限分析 |
| 63 | + |
| 64 | +```text |
| 65 | +d rwx r-x r-x |
| 66 | +┬ ─┬─ ─┬─ ─┬─ |
| 67 | +│ │ │ │ |
| 68 | +│ │ │ └─ 4. Other|5 (4+0+1) |
| 69 | +│ │ └────── 3. Group|5 (4+0+1) |
| 70 | +│ └─────────── 2. User |7 (4+2+1) |
| 71 | +└─────────────── 1. File Type | directory |
| 72 | +``` |
| 73 | + |
| 74 | +### 权限模式 |
| 75 | +<!--rehype:wrap-class=col-span-2--> |
| 76 | + |
| 77 | +| 权限 | 描述 | 八进制 | 十进制 | |
| 78 | +|------------|-------------------------|-------|-----------| |
| 79 | +`---` | 没有权限 | 000 | 0 (0+0+0) |
| 80 | +`--x` | 执行 | 001 | 1 (0+0+1) |
| 81 | +`-w-` | 写 | 010 | 2 (0+2+0) |
| 82 | +`-wx` | 执行和写入 | 011 | 3 (0+2+1) |
| 83 | +`r--` | 读 | 100 | 4 (4+0+0) |
| 84 | +`r-x` | 读取和执行 | 101 | 5 (4+0+1) |
| 85 | +`rw-` | 读和写 | 110 | 6 (4+2+0) |
| 86 | +`rwx` | 读取、写入和执行 | 111 | 7 (4+2+1) |
| 87 | +<!--rehype:className=show-header--> |
| 88 | + |
| 89 | +### Objects |
| 90 | + |
| 91 | +| 谁(缩写) | 含义 | |
| 92 | +|-------------|--------------------| |
| 93 | +| `u` | 用户 | |
| 94 | +| `g` | 组 | |
| 95 | +| `o` | 其它 | |
| 96 | +| `a` | 全部,和 ugo 一样 | |
| 97 | +<!--rehype:className=show-header--> |
| 98 | + |
| 99 | + |
| 100 | +### 权限 |
| 101 | + |
| 102 | +| 缩写 | 权限 | 值 | |
| 103 | +|--------------|---------------|-------| |
| 104 | +| `r` | 读 | 4 | |
| 105 | +| `w` | 写 | 2 | |
| 106 | +| `x` | 执行 | 1 | |
| 107 | +| `-` | 没有权限 | 0 | |
| 108 | +<!--rehype:className=show-header--> |
| 109 | + |
| 110 | +### 文件类型 |
| 111 | + |
| 112 | +| 缩写 | 文件类型 | |
| 113 | +|--------------|----------| |
| 114 | +| `d` | 目录 | |
| 115 | +| `-` | 常规文件 | |
| 116 | +| `l` | 符号链接 | |
| 117 | +<!--rehype:className=show-header--> |
| 118 | + |
| 119 | +Chmod 示例 |
| 120 | +-------- |
| 121 | + |
| 122 | +### 操作符 |
| 123 | + |
| 124 | +| Symbol | Description | |
| 125 | +|--------|-------------| |
| 126 | +| `+` | 添加 | |
| 127 | +| `-` | 删除 | |
| 128 | +| `=` | 设置 | |
| 129 | + |
| 130 | + |
| 131 | +### chmod 600 |
| 132 | + |
| 133 | +```shell |
| 134 | +$ chmod 600 example.txt |
| 135 | +$ chmod u=rw,g=,o= example.txt |
| 136 | +$ chmod a+rwx,u-x,g-rwx,o-rwx example.txt |
| 137 | +``` |
| 138 | + |
| 139 | +### chmod 664 |
| 140 | + |
| 141 | +```shell |
| 142 | +$ chmod 664 example.txt |
| 143 | +$ chmod u=rw,g=rw,o=r example.txt |
| 144 | +$ chmod a+rwx,u-x,g-x,o-wx example.txt |
| 145 | +``` |
| 146 | + |
| 147 | +### chmod 777 |
| 148 | + |
| 149 | +```shell |
| 150 | +$ chmod 777 example.txt |
| 151 | +$ chmod u=rwx,g=rwx,o=rwx example.txt |
| 152 | +$ chmod a=rwx example.txt |
| 153 | +``` |
| 154 | + |
| 155 | +### 符号模式 |
| 156 | +<!--rehype:wrap-class=row-span-3--> |
| 157 | + |
| 158 | +拒绝所有人的执行权限。 |
| 159 | + |
| 160 | +```shell |
| 161 | +$ chmod a-x chmodExampleFile.txt |
| 162 | +``` |
| 163 | + |
| 164 | +向所有人授予读取权限。 |
| 165 | + |
| 166 | +```shell |
| 167 | +$ chmod a+r chmodExampleFile.txt |
| 168 | +``` |
| 169 | + |
| 170 | +使文件可由组和其他人读写。 |
| 171 | + |
| 172 | +```shell |
| 173 | +$ chmod go+rw chmodExampleFile.txt |
| 174 | +``` |
| 175 | + |
| 176 | +使用户/所有者可执行 shell。 |
| 177 | + |
| 178 | +```shell |
| 179 | +$ chmod u+x chmodExampleScript.sh |
| 180 | +``` |
| 181 | + |
| 182 | +允许每个人读取、写入和执行文件并打开设置的 group-ID。 |
| 183 | + |
| 184 | +```shell |
| 185 | +$ chmod =rwx,g+s chmodExampleScript.sh |
| 186 | +``` |
| 187 | + |
| 188 | +### 删除权限 |
| 189 | +<!--rehype:wrap-class=row-span-3--> |
| 190 | + |
| 191 | +要删除赋予文件的读写权限,请使用以下语法: |
| 192 | + |
| 193 | +```shell |
| 194 | +$ chmod o-rw example.txt |
| 195 | +``` |
| 196 | + |
| 197 | +对于我们的文件 example.txt,我们可以通过运行以下命令使用 chmod for group 删除读写权限: |
| 198 | + |
| 199 | +```shell |
| 200 | +$ chmod g-rx example.txt |
| 201 | +``` |
| 202 | + |
| 203 | +要从组中删除 chmod 读写权限,同时向 public/others 添加读写权限,我们可以使用以下命令: |
| 204 | + |
| 205 | +```shell |
| 206 | +$ chmod g-rx, o+rx example.txt |
| 207 | +``` |
| 208 | + |
| 209 | +但是,如果你想删除组和其他人的所有权限,你可以使用 go= 来代替: |
| 210 | + |
| 211 | +```shell |
| 212 | +$ chmod go= example.txt |
| 213 | +``` |
| 214 | + |
| 215 | +### 可执行文件 |
| 216 | + |
| 217 | +```shell |
| 218 | +$ chmod +x ~/example.py |
| 219 | +$ chmod u+x ~/example.py |
| 220 | +$ chmod a+x ~/example.py |
| 221 | +``` |
| 222 | + |
| 223 | +### chmod 754 |
| 224 | + |
| 225 | +```shell |
| 226 | +$ chmod 754 foo.sh |
| 227 | +$ chmod u=rwx,g=rx,o=r foo.sh |
| 228 | +``` |
| 229 | + |
| 230 | +Chmod 实践 |
| 231 | +--------------- |
| 232 | + |
| 233 | +### SSH 权限 |
| 234 | + |
| 235 | +```shell |
| 236 | +$ chmod 700 ~/.ssh |
| 237 | +$ chmod 600 ~/.ssh/authorized_keys |
| 238 | +$ chmod 600 ~/.ssh/id_rsa |
| 239 | +$ chmod 600 ~/.ssh/id_rsa.pub |
| 240 | +$ chmod 400 /path/to/access_key.pem |
| 241 | +``` |
| 242 | + |
| 243 | +### 网络权限 |
| 244 | + |
| 245 | +```shell |
| 246 | +$ chmod -R 644 /var/www/html/ |
| 247 | +$ chmod 644 .htaccess |
| 248 | +$ chmod 644 robots.txt |
| 249 | +$ chmod 755 /var/www/uploads/ |
| 250 | +$ find /var/www/html -type d -exec chmod 755 {} \; |
| 251 | +``` |
| 252 | + |
| 253 | +### 批量更改 |
| 254 | + |
| 255 | +```shell |
| 256 | +$ chmod -R 644 /your_path |
| 257 | +$ find /path -type d -exec chmod 755 {} \; |
| 258 | +$ find /path -type f -exec chmod 644 {} \; |
| 259 | +``` |
| 260 | + |
| 261 | +请参阅:[命令替换](https://tldp.org/LDP/abs/html/commandsub.html) |
| 262 | + |
| 263 | +另见 |
| 264 | +---- |
| 265 | + |
| 266 | +* [使用 chmod 修改文件权限](https://www.linode.com/docs/guides/modify-file-permissions-with-chmod/) _(linode.com)_ |
0 commit comments