Linux learning notes -- 7

Write shell

while conditional loop statement

while is often used in uncertain execution times. Syntax format:

For example: guess numbers at random

[root@linuxprobe ~]# vim guess.sh
[root@linuxprobe ~]# cat guess.sh
#!/bin/bash
NUM=$(expr $RANDOM % 1000)
TIMES=0
echo "this is number 0~999,you try guess?"
while true
do
	read -p "enter you numer:" N
	let TIMES++
	if [ $N -eq $NUM ] ; then 
		echo "very good,you right,the number is $NUM"
		echo "you times to $TIMES"
		exit
	elif
		[ $N -gt $NUM ] ; then
		echo "too high!"
	else
		echo "too low!"
	fi
done 

[root@linuxprobe ~]# 

expr is to get the execution result of the command, and exit is to exit from the while statement, because the while is looping all the time.
let TIMES + + is equivalent to TIMS=`expr $TIMES + 1`

case conditional test statement

Case statement is to match data in multiple ranges. If the matching is successful, execute relevant commands and end the whole condition test; If the data is not within the listed range, the default command defined in the asterisk (*) will be executed, and the terminator is actually the reverse writing esac of case. Syntax structure:

For example:

[root@linuxprobe ~]# vim checkkeys.sh
[root@linuxprobe ~]# cat checkkeys.sh
#!/bin/bash
read -p "Please enter a character and press Enter Key confirmation:" KEY
case "$KEY" in
        [a-Z])
                echo "You entered a letter."
                ;;
        [0-9])
                echo "You entered a number."
                ;;
        *)
                echo "You have entered spaces, function keys, or other control characters."
esac
[root@linuxprobe ~]# bash checkkeys.sh 
Please enter a character and press Enter Key confirmation: a
 You entered a letter.
[root@linuxprobe ~]# bash checkkeys.sh 
Please enter a character and press Enter Key confirmation: G
 You entered a letter.
[root@linuxprobe ~]# bash checkkeys.sh 
Please enter a character and press Enter Key confirmation: 9
 You entered a number.
[root@linuxprobe ~]# bash checkkeys.sh 
Please enter a character and press Enter Key confirmation:?/
You have entered spaces, function keys, or other control characters.
[root@linuxprobe ~]# 

Plan task

Divided into one-off and permanent (periodic)
At plans the task at once and executes it only once
crond is periodic, time regular, and automatically executed many times

at command parameters and functions


For example:

[root@linuxprobe ~]# date
Sun Apr 17 12:52:40 CST 2022
[root@linuxprobe ~]# at 12:55
warning: commands will be executed using /bin/sh
at> reboot
at> <EOT>
job 1 at Sun Apr 17 12:55:00 2022
[root@linuxprobe ~]# at -l
1	Sun Apr 17 12:55:00 2022 a root
[root@linuxprobe ~]# atrm 1
[root@linuxprobe ~]# at -l
[root@linuxprobe ~]# 
[root@linuxprobe ~]# echo "systemctl restart httpd" | at 12:56
warning: commands will be executed using /bin/sh
job 2 at Sun Apr 17 12:56:00 2022
[root@linuxprobe ~]# at -l
2	Sun Apr 17 12:56:00 2022 a root
[root@linuxprobe ~]# atrm 2
[root@linuxprobe ~]# at -l
[root@linuxprobe ~]# 

atrm is to delete a task, and the following number is the sequence number of the task.
Instead of a fixed time, it is generally operated in the way of "at now +2 MINUTE", which means that the task will be executed after 2 minutes. It can also be replaced by words such as HOUR, DAY, MONTH and so on

root@linuxprobe ~]# at now +2 MINUTE
warning: commands will be executed using /bin/sh
at> systemctl restart httpd

Use ctrl+ d in at to end editing

crontab command parameters and functions


"Order of minute, hour, day, month and week". This is to use the crond service to set the parameter format of the task. If some fields are not set, you need to use an asterisk () for placeholders


The "minute" field in the planned task must have a numeric value, which must not be empty or a sign. The "day" and "week" fields cannot be used at the same time, otherwise there will be a conflict.
The commands to be executed need to use absolute paths, which can be viewed through the which command
For example:

[root@linuxprobe ~]# which reboot
/usr/sbin/reboot
[root@linuxprobe ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@linuxprobe ~]# crontab -l
0 2 1 3,6,9,12 * /usr/sbin/reboot
[root@linuxprobe ~]# 

It should be noted that in addition to using commas (,) to represent multiple time periods respectively, for example, "8,9,12" represents August, September and December. A minus sign (-) can also be used to represent a continuous time period (for example, if the value of the field "day" is "12-15", it means the 12th ~ 15th day of each month). You can also use a division sign (/) to indicate the interval between tasks (for example, "* / 2" means to execute tasks every 2 minutes)
Of course, you don't need the crontab command to edit. You can also use vim /etc/crontab. If there is an error, there will be no prompt. If there is an error in crontab, there will be a prompt.

User identity and capabilities

