Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Grub repair and AWK usage

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

System startup and kernel management, various fixes and compilations

Linux is a kernel.

The ldd command looks up the libraries that each program depends on.

Start-up process and repair of Centos6

Overview of centos6 startup steps

1. Power on POST self-test, load the hardware information of BIOS, and get the first boot device.

2. Read the boot information of the boot loader (grub) in the first boot device MBR

3. Load the core information of the core operating system, the core begins to decompress, and try to drive all the hardware devices

4. The core executes the init program and obtains the default running information

5. Init program execution / etc/rc.d/rc.sysinit file

6. Start the plug-in module of the core

7. Init executes each batch file that runs (scripts)

8. Init execution / etc/rc.d/rc.local

9. Execute the / bin/login program and wait for a moment to log in

10. After logging in, the host is controlled by shell

Bootloader is used to boot the operating system. Realize the early boot of the operating system.

The GRUB startup program is a combination of many parts, of which the first 446 bytes of MBR are only his first stage.

The second stage is put on the disk file: the grub file under / boot

The kernel is under the / boot/ file. This is the kernel file.

Initialized file system: / boot/initramfs-VERSION-release.img this is also a packaged file.

It can be decompressed by modifying the suffix first

The directory of this file is the same as the directory of the root partition. He is a miniature version of the root. This file contains a hidden version of the small kernel.

:-- force this is a mandatory installation override installation.

Chroot: change the mount position of the root

Rescue of damaged Initramfs files

Rescue of initramfs file destruction

The interface of Initramfs file destruction: ↓↓

Then turn off the virtual machine where the power is turned on

Enter the ESC key to enter boot shutdown rescue mode when restarting the virtual machine

Access to this file.

Change the location of the root mount, this is the real root

Mkinitrd file making command is followed by entering this command.

Delete the initramfs. Img recovery:

1 rescue mode (enter rescue mode)

2 chroot / mnt/sysimage root cutting

3 mkinitrd / boot/initramfs-uname-r.img uname-r

4 exit; exit; reboot

Mkinitrd is a script file. Both methods can be used to generate the ramdisk file.

What is used on centos6 is ubantu's init developed based on upstaet.

But they all have the same file / etc/inittab file, which is read when init starts.

Init startup process and repair methods for cracking passwords and rebooting multiple times

After the Init process starts, it starts to read some configuration files.

Key file: / evt/inittab file:

Defines the very important features at startup, and what mode to enter when booting.

Using the runlevel command, you can see in that mode.

Init startup is defined in that mode by the configuration file vim / etc/inittab.

This is also set to boot in that mode, the role of this line is to set boot in that mode.

1 mode single user mode. Can be used for maintenance

2 mode does not have NFS function. Others that are similar to mode 3

4 mode does not use the function.

Init is the first main program in the loading system.

Configuration file for boot

Init1 mode issuing order user mode cracked password

Only one console of concole is provided in single user mode. Only one terminal is provided, the future single-user mode is mainly used for maintenance, using single-user mode to ensure that no one else can access only I can access, in the work under a single user can easily crack the root password.

How to crack the root password on centos 5 and 6:

1: enter any key when restarting, and then enter a to enter the kernel

2: enter 1 to enter single-user mode

3: enter the change password after entering

This has been a single user cracked the password.

In Init6 mode, the virtual machine keeps restarting and repairing.

1:

Enter any key in the restart interface, enter the ↓↓ below and enter the a key

2:

Enter 3-mode ↓↓

3:vim / etc/inittab enters modified init mode

Id: an identity understood as a line

Runlevels: which runlevel does the subsequent program run at? (represents a pattern of 0-6)

Action: indicates what is to be done in this mode. There are several common actions in action:

Wait: switch this runlevel to run only once, not later.

Resoawn: if this process is terminated, it will be restarted (with regenerative function)

Yes).

Initdefault: boot to this operating mode, the boot system will run in the specified mode.

Sysinit: represents a way of initializing the system. When booting up, all init will run the script that the system runs first.

Start the process:

/ etc/rc.d/rc.sysinit: system initialization script

(1) set the host name

