Linux system basic optimization and common commands

Time:2019-11-6

Linux basic system optimization
There is no introduction, only one picture.

The network function of Linux is quite powerful. For a while, we can’t understand all the network commands. When we configure the server’s basic environment, we should first understand the network parameter setting commands.
Ifconfig query, setting network card, IP and other parameters
Ifup, ifdown script command, a simpler way to start and shut down the network
IP complies with the instruction and directly modifies the above functions
When we first install Linux, we need to use xshell for remote connection, so we need to get the IP address. Sometimes the network card is not started by default, and Linux can’t get the IP address, so we have to start the network card manually

Edit network card profile

vim /etc/sysconfig/network-scripts/ifcfg-eth0

Modify configuration parameters

ONBOOT=yes
Details of network card configuration file
as follows

Network profile:
/etc/sysconfig/network

Network interface profile:
/etc/sysconfig/network-scripts/ifcfg-INTERFACE_NAME
Device =: the associated device name, which should be consistent with the “interface” in the second half of the file name;
Bootproto = {static | none | DHCP | BOOTP}: boot protocol; to use static address, use static or none; DHCP means to use DHCP server to obtain address;
IPADDR =: IP address
Netmask =: subnet mask
Gateway =: set the default gateway;
Onboot =: whether to automatically activate this network interface when starting;
Hwaddr =: hardware address, which should be consistent with the address in the hardware; can be saved;
Userctl = {yes| no}: whether ordinary users are allowed to control this interface;
Peerdns = {yes| no}: whether to accept the DNS address specified by the DHCP server when bootproto is DHCP;

Ifup, ifdown command

Start / close a network card
ifup eth0
ifdown eth0


What happens to xshell if you close the network card?

Ifconfig command
Ifconfig view IP address of network card

Enter ifconfig directly to list the enabled network cards, or enter ifconfig eth0 to display eth0 information separately
The options are explained as follows:
Code of eth0 network card
Loopback address
IP address of INET IPv4
Netmask subnet mask
Broadcast address
Rx / TX traffic TX is transport, Rx is receive
Number of packets
Errors number of packet errors
Number of dropped packets with problems
Collisions packet collision, too many values represent poor network condition

Ifup, ifdown command
Ifup and ifdown are directly connected to the / etc / sysconfig / network scripts directory to search for corresponding network card files, such as ifcfg-eth0, and then set them
IP command
IP is a command, not TCP / IP. This IP command combines the functions of ifconfig and route.
IP addr show – view IP information
Learn how to view network card information, and then view system information.
What is the version of your system?

View system version information

cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

View kernel version number

uname -r
3.10.0-693.el7.x86_64

Check how many bits of the system

uname -m
x86_64

View all kernel information

uname -a

User management and file permission
Modern operating system generally belongs to multi-user operating system, that is to say, the same machine can establish accounts for multiple users, generally these users are ordinary users, these ordinary users can log in to this computer at the same time, and the computer allocates certain resources to these users.
Ordinary users perform their own operations within the allocated resources, and are not affected by each other. However, the permissions of these ordinary users are limited, and if there are too many users, the management is inconvenient, so root user is introduced.
This user is unique and has all permissions of the system. The group the root user is in is called the root group. A group is a collection of multiple users with similar permissions.
Root’s rights
The characteristic of Linux system is that it can satisfy multiple users and work at the same time, so Linux system must have good security.
The root administrator password set when rhel7 is installed. This root administrator is the super user in all UNIX systems. It has the highest system ownership and can manage various functions of the system, such as adding / removing users, starting / closing processes, turning on / disabling hardware devices, etc.
Therefore, “the greater the ability, the greater the responsibility”, the root authority must be well mastered, otherwise a wrong command may destroy the whole system.
Why is root called root?
In Linux system, users also have their own uid identity account and are unique
System administrator uid is 0
The service programs installed with the system user uid of 1 ~ 999 Linux will create unique users to run.
Normal user uid starts from 1000: created by administrator
User group GID
In order to facilitate the management of users belonging to the same group, the concept of user group is also introduced in Linux system. By using the GID (group identification), we can add multiple users to the same group, so that we can plan permissions or assign tasks to users in the group.
Suppose there are multiple departments in a company, and there are many employees in each department. If you only want employees to access resources within your department, you can set permissions for departments rather than specific employees.
For example, you can set permissions on the technology department so that only the employees of the technology department can access the database information of the company.
When a Linux administrator creates a user, he or she will automatically create a user group with the same name. This user group has only one user
Everyone knows that there are administrator users in Windows
such
Or so
Linux / Unix is a multi-user, multi task operating system.
Root: by default, it has the highest administrative authority in UNIX / Linux operating system. It can be understood as the group leader of QQ group

Ordinary user: created by an administrator or a user with administrative authority. It can only be read or viewed, and cannot be added, deleted or modified.
Create a normal user

Add user

