N63050 operation and maintenance in the second week

1. Displays the files or directories in the / etc directory that start with a non letter followed by a letter and any other characters of any length

1 [root@centos8 etc]#touch 5a.txt 9a.txt
2 [root@centos8 etc]#mkdir 3eeee
3 [root@centos8 etc]#ls -d /etc/[^[:alpha:]][[:alpha:]]*
4 /etc/3eeee  /etc/5a.txt  /etc/9a.txt

2. Copy all files or directories starting with p and ending with non numbers in the / etc directory to the / tmp/mytest1 directory.

1 [root@centos8 ~]#mkdir -p /tmp/mytest1
2 [root@centos8 ~]#cd /tmp/mytest1
3 [root@centos8 mytest1]#cp -rf /etc/p[[:alpha:]]*[^[:digit:]] /tmp/mytest1
4 [root@centos8 mytest1]#ls /tmp/mytest1
5 pam.d      passwd   pbm2ppa.conf  pipewire  plymouth      popt.d   prelink.conf.d  profile    protocols
6 papersize  passwd-  pinforc       pki       pnm2ppa.conf  postfix  printcap        profile.d  pulse

3. Convert the contents of the / etc/issue file to uppercase and save it to / TMP / issue Out file

 1 [root@centos8 ~]#ls /etc/issue
 2 /etc/issue
 3 [root@centos8 ~]#cat /etc/issue
 4 \S
 5 Kernel \r on an \m
 6 [root@centos8 ~]#cat /etc/issue |tr a-z A-Z
 7 \S
 8 KERNEL \R ON AN \M
 9 [root@centos8 ~]#cat /etc/issue |tr a-z A-Z >> /tmp/issue.out
10 [root@centos8 ~]#cat /tmp/issue.out
11 \S
12 KERNEL \R ON AN \M

4. Please summarize and describe how to use user and group management commands and complete the following exercises:

User management commands
useradd
usermod
userdel


Group account maintenance command
groupadd
groupmod
groupdel

 

4.1 useradd user creation

The useradd command creates a new Linux user
Format:

useradd [options] LOGIN

-u UID: new UID
-g GID: New master group
-G GROUP1[,GROUP2,...[,GROUPN]]]: New additional group, the original additional group will be overwritten; If the original is retained, it shall be made at the same time
use-a option
-s SHELL: New default SHELL
-c 'COMMENT': New comment information
-d HOME: The new home directory will not be created automatically; To create a new home directory and move the original home data, use-m option
-l login_name: New name
-L: lock Designated user,stay/etc/shadow Addition of password bar !
-U: unlock Designated user,take /etc/shadow Password bar ! Take it off
-e YYYY-MM-DD: Indicates the expiration date of the user account
-f INACTIVE: Set an inactive period, i.e. a grace period

 

4.2 usermod user attribute modification

The usermod command can modify user properties
Format:

usermod [OPTION] login

Common options:

-u UID: new UID
-g GID: New master group
-G GROUP1[,GROUP2,...[,GROUPN]]]: New additional group, the original additional group will be overwritten; If the original is retained, it shall be made at the same time
 use-a option
-s SHELL: New default SHELL
-c 'COMMENT': New comment information
-d HOME: The new home directory will not be created automatically; To create a new home directory and move the original home data, use-m option
-l login_name: New name
-L: lock Designated user,stay/etc/shadow Addition of password bar !
-U: unlock Designated user,take /etc/shadow Password bar ! Take it off
-e YYYY-MM-DD: Indicates the expiration date of the user account
-f INACTIVE: Set an inactive period, i.e. a grace period

 

 

4.3 userdel delete user

userdel to delete a Linux user
Format:

userdel [OPTION]... Login

 

Common options:

-f, --force force
-r, --remove Delete user home directory and mailbox



 

4.4 groupadd create group

Format:

groupadd [OPTION]... group_name

Common options:

-g GID to specify GID number;[GID_MIN, GID_MAX]
-r Create system groups, CentOS 6 before: ID<500,CentOS 7 in the future: ID<1000

 

 

4.5 groupmod group attribute modification

