Manage permissions and ownership of Linux directories, users, groups, and documents

Article directory

        • 1. Manage Linux directories and files
          • 1. View and switch directories
            • pwd: view the current working directory
            • cd: switch working directory
            • ls: list documents and attributes
            • alias: alias
            • cat view file content
            • Less pages display long files
          • 2. Create a new directory/file
            • mkdir creates a directory
            • touch to create a file
          • 3. Delete/Move/Copy
            • rm delete
            • mv mobile
            • cp copy
          • 4. vim editor
            • – vim configuration file .vimrc
            • Command line mode operation
            • – Cursor operation
            • – copy/paste/delete
            • – find/revoke
            • Last row mode operation
            • – save and exit
            • :wq save and exit or ZZ
            • Force quit without saving
            • – find/replace
            • Insert mode operation
        • Two, manage Linux users
          • 1. Account Control Description
          • 2. Add and modify users
          • -useradd add user
          • -usermod modify user
          • -userdel delete user
          • -id query user
          • -su command to switch users
          • 3. The difference between su and su –
        • 3. Manage group accounts in Linux
          • 1. Description of group management
          • 2. Classification of group accounts
          • 3. Add/manage/delete groups
          • -groupadd add group
          • -gpasswd manages group members
          • -groupdel delete group
        • 4. File ownership and authority
          • 1. Overview of Permissions and Attribution
          • 2. Permission bit field analysis
          • 3.chmod permission control
          • 4.chown ownership control
        • 5. Default permissions for files/directories

1. Manage Linux directories and files

1. View and switch directories
pwd: view the current working directory
[root@control ~]# pwd
/root
cd: switch working directory

– Format: cd [destination folder location]

cd ~root means to enter root's home directory
~Represents the home directory
.. indicates the upper directory
ls: list documents and attributes

– Format: ls [options] [directory or filename]…

? Common options:
? -l: show detailed information
? -A: Show everything including hidden data
? -d: display the directory itself
? -h: display units
? -R: display content recursively

ls -l /root
ls -A /root
ls -d /root
...
alias: alias

– Format: alias [alias name]

To set up a permanent alias, put the alias in the ~/.bashrc file

View aliases:
aliases

Set hostname to an alias of hn:
alias hm='hostname'

Remove the hn alias:
unalias hn
cat view file content

– Format: cat [file path]

[root@control ~]# cat /etc/passwd
less page display long files

– Format: less [file recording lesson]

[root@control ~]# less /etc/passwd
2. Create a new directory/file
mkdir create directory
Create /demo directory
[root@control ~]# mkdir /demo

Created with the parent directory
[root@control ~]# mkdir -p /opt/aa/bbb
touch to create a file
Create a 1.txt file
[root@control ~]# touch 1.txt
3. Delete/Move/Copy
rm delete

– format: rm [options] [arguments]

Common options:

? -r: delete recursively
? -f: force delete

[root@control ~]# rm -rf /demo/
mv mobile

-Format: mv source file directory path

The original data will disappear, and it can also be used to modify the name

[root@control ~]# mv demo/ /tmp/mvdemo
cp copy

-Format: cp [options] original file directory path

Common options

? -r : recursive copy

? -p: retain the original permissions, owner, group, special permissions

[root@control ~]# cp -r /tmp/mvdemo /demo
4.vim editor

– Function: vim is a text editor, if the edited text does not exist, vim will automatically create it

– Working mode: command mode, input mode, last line mode

-vim configuration file .vimrc

? The .vimrc file is a configuration file that configures the vim editor function on and off. Generally divided into two categories: system configuration and user configuration.

Example: Configure to display the line number every time you open a file with vim
vim ~/.vimrc
set number #Open vim to automatically display the line number
set ai #Automatic indentation
Command line mode operation
-Cursor operation

? Move the cursor: ↑ ↓ ← →

? Inline jump: home key or ^
? Jump between lines: gg jumps to the beginning of the line G jumps to the end of the file

-Copy/Paste/Delete

? yy copy one line, 3yy copy 3 lines

?p paste

? dd delete (cut)

? D Delete from the cursor to the end of the line

-Find/Revoke

? Find text: /keyword n skip to next result N skip to previous result

? Undo: u Note: Do not use ctrl + z