useradd oldboy

Set password

passwd redhat

The root user can change the password of everyone else without authentication

Switching users
Su command can switch the user’s identity requirements,
su – username

The – sign in the middle of the Su command is very important, which means that the user is completely switched to the new user, that is, the environment variable information is also changed to the new user’s information

First look at the current user (who I am)

whoami

Switching users

su – oldboy

Exit user login

logout
ctrl + d

In general, avoid using root directly in the production environment. Unless there is a special system maintenance requirement, it will be returned to ordinary users immediately after use
Non interactive set password (echo “RedHat” | passwd — stdin Oldboy & & History – C)
Tip:
1. Super user root switches ordinary users without password, such as “group leader” who wants to kick
2. For normal users to switch root, you need to enter a password
3. Ordinary users have small permissions and can only view basic information
4. The $symbol is the common user command prompt, and the ා is the super administrator prompt
Root is the current user, oldbyedu is the host name, ~ represents the current path, and is also the home directory
Groupadd command
The group command is used to create user groups. In order to assign the permissions of each user in the system more efficiently, several users are often added to a group in the work, so that permissions can be arranged for a class of users.
For example, super brother used to be in charge of adding openldap user management in the company. Occasionally, colleagues from Taiwan and the United States go to Shanghai for cooperation, and I have to add them to the IT department group, so that they have permission to operate the server.
groupadd it_dep
Userdel delete user
-F force user deletion
-R colleagues delete users and home directories
userdel -r pyyu
Sudo command
The sudo command is used to execute commands as other identities, and the default identity is root. The user who can execute sudo instructions is set in / etc / sudoers. If an unauthorized user attempts to use sudo, a warning email will be sent to the administrator. When using sudo, the user must first enter the password, and then there is a 5-minute period of validity. If the period is exceeded, the user must re-enter the password.
grammar

Sudo [options] [parameters]
-B: execute instructions in the background;
-H: display help;
-H: set home environment variable as home environment variable of new identity;
-K: end the validity period of the password, that is, you need to enter the password the next time you execute sudo;.
-L: list the instructions that can and cannot be executed by the current user;
-P: change the prompt symbol of the inquiry password;
-s: execute the specified shell;
-U: use the specified user as the new identity. If this parameter is not added, the default is root;
-V: extend the password validity for 5 minutes;
-V: display version information.

Example
When is this sudo command used?
Look at the picture

What should I do? The permission is not enough. At this time, sudo LS / root is required to run as root. The rights of Chaoge are small, and root is OK!!

This is because the configuration of sudo must edit the / etc / sudoers file, and only root can modify it. We can edit sudoers file directly through the command of visudo. Using this command, we can check the syntax, which is safer than directly editing VIM / etc / sudoers

Visudo edit sudoers file

Write in

Allow root to run any commands anywhere

root ALL=(ALL) ALL
Chaoge all = (all) all ා allow Chaoge to execute any command anywhere

At this time, switch the Chaoge user

The Su command is used to switch the current user identity to another user identity. When changing, you must enter the user account and password to be changed.

su – chaoge
Sudo LS / root command is available
File and directory permissions
The purpose of Linux permissions is to (protect account information)
Linux permissions are mainly determined by three identities:
User / owner file user, which user does the file belong to
Which group does the group belong to
Other is neither user nor group. It’s other
What is authority
In Linux, every file has its own owner, and all groups, and stipulates the file owner, all groups and other people’s rights to the file, such as read, write, and execute.

For directory permissions, read is to read the directory file list, and writable is to add, modify, and delete files in the directory. Executable means can enter the directory
The observation of Linux permission
Use a command to view permissions
ls -l /var/log/mysqld.log

Read the picture above:
Permission. The first letter is the file type, and the next nine letters are the permission of three identities in three groups
Number of file links
Owner owner owner
File ownership group
file size
Date and time of last modification
file name
Let’s first analyze the types of files

  • General documents
    D folder
    L soft connection (shortcut)
    B-block device, mainly storing media files
    C for keyboard, mouse and other devices
    file right
    R read is readable and can be viewed with cat and other commands
    W write write. You can edit or delete this file
    X executable can execute
    directory right
    Do not use root to test here!!!! Root is awesome
    Please use ordinary user to execute!!!!! Test file and folder permission operation, please use ordinary user!
    R can LS list all files for this directory
    W can create files in this directory
    X can CD into this directory, or view the details

Authority and digital transformation

ls -l /var/log/mysqld.log
-Rw-r — R — 1 MySQL 6735642 August 11 14:19 / var / log / mysqld.log
This means that the owner of mysqld.log file is mysql, and the group is mysql. Only MySQL users can read and write this file, and others can only read this file.

View user rights command
ID command to view the user’s group owner
[[email protected]_python ~ 16:34:52]#id root
Uid = 0 (root) GID = 0 (root) group = 0 (root)
Modify file permission properties
Ordinary users can only modify their own file name, time and permission (note)
Therefore, you can only use the most NB root user to modify other user permissions