In RHEL 8 system, the user identities are:
Administrator UID 0: administrator user of the system.
The system user UID is 1 ~ 999: in order to avoid being lifted to the whole server by hackers due to the vulnerability of a service program, the default service program will be run by independent system users, so as to effectively control the scope of damage.
Ordinary user UID starts from 1000: it is a user created by the administrator for daily work.
Note: if UID=888, can you judge whether it is the system user. Not necessarily. such as

[root@linuxprobe ~]# useradd -u 888 test
[root@linuxprobe ~]# id test
uid=888(test) gid=1001(test) groups=1001(test)

uid user identity, gid group, gorups extension group
The group is automatically generated when the user creates a new one, and it is also an extension group by default. There can be more than one extension group. For example:

[root@linuxprobe ~]# usermod -G linuxprobe test
[root@linuxprobe ~]# id test
uid=888(test) gid=1001(test) groups=1001(test),1000(linuxprobe)
[root@linuxprobe ~]# 

id command

The id command is used to display user details. The syntax format is "id user name"

[root@linuxprobe ~]# id linuxprobe
uid=1000(linuxprobe) gid=1000(linuxprobe) groups=1000(linuxprobe)

useradd command

The useradd command is used to create a new user account. The syntax format is "useradd [parameter] user name"
When this command creates a user account, the default user home directory will be stored in the / home directory
Parameters and functions in useradd command

For example, you can specify the home directory and Uid, and you cannot log in to the system

[root@linuxprobe ~]# useradd -d /home/linux -u 8888 -s /sbin/nologin linuxdown
[root@linuxprobe ~]# id linuxdown
uid=8888(linuxdown) gid=8888(linuxdown) groups=8888(linuxdown)

groupadd command

The groupadd command is used to create a new user group. The syntax format is "groupadd [parameter] group name"

[root@linuxprobe ~]# groupadd ronny

usermod command

The usermod command is used to modify the user's attributes. The syntax format is "usermod [parameter] user name"
Parameters and functions in usermod command

For example:

[root@linuxprobe ~]# id linuxprobe
uid=1000(linuxprobe) gid=1000(linuxprobe) groups=1000(linuxprobe)
[root@linuxprobe ~]# usermod -u 8888 linuxprobe
[root@linuxprobe ~]# id linuxprobe
uid=8888(linuxprobe) gid=1000(linuxprobe) groups=1000(linuxprobe),0(root)
[root@linuxprobe ~]# usermod -s /sbin/nologin linuxprobe
[root@linuxprobe ~]# su - linuxprobe
This account is currently not available.

passwd command

The passwd command is used to modify the user's password, expiration time and other information. The syntax format is "passwd [parameter] user name"
Parameters and functions in passwd command

[root@linuxprobe ~]# passwd test
Changing password for user test.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@linuxprobe ~]# 

Look at locking usermod and passwd

[root@linuxprobe ~]# passwd -S sony
sony PS 2022-04-17 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@linuxprobe ~]# usermod -L sony
[root@linuxprobe ~]# passwd -S sony
sony LK 2022-04-17 0 99999 7 -1 (Password locked.)


[root@linuxprobe ~]# passwd -l test
Locking password for user test.
passwd: Success
[root@linuxprobe ~]# passwd -S test
test LK 2022-04-17 0 99999 7 -1 (Password locked.)
[root@linuxprobe ~]# usermod -U test
[root@linuxprobe ~]# passwd -S test
test PS 2022-04-17 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@linuxprobe ~]# 

userdel command

The userdel command is used to delete an existing user account. The syntax format is "userdel [parameter] user name"
Parameters and functions in userdel command

[root@linuxprobe home]# ls
linuxprobe  sony  test  workdir
[root@linuxprobe home]# userdel sony
[root@linuxprobe home]# ls
linuxprobe  sony  test  workdir
[root@linuxprobe home]# userdel -r test
[root@linuxprobe home]# ls
linuxprobe  sony  workdir
[root@linuxprobe home]# 

Add users manually

[root@linuxprobe home]# id redhat
id: 'redhat': no such user

#Add user's last line redhat
[root@linuxprobe ~]# vim /etc/passwd
[root@linuxprobe ~]# tail -n 3 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
redhat:x:6666:6666:redhat:/home/redaht:/bin/bash
[root@linuxprobe ~]# id redhat
uid=6666(redhat) gid=6666 groups=6666

# Add group redhat
[root@linuxprobe ~]# vim /etc/group
[root@linuxprobe ~]# tail -n 3 /etc/group
tcpdump:x:72:
linuxprobe:x:1000:
redhat:x:6666:

# Create a home directory and authorize owners and groups
[root@linuxprobe ~]# mkdir -p /home/redhat
[root@linuxprobe ~]# chown -Rf redhat:redhat /home/redhat

# Set password
[root@linuxprobe ~]# passwd redhat
Changing password for user redhat.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

# Switch to redhat user
[root@linuxprobe ~]# id redhat
uid=6666(redhat) gid=6666(redhat) groups=6666(redhat)
[root@linuxprobe ~]# su - redhat
[redhat@linuxprobe ~]$ pwd
/home/redhat
[redhat@linuxprobe ~]$ 

Photo witness

Tags: Linux Operating System

Posted by sandthipe on Sun, 17 Apr 2022 14:32:04 +0930