Format:

groupmod [OPTION]... group

Common options:

-n group_name: New name
-g GID: new GID

 

 

 

4.6 groupdel delete group

Format:

groupdel [options] GROUP

Common options:

-f, --force Force deletion, even if it is the user's primary group,However, users without a primary group will be unavailable and unable to log in

 

 

4.7 viewing user related ID information

The id command can view the user's UID, GID and other information

 

id [OPTION]... [USER]

 

Common options

-u: display UID
-g: display GID
-G: Displays the name of the group to which the user belongs ID
-n: Display name, to be matched ugG use

 

 

 

4.8 switching users or executing commands as other users

su: that is, switch user. The command can switch the user identity and execute the command as the specified user

Format:

su [options...] [-] [user [args...]]

Common options:

-l --login su -l UserName amount to su - UserName
-c, --command <command> pass a single command to the shell with -c

 

 

4.9 setting password

passwd can change the user password
Format:

passwd [OPTIONS] UserName

Common options:

-d: Delete specified user password
-l: Lock the specified user
-u: Unlock specified user
-e: Force the user to change the password at the next login
-f: Force operation
-n mindays: Specify minimum service life
-x maxdays: Maximum service life
-w warndays: How many days in advance
-i inactivedays: Inactive period
--stdin: Receive user password from standard input,Ubuntu No such option

 

 

 

4.10. Modify user password policy

chage can modify the user password policy
Format:

chage [OPTION]... LOGIN

Common options:

-d LAST_DAY #Time to change password
-m --mindays MIN_DAYS
-M --maxdays MAX_DAYS
-W --warndays WARN_DAYS
-I --inactive INACTIVE #Grace period after password expiration
-E --expiredate EXPIRE_DATE #Validity period of the user
-l Show password policy

 

 

4.11 temporary switching of main group
The newgrp command can temporarily switch the primary group. If the user does not belong to this group, the group password is required
Format:

newgrp [-] [group]

 

 

4.12 changing and viewing group members
Group MEMS can manage the membership of additional groups
format

groupmems [options] [action]

Common options

-g, --group groupname #Change to the specified group (root only)
-a, --add username #Specify the user to join the group
-d, --delete username #Remove user from group
-p, --purge #Clear all members from group
-l, --list #Displays a list of group members

 

 

 

4.13} groups to view user group relationships

format

#View the list of groups to which the user belongs
groups [OPTION].[USERNAME]...

 

 

 

(1) . create a group distro whose GID is 2019;

 

1 [root@centos8 ~]#groupadd -g 2019  distro

 

(2) Create the user mandriva, whose ID number is 1005; The basic group was distro;

 

1 [root@centos8 ~]#useradd -u 1005 -g distro mandriva
2 useradd: user 'mandriva' already exists
3 [root@centos8 ~]#getent passwd mandriva
4 mandriva:x:1001:1001::/home/mandriva:/bin/bash

 

(3) Create user mageia with ID number of 1100 and home directory of / home/linux;

 

1 [root@centos8 ~]#useradd -u 1100 -d /home/linux mageia
2 [root@centos8 ~]#getent passwd mageia
3 mageia:x:1100:1100::/home/linux:/bin/bash

 

(4) Add a password to user mageia, the password is mageedu, and set the user password to expire in 7 days

 

1 [root@centos8 ~]#passwd mageia
2 Changing password for user mageia.
3 New password: 
4 BAD PASSWORD: The password is shorter than 8 characters
5 Retype new password: 
6 passwd: all authentication tokens updated successfully.
7 [root@centos8 ~]#chage -E 7 mageia

 

(5) Delete mandriva, but keep its home directory;

 

1 [root@centos8 ~]#userdel mandriva
2 [root@centos8 ~]#id mandriva
3 id: 'mandriva': no such user
4 [root@centos8 ~]#getent passwd mandriva
5 [root@centos8 ~]#ll /home
6 total 4
7 drwx------. 16 brucelebron brucelebron 4096 Jan 10 09:53 brucelebron
8 drwx------   3 mageia      mageia        78 Jan 13 21:06 linux
9 drwx------   3        1001        1001   78 Jan 13 20:40 mandriva

 