Switch root

[[email protected]_python root]$ su –
The current / TMP / pyyu.txt file exists, and the information is
-Rw-rw-r — 1 pyyu pyyu 0 August 11 16:41 pyyu.txt
Change the owner to root
chown
[[email protected]_python /tmp 16:43:12]#chown root pyyu.txt
see information
[[email protected]_python /tmp 16:43:42]#ll pyyu.txt
-Rw-rw-r — 1 root pyyu 0 August 11 16:41 pyyu.txt
Modify group
chgrp
[[email protected]_python /tmp 16:43:42]#ll pyyu.txt
-Rw-rw-r — 1 root pyyu 0 August 11 16:41 pyyu.txt
[[email protected]_python /tmp 16:44:59]#chgrp root pyyu.txt
[[email protected]_python /tmp 16:45:51]#ll pyyu.txt
-Rw-rw-r — 1 root 0 August 11 16:41 pyyu.txt
file right
We have known three kinds of identity permissions (owner, group and others), each of which has three kinds of permissions of RWX, and the system also provides digital computing permissions.
r read 4
w write 2
x execute 1
The lowest score for each identity is 0, and the highest is R + W + x 7
So there are three identities, the highest authority is 777 and the lowest is 000
-Rw-rw-r — 1 root 0 August 11 16:41 pyyu.txt
So the permissions of pyyu.txt are
The dominant species is 6 R + W (4 + 2)
The genus group is 6 R + W (4 + 2)
Others are 4 R (4)
Commands for modifying permissions
chmod
Chmod [identity] [parameter] [file]
U (user) + (add)
G (Group) – (minus)
O (other) = (assignment)
    a(all)
for example

Current permissions
-Rw-rw-r — 1 root 0 August 11 16:41 pyyu.txt
Method 1
Subtract the owner’s write permission
chmod u-w pyyu.txt
View permission
-R — rw-r — 1 root 0 August 11 16:41 pyyu.txt

Method 2
Owner add read-write executable permission
chmod 700 pyyu.txt

Owner readable, writable and executable
Group readable and executable
Others readable and executable
chmod 755 pyyu.txt

Modify file name, modify file change date
mv pyyu.txt chaoge.txt

Touch, modify time

touch chaoge.txt
Soft connection
Soft links are also called symbolic links, similar to windows shortcuts.
Shortcut configuration commonly used to install software, such as python, nginx, etc

Ln-S target file soft connection name

1. The file / TMP / test.txt exists
[[email protected] tmp]# ls -l
-Rw-r — R — 1 root October 15 21:23 test.txt

2. Establish a soft connection in the / home directory, and point to the / TMP / test.txt file
ln -s /tmp/test.txt my_test

3. View soft connection information
Lrwxrwxrwx 1 root 13 October 15 21:35 my_test – > / TMP / test.txt

4. View files through soft connection
cat my_test

My test is only an alias of / TMP / test.txt, so deleting my test will not affect / TMP / test.txt, but delete the original,
Shortcuts don’t make sense. They don’t exist

PS1 variable
The Linux command prompt is controlled by the PS1 environment variable

[[email protected]_python ~]# echo $PS1
[\[email protected]\h \W]$

You can adjust the global variable / etc / profile file by yourself for permanent effect PS1 = ‘[\ u @ \ h \ w \ t] $’
\D date
\H full host name
\H hostname first name
\T time 24 hour hhmmss
\T time 12 hour system
\A time 24 hour HHMM
\U current user account name
\Version of V Bash
\W full working directory
\W use basename to get working directory name
#Order number one
$prompt character, root = x, normal user = x$
PS1 > variable name
$PS1 > View variable content
PS1 = new content reassignment

Variable assignment, viewing
name=’chaoge’
echo $name

PS1 shows IP address
export PS1=”[\[email protected]\h /sbin/ifconfig ens33 | sed -nr 's/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' \w]$”

Tar unzip command
People have invented all kinds of bags, whether it’s backpacks or bags, to make “documents” more convenient to carry. The most famous file packing tool of Linux is tar.

Tar command: used to compress and decompress files. Tar itself does not have compression. It is implemented by calling compression function
grammar

Tar (option) (parameter)
-A or — catalate: add a file to the existing backup file;
-B: set block size;
-C or — create: create a new backup file;
-C: this option is used for decompression. To decompress in a specific directory, you can use this option.
-D: difference of record documents;
-X or — extract or — get: restore files from backup files;
-T or — List: list the contents of backup files;
-Z or — gzip or — ungzip: process the backup file through the gzip instruction;
-Z or — compress or — uncompress: process the backup file through the compress command;
-F or — file =: Specifies the backup file;
-V or — verbose: display the instruction execution process;
-R: add files to the compressed files;
-U: add changed and existing files to existing compressed files;
-J: support bzip2 to extract files;
-V: display operation process;
-L: file system boundary setting;
-K: keep the original file without overwriting;
-M: the reserved file is not overwritten;
-W: confirm the correctness of the compressed file;
-P or — same permissions: restore the file with the original file permissions;
-P or — absolute names: absolute names are used for file names, and the “/” sign before the file name is not removed;
-N or — newer =: only the files updated on the specified date are saved in the backup file;
–Exclude =: exclude files that conform to the template style.