(2) set welcome information

(3) set udev rules and selinux

(4) Mount again according to the contents of the / etc/fstab file

(5) remount the root and mount it in a readable and writable manner

(6) set system time

(7) activate swap device

(8) set kernel parameters according to / etc/sysctl.conf file

(9) activate lvm and software raid devices

(10) load drivers for additional devices

(11) cleanup operation

This is a complex script, this script when the system provides good general will not be recommended to modify his.

After the script is finished, it begins to run according to the running mode of the system.

Process: name of the process program.

Print the running level of the current Linux system

If the boot enters 0 mode, then enter any key when the system reads seconds, and then enter an and 5 modes for kernel parameters, bypass the error file at 00:00, and then enter the vim / etc/inittab file configuration.

Modify the system operation level.

Service refers to the script / etc/init.d/atd.

At the beginning of K, it is decided not to start the service.

The start of S determines the start of the service.

Planning a task is a service.

The startup service that starts with S is the boot service.

The order of boot is determined by:

The order is in alphabetical order

There is a certain emphasis on starting services. Some services have certain dependencies, and some services have to start other services first. Turn off the back in front of the tube when shutting down the service, because you don't have to rely too much, and it's the other way around when you turn it on.

In the seven modes of 0-6, the startup or shutdown of the service can be considered to be changed. The difference between 0-6 is that there are various combinations in which various services are started and not started.

The beginning of K and the beginning of S can be manually modified to start with K or S:

After entering the ntsysv command, you can modify the startup of the service (only one mode can be changed, but you can also specify the mode):

After entering, * means to boot up, and the file that starts with S closes it on behalf of the file that begins with K. But he can only start with K in the current modified mode when the boot mode is switched to init other modes he still starts with S files.

This command indicates that only the boot service in the 5 mode is modified (but this command can only change one mode at a time)

Write a script to add services

: it means that the service has just been added to the system, and the system thinks that all modes (0-6) are off.

1. Create a script

(1): must be written to this directory

(2) add to the startup service.

Chkconfig-add is added to the startup service by calling the script name

There is an error in the startup script. The machine can't get up.

1. If the service that cannot be enabled when the virtual machine is turned on, it will be stuck there.

two。 Then restart the service script name off which cannot be obtained by entering chkconfig in single-user mode again to stop him.

3. Then the restart is trying to find a way to get into that startup script to solve.

Delete the startup script.

Chkconfig-- del with script name: the advantage of this deletion is that the soft connection will be gone immediately, and of course the script is still there. This is no longer recommended on centos7.

Non-independent service

Service-- status-all this option shows the status of all services.

Transient service: not too many services are used to find an agent that will be managed by it. This agent is called xinetd service. The services he uses to solve some services are not so frequent. Services that are not normally used are not started and are monitored by the xinetd service.

Services that are proxied with xinetd are called non-independent services.

Non-independent services are supervised and represented by xinetd.

The newly installed services will not start automatically, only manually:

1 start the xinetd service first:

2

(3) restart the xinetd service so that the configuration file takes effect.

Another way to start

This command also changes the yes of the configuration file to on and modifies it directly

S99local:

This script runs all the init scripts after it is run. Just write the command that you want to turn on in this file. It is inconvenient or does not need to be written as a service script in the / etc/rc.d/init.d/ directory, and you want to run it automatically when you boot.

The command can be placed directly in the / etc/rc.d/rc.local file, and the execution permission must be added to the centos7 before it can be run.

Https://cache.yisu.com/upload/information/20200309/28/32514.jpg

Grnb is the process of Boot loader:

The key content is divided into the first stage and the second stage. Grub is our corresponding boot loader.

The version used on centos6 is 0.97 on centos7 and is already 2.02 on centos7. No matter whether it is or its version, its function is loaded to find the operating system. It is software that is separated from the operating system and can be used to start a variety of operating systems.

The first stage:

The GRUB startup program is a combination of many parts, of which the first 446 bytes of MBR are only his first stage.

Stage 1.5:

There is also phase 1.5: the location of the storage is in the sector behind the mbr, the driver is placed in the subsequent sector, and the corresponding driver of the boot is found somewhere later. Find the driver for boot and load boot and then we can go into boot and find this file. Because after entering boot, he will first go to the grub folder, and then locate the file name of the vmlinuz kernel file through a file in grub, which is the grub.conf configuration file.

The second stage:

In the second stage, the grub.conf file will be read to find the kernel according to the configuration file inside.

Grub.conf this configuration file parsing diagram

Yum installs and uninstalls the kernel

1: http://elrepo.org/tiki/tiki-index.php this website provides many new versions of the kernel

2: download the required kernel packages according to the requirements

This is 6 corresponds to 6, and other versions correspond to different ones.

3: enter the kernel configuration file and enter the official website.

(1)

(2)

(3)

(4)

4: install directly after the above work is done:

5: enter cd / boot/grub vim grub.conf

Then the reboot can be restarted to generate.

Delete kernel

Grub repair, and delete / boot/initramfs.Xxx.img restore, and make linux system

Grub corresponds to the first sector and how to repair it if the first sector is damaged.

If the first 446 bytes of boot loader is destroyed and does not need to be backed up to restore, use the grub command to repair it.

(1) repair with grub-install

Syntax:

When performing grub-install for repair, its syntax is to specify the root, or it can not specify the device name followed by the specified hard disk by default.

Grub-install-root-directory=DIR / dev/DISK

(2) or fix it with the interactive command of grub

Syntax:

Grub > root (hd#,#): specify the name of the partition with root after entering, because he wants to find the location of boot

Grub > setup (hd#): then specify which hard drive to install on

Failure repair of the first 446 bytes of Grnb

Use the grub-install command to fix the method

Destroy the first 446 bytes

1: if you restart during production and do not look for the hard disk to find the CD, the mbr partition table is malfunctioning:

2: enter rescue mode

3: use the grub-install method to modify: the repair will be successful

4: check whether the repair is successful after the repair.

This is to see if the first 446 bytes of the first 512 bytes have been repaired successfully

Use the grub interactive repair method

Use grub interactive repair, which depends on several files in the gurb under boot.

1:grub interactive repair

2: OK after entering the interactive post-rescue repair

3: check whether the repair is successful after the repair.

This is to see if the first 446 bytes of the first 512 bytes have been repaired successfully

Destroy stage 1.5 repair in grub skip the first 512 bytes of damage repair

1: the byte failure after destroying 512 shows that this is different from the state before destroying 512 bytes, but it can't get up, but this time he doesn't prompt to use the CD because he thinks the hard drive has boot function.

2: directly enter the rescue mode to repair

3: synchronize after repair

Then exit exits and goes directly to the restart interface.

Using grub.conf configuration files to prevent password cracking in single-user mode

one

We can add a lot of parameters to the kernel parameters, and we can observe the role of each parameter by installing the kernel-doc document.

Making linux system

This experiment is of great significance for understanding the composition of linux.

1: create a small linux system first

2: find the configuration file for the new centos

3: add the new virtual machine to the disk of the existing virtual machine

4: once added, divide the work directly on shell.

(1)

(2)

(3)

(4) then mount and install grub

Mount boot

The mkdir / mnt/boot subdirectory must be boot

Mount / dev/sdb1 / mnt/boot

 install grub

Grub-install-root-directory=/mnt / dev/sdb

(5) write grub.conf configuration file

(4) add details of the root

5: add network services

6: restart the virtual machine of the installed small linux and then install the network service

Summarize the composition of the linux system:

As far as startup is concerned, Boot Loader (that is, grub), the kernel and other related files must have this to put together a linux.

Repair of logical volume corruption. Experiment:

The second stage of grub damage repair

The second stage mainly corresponds to / boot/grub. The whole broken fix in the Grub directory

To do the experiment, delete all of this directory:

1: Grub second stage failure state:

2: enter rescue mode

3: first cut the root and then enter grub-install / dev/sda: this command is a universal command, which can repair the first and second stage commands.

4: after being fixed, go to grub but the grub.conf configuration file is missing. If you restart now, the menu item will be missing. Enter the / boot/grub file to edit the grub.conf configuration file and restart it.

(1)

(2)

(3) write grub.conf configuration file

(5) if there is interactive, modify it directly according to interactive.

Deleted / boot/initramfs.Xxx.img, restore

If the / etc/fstab file and / boot file are all destroyed

1:

2:

(1) when the logical volume is no longer started normally, it will not be activated normally, so the logic is currently inaccessible.

(2) activate logical volumes

(3) Mount the root activation file directory, and mount boot will be used.

Because root is used as a root. There is a gurb startup file in Boot.

(4) compile and mount files

3: after changing the logical volume configuration file, enter the rescue mode again

(1) Mount and install and repair the kernel when you are in rescue mode

(2) repair grub and write grub configuration file

: repair grub files

Use redirection to import this into the vim configuration file. Finally, synchronously refresh using sync.

Proc directory

There are a lot of things that can be set under proc, but there are also a lot of things that can be read. Under proc, things that mainly store a large number of processes during the operation of the system are under proc. Some settings can also be modified through the proc directory.

The / Proc/sys directory can modify a lot of parameters.

This file is dedicated to managing the configuration of this directory

Ban ping ↓↓

If you change it to 1, the ping will not work.

The router is in effect forever.

Sysctl-a lists all active

Sysctl-p reads the parameters of the configuration file to make it effective.

TCP protocol connection optimization.

Source code compiles kernel

If you want to use the latest kernel, you have to compile source code:

When compiling, we can compile by clicking the menu.

[M]: represented as a module, the advantage of independent files on disk is that they are loaded as needed.

[*]: it is integrated into the kernel file. It means it will be packaged into our core file.

Lsmod various loaded driver modules, which can be loaded on demand or manually

Modprobe is followed by manual loading of the file type loading module (the driver module can be loaded or unloaded)

Rmmod is followed by file type unloading module

Modinfo is followed by the file type to see the module details.

There are dependencies between modules.

First uninstall the dependent file type unload the uninstall module.

Insmod installs the load module (followed by a file name and path)

Compile the software and dependency packages needed to install the kernel

The compilation environment should be considered when compiling.

7 compiles the kernel and supports ntfs files

1: download the new kernel on the Internet, and then drag it into xshell.

In choosing this item

At the end of selecting the NTFS file, all exit and save for compilation

two。 The installation should be divided into two steps, the first step is to install the module and the second step is to install the kernel.

(1) make modules_install installation module

(3) make install installs a new kernel

This is the compilation.

Busybox introduction

Busybox source code compilation

There is a tool called make install, which automatically places the soft link in a directory called _ install, and just tests out the soft link.

Busybox source code compilation

1: install the required dependency packages

Yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl

Openssl-devel systemd-devel zlib-devel glibc-static ncurses-devel

2: go to the official website to download the source code https://www.busybox.net/

Copy version link

 cd busybox-1.30.1/

 make menuconfig selects the following to compile busybox into static binaries without sharing libraries

Busybox Settings-- > Build Options-- > [] Build BusyBox as a static

Binary (no shared libs)

 make & & make install if there is an error, re-execute the above command after executing make clean

 mkdir / mnt/sysroot/

 cp-a _ install/ / mnt/sysroot/

The principle of Systemd

Then centos7 uses the systemd process to manage all resources, and these resources are collectively referred to as unit,unit, which divides different resources into different types, no matter what type is collectively referred to as unit units.

The most important part of Unit is / urs/lib/systemd/system, which contains a variety of resources, mainly with different file suffixes. This is all for unit to use different file suffixes to distinguish resource types.

The most common ones are service files, which are equivalent to various service scripts on centos6. That is to say, a service has other resources besides service. For example, target,target is like the 0-6 mode of runlevel before.

The higher the priority, the higher the priority of the script in the file.

Systemctl command and the usage of various options

The advantage of this syntax is that it can manage and view multiple services.

When starting on centos7, ok is no longer displayed, if it is not displayed, it is normal, and if it is displayed, it is often an error.

The Maks option means that a service is disabled for him to use.

Mask forbidden service process: ↓↓

Unmask Unban Service process: ↓↓

In this way, it will not cause misoperation.

To view the status of whether a service is currently active or not, the following two figures are shown: ↓↓

Setting it to boot or not to start is essentially whether there is a soft link created.

The next boot is disabled, and then a soft connection is created

Red indicates no startup, green indicates startup

It is a collection of resources that serve a variety of states. When you enter something and have various dependencies on each other, putting multiple functions in a file is equivalent to a target.

Centos7 fixes the first 446 bytes of grub

Centos7 cracked the root password

Anti-cracking password add grub2 password

The second phase of Centos7 repair grub2

Remove centos7 / boot repair

Repair order: first repair the kernel and then modify gurb

The repair is done.

Modify the default boot kernel

If the kernel does not want to delete the method and modify the default kernel

The default boot kernel can be set in both of the following figures

Proficiency in experiment: systemctl start stop restart enable disable mask unmask is-active is-enabled name.service

Systemctl set-default multi-user.target

Secure reinforcement of SElinux

The more secure, the more complicated.

MAC: mandatory access control

Whether the current mac mode is enabled and disabled

This file can be modified to disable and allow

Temporarily modify the alarm and do not stop it

Text three Musketeers AWK (key points)

Process one line for each read, and then process it. This command can not only deal with all kinds of text messages, but also deal with standard content and some content passed through the pipeline.

The main functions can realize the processing of text.

Awk syntax:

The Awk + option-f can be followed by a file or program. The file behind him does not have to be followed, and the standard input can be processed.

The format of the program is divided into two parts, one is pattern, the other is action in parentheses. Pattern is the pattern, and the pattern determines whether each line read conforms to his pattern definition. If it conforms to this pattern definition, he will perform action actions for the contents of this line. If it does not meet this pattern definition, he will not perform the actions in action.

Automatically read each line in the file and use pattren to filter the lines that meet the pattern condition, and execute action accordingly. If not, skip directly.

The most common action of action is print (printing). Awk has a very good feature: it automatically reads a line into it, and then automatically cuts the read line into multiple fields.

Cutting does not need to be artificially cut, the default can be cut with a blank character (the space tab key can be used), you can use this blank character to automatically put a line. Cut into multiple fields, and after cutting, awk automatically assigns variables to these cut fields, the first field is assigned $1, and the second field is $2. Until $n.

But this $does not have the same meaning as the $in the shell script, where $0 represents the entire line of all fields. The default is one-line processing, but one-line processing can also be avoided, where wak can specify a row as a record as a unit. In fact, it is possible to divide units without rows.

If this action action is not written, it is equivalent to performing a print $0 operation.

How Awk works

Comma separator; the output item can be either a string or a numeric value; the field, variable, or awk expression of the current record; if item is omitted, it is equivalent to print $0.

The awk program usually consists of three parts: the BEGIN sentence block, the general sentence block which can match the matching pattern, and the END sentence block. Program is usually enclosed in single or double quotation marks.

Options:

-F: indicates the field delimiter used when entering

-v var=value: custom variable.

Basic format:

Awk [options] 'program' file

Program: expressed as pattern {action statements;...}

Pattern: indicates when the action statement is triggered and events BEGIN and END are partially determined.

Action statements: data processing. Specify print and printf in {}.

Delimiters, fields, and records:

When the awk is executed, the field (domain) of the delimiter marks $1 # 2. Domain as the domain ID. $0 is all fields. Note: the variable $does not mean anything in shell.

Same as

Each line of the file is called a record

If action is omitted, the operation of print $0 is performed by default.

BEGIN is similar to printing:

Example figure:

The most important function of Awk is the processing of files.

Double quotation marks must be enclosed in double quotation marks as long as they are ordinary strings and not variables, except for numbers.

The input line is read in according to the file or standard, and then the pattren {task} statement block is executed, and then executed line by line until the last line is finished.

But awk also supports two other separate special statements, one called BEGIN and the other called END.

BEGIN,END is not executed during file processing

BEGIN is the beginning: execute BEGIN before reading the file, execute before the file, you can make some headers (headings) of the printed form.

END is the end: END, on the contrary, processes all the files and executes them after the file is executed.

It is equivalent to a head and a tail, which can be summarized.

When Awk performs split printing, the default is to cut the white space character into multiple fields, and then use $1 to represent the first field.

In fact, his cutters can also be cut artificially-F is to specify what characters to use as cutters.

The comma between $1 and $3 defaults to using spaces as delimiters, and we can also artificially add some symbols as specified delimiters

In AWK, as long as the content we print is not a variable or a number, we must use double quotation marks.

The usage of awk variable

Awk also supports variables, one is a built-in variable supported by the system, and the other is a custom variable.-v means that there is a variable assignment, and a-v represents a variable. If there are multiple assignments, each variable has to add-v.

FS: is a variable that comes with the system, and the function of this variable is the function of-F. But this FS is a variable.

The standard syntax for variables is-v to assign values to variables. FS is a variable that comes with the system. Its function is to declare the separator between fields and fields, so use-v FS to declare a value to him. After assignment, we go to print to call it.

FS variable can be declared and called: ↓↓

The variable FS specifies how to distinguish between the delimiters of $1, "2," 3, and so on when the file is read, and the delimiter is defined with FS. In fact, its function is the same as that of-F.

You need to specify the delimiter, so use-F or FS to specify the variable of the colon.

Variable reference: ↓↓ (the advantage of a variable is that it can be referenced after the previous declaration)

Multiple reference variables and calling shell variables are referenced into awk variables.

The OF S variable specifies the separator between the column and the column when output: ↓↓

RS defines the separator ↓↓ between the field record and the record at input time

ORS defines the delimiter ↓↓ between the field record and the record on output

The variable NF is expressed as the number of fields.

NR defines the number of records, and one line is a record to print his number.

FNR indicates that each file has its own independent number

: division number between file and file

FILENAME defines which file this line is from

ARGC defines the number of parameters

ARGV defines digitally saved command line parameters

You can also customize variables in awk, which can be declared with-v.

In fact, you can also define variables in the program, in curly braces (programs)

In fact, variables can also be defined in the program. In curly braces (programs), ↓↓ must first be defined in the

The delimiter is not necessarily a character or can be a regular expression ↓↓

The-f option calls the file, and the file can be stored to perform actions:

The following picture invokes the contents of the above picture.

Awk Printf option usage

Printf is also supported in our shell, but it is an internal command that is familiar with the usage of awk's printf and shell.

The power of Printf is that it can define formats.

Printf syntax:

You must specify a template, define the format to be printed later, and then follow a number of printed content, which will be printed according to the defined format.

Format characters: one to one correspondence with item (English translation: project)

It will not wrap automatically. The newline control character needs to be displayed.\ n

% c: ASCII code that displays characters

% d,% I: display decimal integers (used more)

% e,% E: display scientific counting values

% f: displayed as a floating point number

% g,% G: display values in scientific counting or floating point form

% s: display string (used more)

% u: unsigned integer

%%: show% itself (used more)

It can also specify the format of the print string and insert modifiers in the middle.

Modifier

# [. #] the first number controls the width of the display; the second # represents the precision after the decimal point, for example:% 3.1f f: the width of the print time

Left alignment (default right alignment) example:%-15s + positive and negative symbols showing values example:% + d

Sina interview question: ↓↓

Operator operation

Arithmetic operator:

Xaddiy: addition

Xmury: subtraction

X multipliy: multiplication

XUnix: division

X ^ y: power operation

X% y: take the module (remainder).

Assignment operator:

The right side is assigned to the left side

+ =: add first, then assign value

-=: subtract first, then assign value

* =: multiply first, then assign values

/ =: divide first, then assign value

% =: take the remainder first, and then assign the value

^ =: first exponentiation, then assignment

+ +: incremental operation

-: decrement operation.

BEGIN/END mode

BEGIN {}: execute only once before starting to process the text in the file

END {}: executes only once after text processing is complete.

Regular expressions and operators of Awk

Compare operator:

=: judge equality

! =: judgments vary

\ >: judge to be greater than

\ > =: judge greater than or equal to

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report