? Undo: ctrl + r

Last line mode operation
-Save and exit
:wq to save and exit or ZZ
Force exit without saving

? :q!

-Find/Replace

? Inline replacement

:s/old string/new string #Replace the first old string where the cursor is
Example: s/oo/OO
:s/old string/new string/g #Replace all old strings in the line where the cursor is
Example: s/o/O/g

? In-region replacement

:1,10s/old string/new string/g #Replace all old strings from line 1 to line 10
Example: 1,10s/bin/BIN/g
Insert mode operation

? – Press i or o to enter insert mode

? – uppercase C deletes a line and enters insert mode

Second, manage Linux users

1. Account Control Description

– Function: used to log in to the operating system, different users have different permissions

-UID: The unique identifier of the user, the number starts from 1, and the default is up to 60000
The UID of the administrator root is always 0
The UID of ordinary users starts from 1000 by default

root:x:0:0:root:/root:/bin/bash
Username: Password Placeholder: UID: GID: User Description Information: Home Directory: Interpreter
First field: username
The second field: password placeholder, always x
The third field: UID
The fourth field: basic group GID
The fifth field: user description information
The sixth field: user home directory
The seventh field: the path of the user shell interpreter

-/etc/passwd user account file: used to save the basic information of the account, each user records one line, separated by colons into 7 fields
Note: /etc/passwd- is the backup file of this file, which is automatically backed up by linux. Can be used for recovery if /etc/passwd is damaged

2. Add and modify users
-useraddAdd user

– Format: useradd [options] username
Common options:
-u: Specify UID (starts from 1000 by default)
-d: specify the home directory
-G: specify additional groups
-g: specify the basic group
-s: Specify the user’s interpreter Many interpreters are in /etc/shells

Add zs user and specify UID as 2000
[root@control ~]# useradd -u 2000 zs
[root@control ~]# tail -1 /etc/passwd #View the last line of the user file
zs:x:2000:2000::/home/zs:/bin/bash #You can see that the UID is 2000
Add liis user, specify the home directory as /opt/lisi
[root@control ~]# useradd -d /opt/lisi lisi
[root@control ~]# tail -1 /etc/passwd
lisi:x:2001:2001::/opt/lisi:/bin/bash
Add tom user, the additional group is adm
[root@control ~]# useradd -G adm tom
[root@control ~]# id tom
uid=2002(tom) gid=2002(tom) group=2002(tom),4(adm) #The basic group has the same name as the user name by default, and there are more adm groups
Add jack user, the basic group is the lisi group (the default basic group has the same name as the user name)
[root@control ~]# useradd -g lisi jack
[root@control ~]# id jack
uid=2003(jack) gid=2001(lisi) group=2001(lisi)
Add lili user, the interpreter is /sbin/nologin. The default interpreter is /bin/bash
[root@control ~]# useradd -s /sbin/nologin lili
[root@control ~]# tail -1 /etc/passwd
lili:x:2004:2004::/home/lili:/sbin/nologin
-usermod modify user
 - format: usermod [options] username

? Common options:
? -l: Change the user’s login name
? -u: change uid
? -d: change the home directory (the home directory will not be created automatically)
? -G: reset additional groups
? -s: change interpreter

-userdel delete user

? – format: userdel [options] username

? Common options:

? -r: Delete together with the home directory/user email

-id query user

? – format: id username

[root@control ~]# id one
uid=1000(one) gid=1000(one) group=1000(one)
Or: grep username /etc/passwd
-su command to switch users

? Function: The function of su is to switch user identities. You need to enter the password of the user, except for the root user.

? – Format: su – username

[root@control ~]# su - one
[one@control ~]$
3. The difference between su and su-

su is to switch to other users, but does not switch environment variables, which directory it was in before switching, and remains in this directory after switching, without switching the environment

su – is a complete switch to a user environment, automatically to the new user’s home directory after switching

3. Manage group accounts in Linux

1. Group management description

-Function: Convenient to manage users

-GID: the unique identifier of the group, the number starts from 0, and the default maximum is 60000

-Principle: A Linux user must belong to at least one group

2. Group account classification

-Basic group: created by the system with the same name as the user

-Additional groups (subordinate groups): Created by administrators, joined by administrators

