Users, Files

This is an introduction to “users” and “files” in Linux.

Server administrators can create users for different users and assign different privileges to ensure the normal operation of the system. They can also create users for network services and limit the privileges to reduce the damage to the system security when the services are attacked.

Modern operating systems generally distinguish between user “user” and “system user”, and divide the privileges to ensure that the integrity of the system will not be damaged by user’s misuse or malicious programs.

Users in Linux


You can see various user information in /etc/passwd.

Root user

The root user /root user has the highest privileges in the Linux operating system and can do anything to the system (including the extremely dangerous operation of deleting all system files). The root user’s data is stored under /root.

Use the root privilege carefully to execute commands (such as sudo), do not execute the following commands:

  • rm -rf /
  • mkfs.ext4 /dev/sda
  • dd if=/dev/urandom of=/dev/sda
  • :(){ :|: & };:

System users

System users are created by the system or related programs to perform system tasks such as services, e.g. nobody , www-data and so on. Do not delete these users at random.

Normal users

The normal user is located at /home/username/ and username is the username. Normal users can log in to the system and operate on files in their home directory. They cannot directly modify the system configuration or install or uninstall software for the system environment.

Switching users


sudo

sudo is used to execute the specified command as another user.

1
2
3
$ sudo + command # Execute the command as the root user
$ sudo !!! # Execute the previous command as the root user
$ sudo -u username command # Execute the command as a user other than root

su

su is used to switch users directly.

Note that Linux distributions such as Ubuntu disable password login for the root user by default, so you can’t use su directly and have to raise privileges with sudo.

1
2
$ sudo su # Switch to root 
$ su username # Switch to another user

User group


A user group is a collection of users. User groups can set permissions for a group of users. User groups also have a number GID (Group ID).

1
2
3
4
5
6
7
8
$ group # See which user group you belong to

$ passwd username # Change user username's password
$ passwd # Change your own password

$ sudo adduser username # Add user username
$ sudo adduser --group groupname # Add the user groupname
$ sudo adduser username groupname # Add user username to usergroup groupname

File permissions


1
2
3
4
$ ls -l # View the details of the files in the current directory
## eg. -rwxrw-r-- 1 ustc ustc 40 Feb 3 22:37 a_file
## a_file is a normal file (-); the permission of the user it belongs to is rwx, the permission of the user group it belongs to is rw-, the permission of others is r--; both the user and the user group it belongs to is ustc
## r for Read, w for Write, x for Execute, - for no corresponding permission

Execute permissions

For a file, execute permission means that it can be executed as program code by the operating system. If a program file does not have execute permissions, you can still view the program file itself and modify its contents, but you cannot execute it.

For a directory, execute permission means that you can access the contents of the files in that directory. You can think of a directory as a “file”, which contains a list of files at the next level of the directory, “read” corresponds to the permission to read the list of files, “write” corresponds to the permission to modify the list of files (add, delete, rename files), and “execute” for actually accessing the files in the list, and cd for switching from the current directory to this directory.

1
2
3
4
$ chmod # change file mode bits to modify permissions
$ chmod +x program # Add execute permissions to the file program (x)

$ chown # change file owner Modify file owner

File system hierarchy


The entire UNIX family of systems starts with / (the root directory) as a tree; other partitions are “mounted” on this tree.

The Filesystem Hierarchy Standard (FHS) defines the standard directory structure for Linux distributions and can be found at Filesystem Hierarchy Standard. You can also use man hier and man file-hierarchy to see the documentation on the filesystem hierarchy on your system.

Appendix


Linux 101 - chap 05: 用户与用户组、文件权限、文件系统层次结构

Basic Components of Electronics of Particle Physics Experiments Apr 2023

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×