Linux study notes-user management and file permissions

User Management

useraddadd user

# 需要在root用户下进行使用该命令
$ useradd tony #创建新用户tony,并创建一个主目录为tony
$ useradd -d /home/dave david #创建新用户,并用-d参数指定新的主目录为dave
$ passwd tony #为tony设置密码
⚠️: $ useradd -g bigdata xiaoming #创建用户xiaoming并添加到bigdata组内

id, /etc/passwd to query whether the user exists

$ id tony # 查询该用户是否存在
uid=0(root) gid=0(root)=0(root) #输出
$ less /etc/passwd #查看/etc/passwd配置文件
#文件第一行为root
#文件最后的行为新添加的用户

Insert image description here

su switches users

$ su pirmingham #切换到pirmingham用户

whoami command to view sessions

$ whoami # 查看当前会话用户是谁?
$ who am i #查看创建当前会话的用户是谁?

sudo temporarily elevates privileges to root for ordinary users

$ sudo 命令 #

/etc/sudoers configuration file configures privileged users

This configuration file contains users who can execute sudo commands.

## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL 

## Allows members of the 'sys' group to run networking, software, 
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
 %wheel ALL=(ALL)       ALL

## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

## Allows members of the users group to mount and unmount the 
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

userdel delete user

$ userdel tony # 删除tony用户,但不会删除home下的家目录
$ userdel -r tony #删除tony用户,并同时删除home家目录
## 注意⚠️:需要在root用户下执行该命令

User group management

groupaddadd group

$ groupadd 新组名

/etc/group View user group information

$ less /etc/group #查看组信息
haircut:x:1002:
tony:x:1000:
david:x:1001:

usermod modifies the user's group

$ usermod -g "组名" "用户名"
$ usermod -g haircut tony

groupmod modifies the group name of the user group

$ groupmod -n "newGroupName" "oldGroupName"
# 对比记忆 
$ mv "oldName" "newName"

File Permissions

File attributes - 1st letter

  • -: Ordinary file
  • d: directory file
  • l: a link file
  • c: character device-mouse-keyboard-shell typing
  • b: block device - hard disk

File permissions - 1 to 9 digits

1~3:user: Owner permissions

4~6:group: Group permissions

7~9:other: other user permissions

The meaning of files and directories rwx represents

document

  • r stands for readable and can be viewed
  • w means writable, but it does not mean it can be deleted. If it can be deleted, the permissions of its parent folder (the folder it is located in) must also be w before the file can be deleted.
  • x means it can be executed by the system

Table of contents

  • r stands for readable and can be viewed by ls; without r, it can also be entered by cd. Without r, it only affects ls viewing and reading;
  • w means modifiable, that is, create files, delete files, delete directories, and rename directories.
  • x means that the directory can be entered, that is to say, without x, it cannot be entered by cd

⚠️: The above is for ordinary users, root users are not restricted

⚠️: sudo cd cannot enter restricted directories, chatgpt provides a cd method:

$ sudo -s #打开root用户会话,mac也适用

summary

document Table of contents
r read It is not that cd and cd are controlled by x, but can be controlled by ls
w Write, but not delete, deletion is controlled by the folder Create files, delete files, delete directories, rename files
x Executable Can be entered, that is, can be entered by cd

ls file information

Insert image description here

⚠️: File size in bytes B

chmod changes file permissions

Way 1

$ chmod {
    
    ugoa}{
    
    +-=}{
    
    rwx} 文件或目录
$ chmod u=rwx file #给file用户权限添加rwx权限

Way 2

$ chmod [mode=421][mode=421][mode=421] 文件或目录
$ chmod 777 file #给file用户权限、组权限、其他权限都设置为rwx;

chown change user

$ chown tony 文件或目录 #改变文件或目录的用户所属
$ chown -R tony 目录 #递归地改变目录的用户所属

chgrp changes the group it belongs to

$ chgrp 用户组 文件或目录 #改变文件或目录的所属组
$ chgrp -R 用户组 目录 #递归地改变用户的所属组

Guess you like

Origin blog.csdn.net/sinat_26394043/article/details/132678286