-group account file /etc/group

This file is used to save the group account information, each group records one line, separated by colons into 4 fields
one:x:1000:one
Group Name: Password Placeholder: GID: Group Members

-Group management information file /etc/gshadow

one:!:kaka:jack
Group Name: Password Placeholder: Group Admins: Group Members
3. Add/manage/delete groups
-groupadd add group

? Format: groupadd group name

[root@control ~]# groupadd NTD

View NTD group:
[root@control ~]# grep NTD /etc/group
NTD:x:2005:
-gpasswd management group member

? Format: gpasswd [options] username groupname

? Common options:
? -a: Add group members, one at a time
? -d: Delete group members, one at a time
? -A: Define the list of group administrators, so that a user can manage this group, and can add and delete users to this group
-M: Define the user list of group members, you can set multiple gpasswd -M ‘user 1, user 2’ group name, add multiple users to the group, the original members in the group will be removed

-groupdel delete group

? Format: groupdel group name
? Note: The deleted group cannot be the user’s basic group

Fourth, file ownership and permissions

1. Overview of permissions and attribution

? -access permission:

? Read: read allows to view the content can also be represented by 4

? Write: write allows modification of the content and can also be represented by 2

? Execute: Allow running and switching: execute can also be represented by 1

? – Affiliation:

? Owner (owner): user is the user who owns this file or directory

? Belonging group (belongs to the group): group owns the group of this file or directory

? Other users: other users except the owner and the group they belong to

2. Permission field analysis

The output information includes 7 fields:
dr-xr-x---. 16 root root 4096 Sep 26 10:32 /root
permission bit
Number of subdirectories/number of hard links
owner (owner)
Belonging group (belonging group)
size
\tLast Modified
\tFile Directory
ls -l or ls -ld command to view
Start with -: text file Example: ls -l /etc/passwd
Start with d: directory Example: ls -ld /etc
Start with l: shortcut Example: ls -l /dev/cdrom
3.chmod permission control

? – Format: chmod [ugoa] [rwx] filename

?u:user g:group o:other a:all

?r:read w:write x:execute

? Common options:
? -R: recursively modify permissions

[root@control ~]# ls -l /opt/1.txt #View
-rw-r--r-- 1 root root 0 Apr 24 14:47 /opt/1.txt

Add the write permission of the group to /opt/1.txt
[root@control ~]# chmod g + w /opt/1.txt
[root@control ~]# ls -l /opt/1.txt
-rw-rw-r-- 1 root root 0 Apr 24 14:47 /opt/1.txt
4.chown ownership control

? – Format: chown owner file

Modify owner:
[root@control ~]# ls -l /opt/1.txt #Check before modifying the attribution:
-rw-rw-r-- 1 root root 0 April 24 14:47 /opt/1.txt #The owner is root

[root@control ~]# chown one /opt/1.txt #Change the owner to one user
[root@control ~]# ls -l /opt/1.txt
-rw-rw-r-- 1 one root 0 Apr 24 14:47 /opt/1.txt
Modify owner and group:
[root@control ~]# ls -l /opt/1.txt #Check before modifying the attribution:
-rw-rw-r-- 1 one root 0 April 24 14:47 /opt/1.txt #The owner is one and the group is root
\t
[root@control ~]# chown zs:zs /opt/1.txt #Change the owner and group to zs
[root@control ~]# ls -l /opt/1.txt
-rw-rw-r-- 1 zs zs 0 Apr 24 14:47 /opt/1.txt
Modify the attribute group:
[root@control ~]# ls -l /opt/1.txt #View before modifying attribution
-rw-rw-r-- 1 zs zs 0 April 24 14:47 /opt/1.txt #The group is zs

[root@control ~]# chown :root /opt/1.txt #Change the belonging group to the root group
[root@control ~]# ls -l /opt/1.txt
-rw-rw-r-- 1 zs root 0 Apr 24 14:47 /opt/1.txt

5. Default permissions for files/directories

-Directory: The default permission for new directories is 755
Default permissions depend on umask settings
The default umask is 0022, where the first 0 is an additional permission, if you don’t want to see it, just look at 022
777-022=755

-File: The default permission for new files is 644
Because the general file does not give x execution permission by default, 755 minus the execution permission is 644