Example

Tar – zxvf python-3.7.0b3.tgz

Tar – czvf oldboy.txt.tar.gz oldboy.txt compress oldboy.txt

The above command is equal to the following two commands
tar -cvf oldboy.tar oldboy.txt
gzip oldboy.tar

Tar – CF all pic.tar *. JPG compress all JPG ending files in the current directory

Tar – XJF XX. Tar. Bz2 extract the file at the end of bz2

Gzip command
Gzip is used to compress files. It is a widely used compression program with “. GZ” extension
Gzip can compress large files and save disk space at a compression rate of 60% ~ 70%
grammar

-D or — decompress or — uncompress: decompress the compressed file;
-F or — force: forcibly compress the file.
-H or – help: online help;
-L or — List: list information about compressed files;
-L or — license: display version and copyright information;
-R or — recursive: recursive processing, processing all files and subdirectories under the specified directory together;
-V or — verbose: display the instruction execution process;

Example

Compress all files in the current directory as. GZ files
gzip *
Extract each compressed file in the above example and list the detailed information
gzip -dv *

Displays information about the compressed file and does not unzip it
gzip -l *
Compress a tar backup file with the extension tar.gz
tar -cf my.tar my_first.py
gzip -r my.tar

Netstat command
The netstat command is used to print the status information of the network system in Linux, and let you know the network situation of the whole Linux system.
Syntax options

Netstat [options]
-T or — TCP: display the connection status of TCP transmission protocol;
-U or — UDP: display the connection status of UDP transmission protocol;
-N or — numeric: use IP address directly instead of domain name server;
-L or — listening: displays the socket of the server in monitoring;
-P or — programs: displays the program ID and program name of the socket being used;
-A or — all: displays the sockets in all connections;

Example

[[email protected]_python ~ 10:21:59]#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 814/sshd
tcp6 0 0 :::111 :::* LISTEN 2703/rpcbind
tcp6 0 0 :::3306 :::* LISTEN 29269/mysqld
udp 0 0 0.0.0.0:758 0.0.0.0:* 2703/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 2703/rpcbind
udp 0 0 10.141.32.137:123 0.0.0.0:* 484/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 484/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 484/ntpd
udp6 0 0 :::758 :::* 2703/rpcbind
udp6 0 0 :::111 :::* 2703/rpcbind
udp6 0 0 :::123 :::* 484/ntpd

PS command
The PS command is used to view the process status in the system in the form of “PS [parameter]”.
Common parameters of PS command
-A show all processes
-U user and other details
-X shows the process without control terminal
Kill command
The kill command is used to delete an executing program or work. Kill to send the specified information to the program.
option
-A: when processing the current process, the correspondence between the command name and the process number is not limited;
-L: if no option is added, the – L parameter will list all information names;
-P: specify that the kill command only prints the process number of the relevant process without sending any signal;
-S: specify the information to be sent;
-U: specify the user.
Only the ninth signal (sigkill) can terminate the process unconditionally. Other signal processes have the right to ignore. The following are common signals:

HUP 1 terminal disconnection
Int 2 interrupt (same as Ctrl + C)
Quit 3 exit (same as Ctrl +)
Term 15 termination
Kill 9 forced termination
Cont 18 continue (as opposed to stop, FG / BG command)
Stop 19 pause (same as Ctrl + Z)

Example

First use PS to find the process, and then kill it with kill:
ps -ef | grep vim
root 3268 2884 0 16:21 pts/1 00:00:00 vim install.log
root 3370 2822 0 16:21 pts/0 00:00:00 grep vim

kill 3268

Kill Command
Generally speaking, the service program of complex software will have multiple processes cooperating to provide services for users. If these processes are ended one by one, it will be more troublesome. At this time, you can use the kill command to batch end all processes of a service program.
For example, there are two processes after nginx starts
killall nginx

SELinux features
SELinux (security enhanced Linux) is the implementation of mandatory access control by the National Security Administration (NSA). This function administrator loves and hates it. Most production environments are closed. Other methods are used for security.
Most SSH can’t connect to the virtual machine because the firewall and SELinux block it
Permanent closing mode:

1. Modify the configuration file to permanently shut down SELinux
CP / etc / SELinux / config / etc / SELinux / config.bak – backup before modification
2. The modification method can be edited by vim and found

This file controls the state of SELinux on the system.

SELINUX= can take one of these three values:

enforcing – SELinux security policy is enforced.

permissive – SELinux prints warnings instead of enforcing.

disabled – No SELinux policy is loaded.

SELINUX=disabled
3. Replace with sed
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’ /etc/selinux/config
4. Inspection status
grep “SELINUX=disabled” /etc/selinux/config

The result indicates that the modification is successful

Temporarily shut down SELinux (command line modification, restart invalid):

Getenforceget SELinux status

Modify SELinux status

setenforce
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
The number 0 indicates permission, gives warning, does not block, equivalent to disabled
The number 1 indicates forcing and on

Tip:
After modifying the SELinux configuration, if you want to take effect, you have to restart the system. The trick is (modify the configuration file + command line modification to take effect immediately)

The server of production environment is forbidden to restart at will!!!!
Iptables firewall
In the learning stage, it is more convenient to close the firewall. In the enterprise environment, generally only the Linux server configured with the Internet IP can open the firewall, but for the business server with high concurrent traffic, it is still unable to open, and there will be a great performance loss, so a more NB hardware firewall is needed.
The specific operations to close the firewall are as follows:

CentOS 7 already uses firewall by default
1. Turn off the firewall
Systemctl status firewalld view firewall status
Systemctl stop firewall
Systemctl disable firewall
Systemctl is enabled firewalld.service? Check whether the firewall is started

Linux Chinese display settings (to prevent Chinese code scrambling)
This optimization is optional. You can choose whether to adjust the character set of Linux system according to your personal situation. The character set is a set of text symbols and encoding.
Common character sets under Linux are:
GBK is rarely used in actual Enterprises
UTF-8 is widely supported, MySQL also uses UTF-8, and enterprises widely use it

View the current character set of the system

echo $LANG
locale

Check character set of xshell CRT

Command modify character set

vim /etc/profile.d/locale.sh

export LC_CTYPE=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8

vim /etc/locale.conf

LANG=zh_CN.UTF-8

vim /etc/sysconfig/i18n

LANG=zh_CN.UTF-8

vim /etc/environment

LANG=zh_CN.UTF-8
LC_ALL=zh_CN.UTF-8
English version

vim /etc/profile.d/locale.sh

export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

vim /etc/locale.conf

LANG=en_US.UTF-8

vim /etc/sysconfig/i18n

LANG=en_US.UTF-8

vim /etc/environment

LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8

2. View system language variables after change
locale

Core solutions to garbled code
1. System character set utf8
2. Xshell character set utf8
3. File character set consistency zh cn.utf-8
DF command
The DF command displays the available disk space on the partition. The default display unit is KB. You can use this command to get information about how much space the hard disk is occupied and how much space is left.

grammar
DF (option) (parameter)
-H or — human readable: display information in a highly readable way;
-K or — kilobytes: Specifies that the block size is 1024 bytes;
-T or — print type: displays the type of file system;
–Help: display help;
–Version: displays version information.

Example
View the system disk devices. The default value is KB
df
Use the – H option to display in kilobytes or more, with high readability:
df -h
Tree command
Tree parameter
Set host name
[[email protected] /tmp 11:04:42]#hostnamectl set-hostname pyyuc
[[email protected] ~ 11:05:12]#hostname
pyyuc

DNS
DNS (domain name system), as a distributed database of domain name and IP address mapping each other on the world wide web, enables users to access the Internet more conveniently without remembering the IP strings that can be read directly by the machine.
The process of getting the IP address corresponding to the domain name through the domain name is called domain name resolution (or host name resolution).
View DNS of Linux, unique configuration file
configuration file
cat /etc/resolv.conf

DNS server address

nameserver 119.29.29.29
nameserver 223.5.5.5
Local forced DNS parsing file / etc / hosts
Specify local resolution:
/etc/hosts
Host IP host name host alias
127.0.0.1 www.pyyuc.cn
NSLOOKUP command
NSLOOKUP command is a common domain name query tool, which is used to query DNS information.
Nslookup4 has two working modes, namely “interactive mode” and “non interactive mode”. In “interactive mode”, users can query the information of various hosts and domain names from the domain name server, or output the list of hosts in the domain name. In “non interactive mode”, users can only obtain specific names or required information for a host or domain name.
Enter the interactive mode, enter the NSLOOKUP command directly, and enter the interactive mode directly without any parameters. At this time, NSLOOKUP will connect to the default domain name server (i.e. the first DNS address of / etc / resolv.conf). Or enter NSLOOKUP – nameserver / IP. Enter the non interactive mode, and enter the NSLOOKUP domain name directly.

analysis

nslookup www.oldboyedu.com
Schedule task crond service

How do you get up every day? Some people have girlfriends, or boyfriends, while I am awakened by poverty,,,
What is a scheduled task:
Background operation, when the scheduled time will automatically execute the task, the premise is: manually set the scheduled task in advance. This uses the crond service