(6) . create the user slackware, whose ID number is 2002, the basic group is distro, and the additional group is peguin;

 

1 [root@centos8 ~]#groupadd peguin
2 [root@centos8 ~]#useradd -u 2002 -g distro -G peguin slackware
3 [root@centos8 ~]#id slackware
4 uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin)

 

(7) . modify the default shell of slackware to / bin/tcsh;

 

1 [root@centos8 ~]#usermod -s /bin/tcsh slackware
2 [root@centos8 ~]#getent passwd slackware 
3 slackware:x:2002:2019::/home/slackware:/bin/tcsh

 

(8) . add an additional group admins for user slackware and set it not to log in.

 

1 [root@centos8 ~]#usermod -a -G admins slackware 
2 [root@centos8 ~]#usermod -s /sbin/nologin slackware 
3 [root@centos8 ~]#su slackware
4 This account is currently not available.

5. Create users user1, user2, user3. Create the directory test under / data /

 

1 [root@centos8 ~]#mkdir -p /data/test
2 [root@centos8 ~]#useradd user1;useradd user2;useradd user3;
3 useradd: user 'user1' already exists
4 useradd: user 'user2' already exists
5 useradd: user 'user3' already exists

(1) The directory / data/test belongs to user1

 

1 [root@centos8 data]#chown user1:user1 /data/test
2 [root@centos8 data]#ll
3 total 0
4 drwxr-xr-x 2 user1 user1 6 Jan 13 21:49 test

(2) . user2 has read and write permission to the file when the directory's owner and group remain unchanged

1 [user2@centos8 data]$echo hello >> a.txt
2 bash: a.txt: Permission denied
3 [user2@centos8 data]$exit
4 exit
5 [root@centos8 data]#setfacl -m u:user2:rw a.txt
6 [root@centos8 data]#su - user2 -c "echo hello >> a.txt
7 > hello

(3) User1 creates a file A1 in the / data/test directory sh, a2. sh, a3.sh, a4.sh, set that all users cannot be deleted 1 sh,2.sh file. All users except user1 and root cannot delete A3 sh, a4. sh

 1 [user1@centos8 root]$cd /data/test
 2 [user1@centos8 test]$touch a{1..4}.sh
 3 [user1@centos8 test]$ll
 4 total 0
 5 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a1.sh
 6 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a2.sh
 7 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a3.sh
 8 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a4.sh
 9 [user1@centos8 test]$chattr +i a1.sh a2.sh
10 chattr: Operation not permitted while setting flags on a1.sh
11 chattr: Operation not permitted while setting flags on a2.sh
12 [user1@centos8 test]$chmod o+x a3.sh a4.sh
13 [user1@centos8 test]$chmod o+t a3.sh a4.sh

(4) . user3 adds an additional group user1. At the same time, user1 is required not to access the / data/test directory and all files under it

 

 1 [root@centos8 ~]#usermod user3 -a -G user1
 2 [root@centos8 ~]#id user3
 3 uid=2005(user3) gid=2005(user3) groups=2005(user3),2003(user1)
 4 [root@centos8 ~]#setfacl -m u:user:- /data/test
 5 setfacl: Option -m: Invalid argument near character 3
 6 [root@centos8 ~]#setfacl -m u:user1:- /data/test
 7 [root@centos8 ~]#getfacl /data/test/
 8 getfacl: Removing leading '/' from absolute path names
 9 # file: data/test/
10 # owner: user1
11 # group: user1
12 user::rwx
13 user:user1:---
14 group::r-x
15 mask::r-x
16 other::r-x

 

 

(5) . clear acl permissions of / data/test directory and all files under it

 

1 [root@centos8 ~]#setfacl -b /data/test/
2 [root@centos8 ~]#getfacl /data/test/
3 getfacl: Removing leading '/' from absolute path names
4 # file: data/test/
5 # owner: user1
6 # group: user1
7 user::rwx
8 group::r-x
9 other::r-x

 

Added by eflopez on Thu, 13 Jan 2022 17:52:59 +0200