Chuyển đến nội dung chính
eLearner.app
Mô-đun 4 · Bài học 2 trong tổng số 29/11 trong khóa học~10 min
Bài học theo mô-đun (2/2)

Quyền sở hữu và đặc quyền (chown và sudo)

In a multi-user operating system like Linux, every resource has an owner and belongs to a specific group. Managing ownership and temporarily acquiring elevated privileges are key to system security.


Modifying Owner and Group: chown and chgrp

The chown (change owner) command allows you to change the owner user and/or group of a file or directory. Only the system administrator (root) or the current owner (with sufficient privileges) can make this change.

chown Syntax

The primary syntax of chown allows specifying the new owner and optionally the group separated by a colon ::

Bash
chown admin report.txt           # Changes owner to 'admin'
chown admin:staff report.txt     # Changes owner to 'admin' and group to 'staff'
chown :staff report.txt          # Changes group only to 'staff' (equivalent to chgrp)

The chgrp (change group) command is dedicated exclusively to changing the group owner of a file or directory:

Bash
chgrp developers script.sh       # Sets the file group to 'developers'

Superuser Privileges: sudo

In Linux, the administrative user is named root and has complete control over the system. For security reasons, you should never work continuously as root. Instead, you use the sudo (superuser do) command.

sudo allows an authorized user to execute a single command with root privileges (or another user's privileges):

Bash
sudo chown root:root private.key  # Changes owner to root by requesting elevated privileges

When you run a command with sudo, the system usually asks you to enter your current user's password (not root's) to confirm your identity and authorize the operation.


Try it yourself

Exercise 1: Change owner

tập thể dục#linux.m4.l2.e1
Nỗ lực: 0Đang tải…

Modify the owner user of the 'script.sh' file to be 'admin'.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Use the command 'chown admin script.sh' to change the owner.

Giải pháp khả dụng sau 3 lần thử

Exercise 2: Change group owner

tập thể dục#linux.m4.l2.e2
Nỗ lực: 0Đang tải…

Modify the group associated with the 'runner.sh' file, setting it to 'developers'.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Use the chgrp command followed by the 'developers' group and the 'runner.sh' file.

Giải pháp khả dụng sau 3 lần thử

Exercise 3: Change owner and group with sudo

tập thể dục#linux.m4.l2.e3
Nỗ lực: 0Đang tải…

Modify both the owner and group of the file 'private.key' to 'root'. Since this is a protected system operation, run the command with administrator privileges using 'sudo'.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Run chown preceded by sudo. Specify 'root:root' as owner:group and 'private.key' as the file.

Giải pháp khả dụng sau 3 lần thử