Crond service related packages
[[email protected] ~]# rpm -qa |grep cron
cronie-anacron-1.4.11-14.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
cronie-1.4.11-14.el7.x86_64
These packages are installed when the installation system is minimized, and will start the crond service automatically, and provide us with crontab commands for writing scheduled tasks.
Crontab command is used to submit and manage the periodic tasks of users, similar to the scheduled tasks under windows
grammar

Crontab (option) (parameter)
-E: edit the timer settings of the user;
-L: list the timer settings of the user;
-R: delete the timer settings of the user;
-U: Specifies the user name to set the timer.

Files for scheduled tasks
/var/spool/cron

Be careful:
1. View the execution of the scheduled task: Tail – F / var / log / Cron
2 when writing a scheduled task, the command must add an absolute path. Otherwise, this situation will occur: from the log, it does trigger the execution of the scheduled task, but the command does not succeed. For example, * * * * reboot will occur. You need to write reboot as / usr / SBIN / reboot

  1. Whether the command executed by the scheduled task exists, and whether the software is installed
  2. Ensure crontab service is running

systemctl status cron

ps -ef|grep crond

  1. Check whether crontab is started

systemctl is-enabled crond
Crontab profile

There is a crontab file in the / etc directory, which is the configuration file of system task scheduling
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

For details see man 4 crontabs

Example of job definition:

.—————- minute (0 – 59)

| .————- hour (0 – 23)

| | .———- day of month (1 – 31)

| | | .——- month (1 – 12) OR jan,feb,mar,apr …

| | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

| | | | |

* * * * * user-name command to be executed

Time sharing day month week

Basic format of crontab task configuration:

  •         command
    Minute (0-59) hour (0-23) date (1-31) month (1-12) week (0-6,0 for Sunday) command

The first column represents minutes 1-59 per minuteperhaps/1 representation
The second column represents hours 1-23 (0 represents zero point)
The third column represents the date 1-31
The fourth column represents the months from January to December
The fifth column identification number is from 0 to 6 (0 means Sunday)
Column 6 commands to run
Asterisk (): represents all possible values. For example, if the month field is an asterisk, it means that the command operation is executed every month after the constraints of other fields are met.
30 08
** go to work at 8.30 every day
Comma (,): you can specify a list range with comma separated values, for example, “1,2,5,7,8,9”

Middle bar (-): the middle bar between integers can be used to represent an integer range, for example, “2-6” means “2,3,4,5,6”

Forward slash (/): you can use forward slash to specify the interval frequency of time, for example, “0-23 / 2” means every two hours. At the same time, the forward slash can be used with the asterisk, for example/10, if used in the minute field, it means every ten minutes.
/3 * * / usr / SBIN / ntpdate ntp1.aliyun.com perform time synchronization every three minutes

Go to work at 8.30 every day
30 08 * * go to work
Go home from work at 12 every day
00 00 * * go home to bed
Example

All commands must be written in absolute path!

