0Pricing
Linux Command Line Mastery · 课时

权限与所有权:`chmod`、`chown`

了解并管理文件权限和所有权,从而安全地控制对数据的访问。

权限与所有权:`chmod`、`chown` 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Permissions: Who Can Do What?

In Linux, file permissions and ownership are vital for security and managing access to your data. They determine who can read, write, or execute files and directories.

Think of it like a set of rules for your digital property. Without proper permissions, anyone could access or modify your important files!

The Three Pillars: UGO

Linux permissions are categorized into three main types of users, often referred to as UGO:

  • User (Owner): The person who created the file or directory.
  • Group: A collection of users who share specific access rights to the file.
  • Others: Everyone else on the system who is not the owner and not part of the file's group.

Action Types: Read, Write, Execute

For each of the UGO categories, there are three basic types of actions they can perform:

  • Read (r): Allows viewing the file's content or listing a directory's contents.
  • Write (w): Allows modifying or deleting the file, or creating/deleting files within a directory.
  • Execute (x): Allows running a file (if it's a program or script) or entering a directory.

Viewing Permissions with `ls -l`

You can see a file's permissions and ownership using the ls -l command. This command provides a detailed, 'long' listing of files.

The first column shows the file type and permissions in a symbolic format, like -rwxr-xr--.

touch example.txt
ls -l example.txt

`chmod`: Symbolic Mode Basics

The chmod (change mode) command is used to change file permissions. In symbolic mode, you specify who (u, g, o, a) gets what permission (r, w, x) using operators (+, -, =).

  • u: user (owner)
  • g: group
  • o: others
  • a: all (u, g, o)
  • +: add permission
  • -: remove permission
  • =: set permission exactly

`chmod` Symbolic Mode in Action

Let's make our example.txt file executable for the owner, readable for the group, and remove all permissions for others.

Notice how the permissions string changes after each command.

touch example.txt
chmod u+x example.txt
chmod g=r example.txt
chmod o-rwx example.txt
ls -l example.txt

`chmod`: Octal Mode Explained

Octal mode is a numerical way to set permissions. Each permission (r, w, x) is assigned a numerical value:

  • r (read) = 4
  • w (write) = 2
  • x (execute) = 1

You sum these values for each UGO category. For instance, rwx (4+2+1) is 7, and r-x (4+0+1) is 5.

`chmod` Octal Mode Example

Common octal permissions include:

  • 755: Owner (rwx), Group (rx), Others (rx)
  • 644: Owner (rw), Group (r), Others (r)
  • 600: Owner (rw), Group (-), Others (-)

Let's set example.txt to rw-r--r-- (644).

touch another_file.txt
chmod 644 another_file.txt
ls -l another_file.txt

Changing Ownership with `chown`

The chown (change owner) command is used to change the user and/or group ownership of a file or directory.

Its basic syntax is chown [USER]:[GROUP] [FILE]. You can change just the user, just the group, or both. This command usually requires sudo privileges.

`chown` in Practice

Let's create a file and then change its owner. For this example, we'll assume a user named 'coddy' exists. If not, replace 'coddy' with an existing user on your system.

Note: You'll likely need sudo to execute chown.

touch owned_file.txt
ls -l owned_file.txt
# Assuming 'coddy' is a valid user
sudo chown coddy owned_file.txt
ls -l owned_file.txt

Permissions Check

You have a file named report.txt. You want the owner to have full read, write, and execute permissions. The group should only have read and execute permissions. Others should have no permissions at all.

Which chmod command (octal mode) achieves this?

Recap: Master Your Files!

Great job! You've learned how to manage file permissions and ownership in Linux.

  • ls -l shows permissions in symbolic format.
  • chmod changes permissions using symbolic (u+r) or octal (755) modes.
  • chown changes the owner and/or group of a file, often requiring sudo.

Understanding these commands is crucial for securing your system and collaborating effectively!

常见问题解答

「权限与所有权:`chmod`、`chown`」课时是免费的吗?

是的 — 「权限与所有权:`chmod`、`chown`」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。

「权限与所有权:`chmod`、`chown`」这节课中我会学到什么?

了解并管理文件权限和所有权,从而安全地控制对数据的访问。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Command Line Mastery 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「权限与所有权:`chmod`、`chown`」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?

能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 查看文件内容:`cat`、`less`、`more`
  2. 搜索文件:`find` 和 `locate`
  3. 权限与所有权:`chmod`、`chown`
  4. 创建与使用符号链接
← 返回 Linux Command Line Mastery