Execute command every minute

          • command

            Execute commands in groups of 3,15 per hour

            3,15 * * command

            At the 3rd and 15th minute of 8-11am

            3,15 8-11 * * command

            Execute orders at 21:30 every night

            30 21 * * command

            No orders at 1:30 on Saturday or Sunday

            30 1 * 6,0 command

            Clear all files in the / tmp directory from 1:00 a.m. to 1:00 a.m. every Monday to Friday

            0 1 * * 1-5 /usr/bin/rm -rf /tmp/*

            Restart nginx at 21:30 every night

            30 21 * * * /opt/nginx/sbin/nginx -s reload

            Restart nginx at 4:45 on the 1st, 10th and 22nd of each month

            45 4 1,1,10,22 * * /opt/nginx/sbin/nginx -s reload

            Every Monday from 8 a.m. to 11 a.m. in the 3rd and 15th minutes

            3,15 8-11 * * 1 command

Package management
What is a package

Program (software) components:
Binary executable command
Library.so file
Configuration file.conf
Help file readme / usr / share / man

Windows program
Pictured

MacOS program

As the name implies, software package is the product of packaging applications, configuration files and data. All Linux distributions adopt a certain form of software package system, which makes linux software management as convenient as in windows. SUSE, red hat, Fedora and other distributions use RPM package, Debian and Ubuntu use. DEB format software package.

mysql-5-3-4.rpm
redis-3-4-3.rpm
nginx2-3-2.rpm

In the early system operation and maintenance, installing software is a very laborious thing. The system administrator has to download the source code compiling software and make various adjustments for the system.
Although the software in the form of source code compilation enhances the freedom of user customization, it is inefficient to spend energy on small software, so the software package came into being.
Package management frees administrators from endless compatibility issues. The yum tool can automatically search for dependencies and perform the installation.
RPM package manager
Package manager core functions
1. Make software package.rpm
2. Install, uninstall, upgrade, query and verify
Before RPM (red hat package manager) was announced, to install software in Linux system, you can only install it in the way of source package. It is very difficult and patient to install the program in the early Linux system, and most of the service programs only provide the source code, which requires the operation and maintenance personnel to compile the code by themselves and solve many software dependencies, so to install a good service program, the operation and maintenance personnel need to have rich knowledge, superb skills, and even good patience. In addition, when installing, upgrading and uninstalling service programs, the dependency of other programs and libraries should also be considered, so it is very difficult to verify, install, uninstall, query, upgrade and other management software operations.
RPM mechanism is designed to solve these problems. Rpm is a bit like the control panel in Windows system. It will establish a unified database file, record the software information in detail and analyze the dependency automatically.
Example

Now install MySQL

Download address

https://dev.mysql.com/downloads/mysql/
The command format of the installation software RPM – I V H filename. Rpm ා I indicates that the installation V displays the detailed process h as the progress bar
Command format for upgrading software RPM – uvh filename.rpm
Command format for uninstalling software RPM – e filename.rpm
Command format for querying software description information RPM – QPI filename.rpm
Command format for listing software file information RPM – QPL filename.rpm
The command format RPM – QF filename to query which rpm the file belongs to

RPM installation software

Download package

wget https://rpmfind.net/linux/centos/7.5.1804/os/x86_64/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm

Install package

[[email protected] /tmp 11:03:42]#rpm -ivh lrzsz-0.12.20-36.el7.x86_64.rpm
Preparing… ################################# [100%]
Updating / installing…
1:lrzsz-0.12.20-36.el7 ################################# [100%]

RPM query software
RPM – Q lrzsz ා query whether lrzsz is installed
RPM – Qi lrzsz – query the description information of lrzsz package
RPM – QL lrzsz – queries the list of files generated by the lrzsz package
RPM – QC nginx – query the path of configuration file generated by nginx installation
RPM – QF / etc / nginx / fastcgi.conf ා check which RPM package this file is installed by
RPM upgrade software
RPM – uvh / path / to / new? Package? File: upgrade if the older version is installed; otherwise, install;
RPM – FVH / path / to / new? Package? File: upgrade if the older version is installed; otherwise, exit;
RPM uninstall software
rpm -e PACKAGE_NAME
Dependency needs to be resolved manually
Better than Yum remove
RPM dependency, so it’s not easy to use
First, it is difficult to delete

[[email protected] /tmp 11:42:01]#rpm -e nginx
error: Failed dependencies:
nginx is needed by (installed) nginx-mod-mail-1:1.12.2-2.el7.x86_64
nginx is needed by (installed) nginx-mod-http-perl-1:1.12.2-2.el7.x86_64
nginx is needed by (installed) nginx-mod-http-xslt-filter-1:1.12.2-2.el7.x86_64
nginx is needed by (installed) nginx-mod-http-image-filter-1:1.12.2-2.el7.x86_64
nginx is needed by (installed) nginx-mod-stream-1:1.12.2-2.el7.x86_64
nginx is needed by (installed) nginx-mod-http-geoip-1:1.12.2-2.el7.x86_64

Second, difficult to install

[[email protected] /tmp 11:45:07]#rpm -ivh zsh-5.6.2-9.6.2.aarch64.rpm
warning: zsh-5.6.2-9.6.2.aarch64.rpm: Header V3 RSA/SHA256 Signature, key ID 3dbdc284: NOKEY
error: Failed dependencies:
ld-linux-aarch64.so.1()(64bit) is needed by zsh-5.6.2-9.6.2.aarch64
ld-linux-aarch64.so.1(GLIBC_2.17)(64bit) is needed by zsh-5.6.2-9.6.2.aarch64
libdl.so.2(GLIBC_2.17)(64bit) is needed by zsh-5.6.2-9.6.2.aarch64
libm.so.6(GLIBC_2.17)(64bit) is needed by zsh-5.6.2-9.6.2.aarch64
libncursesw.so.6()(64bit) is needed by zsh-5.6.2-9.6.2.aarch64
libtinfo.so.6()(64bit) is needed by zsh-5.6.2-9.6.2.aarch64

Yum command
The yum command is a package manager based on RPM in Fedora, RedHat and SUSE. It enables system managers to interact and automatically manage RPM packages in a more detailed way. It can automatically download and install RPM packages from specified servers, automatically handle dependency relationships, and install all dependent packages at one time without tedious downloading and installation.
Although RPM can help users to query software related dependencies, the problem still needs to be solved by the operation and maintenance personnel themselves, and some large-scale software may have dependencies with dozens of programs, in this case, it will be very painful to install the software Yum software warehouse is designed to further reduce the difficulty and complexity of software installation. Yum software warehouse can analyze the required software package and its related dependencies according to the requirements of users, and then automatically download the software package from the server and install it into the system.
The RPM software package in Yum software warehouse can be officially released by red hat, released by a third party, or written by itself.

Comparison of software management tools under Windows

Yum provides commands for finding, installing, and deleting one, a group, or even all software packages, which are concise and easy to remember.

Yum (option) (parameter)
-H: display help information;
-Y: answer “yes” to all questions;
-C: specify the configuration file;
-Q: quiet mode;
-V: detailed mode;
-D: set the debugging level (0-10);
-E: set error level (0-10);
-R: set the maximum waiting time for Yum to process a command;
-C: run completely from the cache without downloading or updating any header files.

Example
Some common commands include:

Auto search for the fastest mirror plug-in Yum install Yum fastestmirror
Yum source configuration
What is Yum source?
Yum (full name: yellow dog updater, modified) is a shell front-end package manager in Fedora, red hat and CentOS. Based on RPM package management, it can automatically download and install RPM package from the specified server, automatically handle the dependency relationship, and install all the dependent software packages at one time without tedious downloading and installation.
When it comes to Yum source, we must talk about the unique dependency problem in Linux system. Yum exists to solve the dependency problem. The yum source is a directory item. When we use the yum mechanism to install the software, if we need to install the dependent software, the yum mechanism will find the dependent software according to the path defined in the yum source and install the dependent software.
Yum is the abbreviation of “yellow dog updater, modified”. It is a package manager. Yum automatically downloads and installs RPM packages from a specified place (RPM package address or local RPM path of related websites), which can solve the dependency problem well.
The basic working mechanism of Yum is as follows:
Server side: all RPM software packages are stored on the server, and then the dependency relationship of each RPM file is analyzed with relevant functions, and these data are recorded as files and stored in a specific directory of the server.
Client: if you need to install a software, first download the dependency file recorded on the server (via WWW or FTP), analyze the recorded data downloaded from the server, and then obtain all relevant software, and download them all at once for installation.
Directory of Yum source

Enter the yum source directory

cd /etc/yum.repos.d/

View Yum source file

ls -l

Configure alicloud Yum source

1. Good habit, backup Yum source
mkdir repo_bak
mv *.repo repo_bak/
2. Download alicloud repo file
wget http://mirrors.aliyun.com/repo/Centos-7.repo
3. Clear the yum cache and generate a new Yum cache
yum clean all
yum makecache
4. Install the software extension source
yum install -y epel-release

Yum repolist all list all warehouses
Yum list all lists all software packages in the warehouse
Yum info package name view package information
Yum install package name install package
Yum reinstall package name reinstall package
Yum update package name upgrade package
Yum remove package name remove package
Yum clean all knows all warehouse caches
Yum check update checks for packages that can be updated
Yum grouplist view installed packages in the system
Yum groupinstall package group install package group

System service management command
If you have studied or used rhel6 system before, you should be used to the commands such as service and chkconfig to manage system services,
But in rhel7 system, system CTL is changed to manage system services.

Set startup related

Necessary knowledge of Linux directory under / etc
/etc:
This directory is used to store all configuration files and subdirectories required by system management.

Network card profile

/etc/sysconfig/network-script/ifcfg-eth0

Modify the machine name, network card, network management and other configurations

/etc/sysconfig/network

DNS client configuration file of Linux to realize mutual resolution of domain name and IP

/etc/resolv.conf

Local DNS resolution file, set the corresponding resolution of IP and domain name, develop and test the most commonly used temporary domain name resolution

/etc/hosts/

A configuration file, such as path, in which the system global environment variables are persistent

/etc/profile

User’s environment variable

~/.bash_profile
~/.bashrc

The directory where executable programs are stored, mostly system management commands

/usr/sbin

The directory where the user compiles and installs the software > is equivalent to C: \ program files (Windows)

/usr/local

For information about the processor, you can also view the top instruction

/proc/cpuinfo

To view memory information, you can also free – M

/proc/meminfo

How to install program under Linux
RPM – IVH package name. RPM needs to resolve the dependency manually
Yum install package name Yum handles dependencies automatically
Compile installation (source installation)
Install Lrzsz

Install this software, then drag and download Linux code to windows

yum install lrzsz
Server can’t get online error
1.yum reported wrong
Error couldn’t resolve host ‘mirrorlist.centos.org’
2. Ping www.baidu.com to see if you can get online
3. If Baidu doesn’t work, Ping IP works
Ping 119.29.29.29, indicating that there is a problem with DNS

User name > message name or number > message number > message number > template style > date time > Date Format > backup file > backup file > Directory > User >

Recommended Today

The use of progressbarcontrol, a progress bar control of devexpress – Taking ZedGraph as an example to add curve progress

scene WinForm control – devexpress18 download installation registration and use in vs: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 When using ZedGraph to add curves, the number of curves is slower if there are many cases. So in the process of adding curve, the progress needs to be displayed, and the effect is as follows     Note: Blog home page:https://blog.csdn.net/badao_liumang_qizhi […]