Linux的启动和内核管理

# CentOS 6 的启动管理

1. Linux 组成  (由内核和文件系统组成)

kernel 实现进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能等功能

rootfs 包括程序和 glibc 库

程序:二进制执行文件

库:函数集合, function, 调用接口(头文件负责描述)


2. 内核设计流派

宏内核(monolithic kernel):又称单内核和强内核,Unix,Linux把所有系统服务都放到内核里,所有功能集成于同一个程序,分层实现不同功能,系统庞大复杂,Linux其实在单内核内核实现了模块化,也就相当于吸收了微内核的优点


微内核(micro kernel):Windows,Solaris,HarmonyOS简化内核功能,在内核之外的用户态尽可能多地实现系统服务,同时加入相互之间的安全保护,每

种功能使用一个单独子系统实现,将内核功能移到用户空间,性能差


3. CentOS 6启动流程

1. 加载BIOS的硬件信息,获取第一个启动设备

2. 读取第一个启动设备MBR的引导加载程序(grub)的启动信息

3. 加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有的硬件设备

4. 核心执行init程序,并获取默认的运行信息

5. init程序执行/etc/rc.d/rc.sysinit文件,重新挂载根文件系统

6. 启动核心的外挂模块

7. init执行运行的各个批处理文件(scripts)

8. init执行/etc/rc.d/rc.local  系统最后执行的一个文件

9. 行/bin/login程序,等待用户登录

10. 登录之后开始以Shell控制主机


4. 硬件启动POST

POST:Power-On-Self-Test,加电自检,是BIOS功能的一个主要部分。负责完成对CPU、主板、内存、硬盘子系统、显示子系统、串并行接口、键盘等硬件情况的检测


主板的ROM:BIOS,Basic Input and Output System,保存着有关计算机系统最重要的基本输入输出程序,系统信息设置、开机加电自检程序和系统启动自举程序等


主板的RAM:CMOS互补金属氧化物半导体,保存各项参数的设定,按次序查找引导设备,第一个有引导程序的设备为本次启动设备


5. 启动加载器 bootloader

grub 功能和组成

bootloader: 引导加载器,引导程序

windows: ntloader,仅是启动OS

Linux:功能丰富,提供菜单,允许用户选择要启动系统或不同的内核版本;把用户选定的内核装

载到内存中的特定空间中,解压、展开,并把系统控制权移交给内核


Linux的bootloader

LILO:LInux LOader,早期的bootloader,功能单一

GRUB: GRand Unified Bootloader, CentOS 6 GRUB 0.97: GRUB Legacy, CentOS 7 以后使用

GRUB 2.02


GRUB 启动阶段

primary boot loader :

# 一阶段 :1st stage:MBR的前446个字节   

# 1.5阶段 :1.5 stage: mbr 之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统

# 二阶段 :secondary boot loader :2nd stage,分区文件/boot/grub/


#  恢复第一阶段  

1. hexdump -C -n 512 /dev/sda -v 查看二进制  前448字节为0   

2. 重新启动系统 进入救援模式

光盘启动,进入rescue模式

#chroot /mnt/sysimage    

#grub-install /dev/sda

#sync

#exit

#exit

3. 重启后在此看产二进制   如前448字节恢复,就完成修复


#  恢复1.5阶段

1. 光盘启动,进入rescue模式

#chroot /mnt/sysimage

#grub-install /dev/sda

#sync

#按 ctrl+alt+delete 三个键重启动


# grub legacy 管理

配置文件:/boot/grub/grub.conf <-- /etc/grub.conf

grub legacy 功用:

(1) 提供启动菜单、并提供交互式接口

# a:内核参数

# e:编辑模式,用于编辑菜单

# c:命令模式,交互式接口

(2) 加载用户选择的内核或操作系统

允许传递参数给内核

可隐藏启动菜单

(3) 为菜单提供了保护机制

为编辑启动菜单进行认证

为启用内核或操作系统进行认证


# grub legacy配置文件:/boot/grub/grub.conf

default=#: 设定默认启动的菜单项;落单项(title)编号从0开始

timeout=#:指定菜单项等待选项选择的时长

splashimage=(hd#,#)/PATH/XPM_FILE:菜单背景图片文件路径

password [--md5| --encrypt] STRING: 启动菜单编辑认证

hiddenmenu:隐藏菜单

title TITLE:定义菜单项“标题”, 可出现多次

root (hd#,#):查找stage2及kernel文件所在设备分区;为grub的根

kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:启动的内核

initrd /PATH/TO/INITRAMFS_FILE: 内核匹配的ramfs文件

password [--md5|--encrypted ] STRING: 启动选定的内核或操作系统时进行认证


# 加载 kernel

# kernel 自身初始化过程

1. 探测可识别到的所有硬件设备

2. 加载硬件驱动程序(借助于ramdisk加载驱动)

3. 以只读方式挂载根文件系统

4. 运行用户空间的第一个应用程序:/sbin/init

# Linux内核特点:

支持模块化:.ko(内核对象),如:文件系统,硬件驱动,网络协议等

支持内核模块的动态装载和卸载

内核组成部分:

核心文件:/boot/vmlinuz-VERSION-release

ramdisk:辅助的伪根系统,加载相应的硬件驱动,ramdisk --> ramfs 提高速度

CentOS 5 /boot/initrd-VERSION-release.img

CentOS 6 以后版本 /boot/initramfs-VERSION-release.img

模块文件:/lib/modules/VERSION-release


# 范例:误删除内核文件/boot/vmlinuz-2.6.32-754.el6.x86_64无法启动,故障恢复

[root@centos6 ~]#rm -f /boot/vmlinuz-2.6.32-754.el6.x86_64

[root@centos6 ~]#reboot

#进入rescue模式

#chroot /mnt/sysimage

#mount /dev/sr0 /mnt/

#cp /mnt/isolinux/vmlinuz /boot/vmlinuz-2.6.32-754.el6.x86_64

#sync

#exit

#reboot


# ramdisk文件的制作:mkinitrd命令

mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)

# dracut命令

dracut /boot/initramfs-$(uname -r).img $(uname -r)


# 范例:误删除/boot/initramfs-2.6.32-754.el6.x86_64.img无法启动,故障恢复

[root@centos6 ~]#rm -f /boot/initramfs-2.6.32-754.el6.x86_64.img

[root@centos6 ~]#reboot

#进入rescue模式

#chroot /mnt/sysimage

#mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)

#sync

#exit

#exit

#reboot


# 故障:rm -rf /boot/* 和 /etc/fstab 进行恢复

1. 用光盘进入 rescue mode,找到/ 所在分区并恢复/etc/fstab

fdisk -l

mkdir /mnt/rootdir

mount /dev/sdaN /mnt/rootdir

ls /mnt/rootdir

mount /dev/sda2 /mnt/rootdir

vim /mnt/rootdir/etc/fstab

/dev/sda1 /boot ext4 defaults 0 0

/dev/sda2 / ext4 defaults 0 0

/dev/sda3 /data ext4 defaults 0 0

/dev/sda5 swap swap defaults 0 0

reboot


2. rescue mode 恢复内核和initrd 文件

/dev/sda2 --> /mnt/sysimage

chroot /mnt/sysimage

mount /dev/sr0 /mnt/

#方法1

rpm -ivh /mnt/Packages/kernel.xxxx.rpm --force

#方法2

cp /mnt/isolinux/vmlinuz /boot/

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


3. 修复 grub

grub-install /dev/sda

vim /boot/grub/grub.conf 方法2

[root@centos6 ~]#cat /boot/grub/grub.conf

default=0

timeout=5

title centos

kernel /vmlinuz root=/dev/sda2

initrd /initramfs.img


4. reboot


#  init初始化

POST --> BootSequence (BIOS) --> Bootloader(MBR) --> kernel(ramdisk) --> rootfs(只读) -->init(systemd)

## init程序的类型:

SysV: init, CentOS 5之前

配置文件:/etc/inittab

Upstart: init,CentOS 6

配置文件:/etc/inittab, /etc/init/*.conf

Systemd:systemd, CentOS 7

配置文件:/usr/lib/systemd/system

/etc/systemd/system


# 运行级别 运行级别:为系统运行或维护等目的而设定;0-6:7个级别,一般使用3, 5做为默认级别

0:关机

1:单用户模式(root自动登录), single, 维护模式

2:多用户模式,启动网络功能,但不会启动NFS;维护模式

3:多用户模式,正常模式;文本界面

4:预留级别;可同3级别

5:多用户模式,正常模式;图形界面

6:重启

1. 切换模式 init #

2. 查看模式级别 runlevel | who -r

3. 定义运行级别   /etc/inittab   进入文件把最后一行的数字修改一下就可以长久保存 

 # 如定义成0或6模式  需要在系统启动时  随便摁一个键 进入grub模式   a键进入 在后方添加一个 3 就是三模式 ,重启后在吧配置文件修改回来即可


# /etc/rc.d/   存放0-6 其中模式中启动所开启和默认不启动的服务   但是这个只是个软连接 实际目录放在了  /etc/init.d  的目录中  如何需要添加服务都是在这个文件中添加

# chkconfig --list  可以列出0-6 七种模式中服务的状态  

范例:

[16:39:01 root@CentOS6 ~]#chkconfig iptables on

  启动iptables服务  但是默认启动2-5模式  on就是启动  off就是关闭

[16:39:01 root@CentOS6 ~]#chkconfig --and  httpd    添加httpd服务   如自己制作一个服务  需要添加权限

[16:39:01 root@CentOS6 ~]#chkconfig --del  httpd    删除httpd服务   会把相应的软连接文件全部删除

[16:39:01 root@CentOS6 ~]#chkconfig --level 35 httpd on   把httpd服务设置在3和5模式中启动


# 直接把服务路径放在/etc/rc.d/rc.local 后边加一个start   就会直接开机启动

开机启动文件 /etc/rc.d/rc.local     /etc/rc.d/rc.local在指定运行级别脚本后运行


# /etc/rc.d/rc.sysinit   在启动服务脚本之前 系统会先启用这个脚本   cat /etc/init/rcS.conf调用这个文件  随后启动服务脚本

系统初始化脚本功能

(1) 设置主机名

(2) 设置欢迎信息

(3) 激活udev和selinux

(4) 挂载/etc/fstab文件中定义的文件系统

(5) 检测根文件系统,并以读写方式重新挂载根文件系统

(6) 设置系统时钟

(7) 激活swap设备

(8) 根据/etc/sysctl.conf文件设置内核参数

(9) 激活lvm及software raid设备

(10)加载额外设备的驱动程序

(11)清理操作


# 超级守护进程  telnet-server   安装此包会携带安装xinetd 服务     

xinetd负责监视系统中不常用的服务  在特定时间内如果需要 会唤醒当中需要启动的服务 使用完毕后会再次进入睡眠

/etc/xinetd.d/  此目录里边放着所有不常用服务的配置文件   可以进入相应的配置文件更改开启或者关闭  吧里边的disable=yes/no  no是开启/yes是关闭

或者用chkconfig 开启或者关闭 

# xinend服务只是用来替不常用的服务来值岗,如有人访问此服务xinend则会唤醒此服务

service xinend start 启动服务  用ss -netl  查看有没有23端口  如果有就是已经开启服务

在另一个客户端用telnet命令来查看此服务

范例:

[21:02:09 root@CentOS28 ~]#telnet 10.0.0.6

Trying 10.0.0.6...

Connected to 10.0.0.6.

Escape character is '^]'.

CentOS release 6.10 (Final)

Kernel 2.6.32-754.el6.x86_64 on an x86_64

CentOS6.1 login: root

Password: 

Login incorrect


CentOS6.1 login: 


# 在原有的客户端上用ps -aux  看到来访问的人已由telnet来接待

root       1791  0.0  0.0  12528  1024 ?        Ss   20:47   0:00 in.telnetd: 10.0.0.28

# 如访问人结束访问 此服务则会继续处于睡眠状态。



# 自制linux系统

## 分区并创建文件系统

#分两个必要的分区,/dev/sdb1对应/boot /dev/sdb2对应根 /

[root@centos6 ~]#echo -e 'n\np\n1\n\n+1G\nw\n' | fdisk /dev/sdb

[root@centos6 ~]#echo -e 'n\np\n2\n\n\n\nw\n' | fdisk /dev/sdb

[root@centos6 ~]#mkfs.ext4 /dev/sdb1

[root@centos6 ~]#mkfs.ext4 /dev/sdb2


## 挂载boot

#子目录必须为boot

[root@centos6 ~]#mkdir /mnt/boot

[root@centos6 ~]#mount /dev/sdb1 /mnt/boot


## 安装grub

[root@centos6 ~]#grub-install --root-directory=/mnt/ /dev/sdb


## 准备内核和initramfs文件

[root@centos6 ~]#cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /mnt/boot/vmlinuz

[root@centos6 ~]#cp /boot/initramfs-2.6.32-754.el6.x86_64.img /mnt/boot/initramfs.img


## 建立grub.conf

[root@centos6 ~]#cat /mnt/boot/grub/grub.conf

default=0

timeout=6

title wang linux

root (hd0,0)

kernel /vmlinuz root=/dev/sda2 selinux=0 init=/bin/bash 

initrd /initramfs.img

[root@centos6 ~]#tree /mnt/boot

/mnt/boot

├── grub

│ ├── device.map

│ ├── e2fs_stage1_5

│ ├── fat_stage1_5

│ ├── ffs_stage1_5

│ ├── grub.conf

│ ├── iso9660_stage1_5

│ ├── jfs_stage1_5

│ ├── minix_stage1_5

│ ├── reiserfs_stage1_5

│ ├── stage1

│ ├── stage2

│ ├── ufs2_stage1_5

│ ├── vstafs_stage1_5

│ └── xfs_stage1_5

├── initramfs.img

├── lost+found

└── vmlinuz

2 directories, 16 files

[root@centos6 ~]#


## 准备根下面相关程序和库

[root@centos6 ~]#mkdir /mnt/sysroot

[root@centos6 ~]#mount /dev/sdb2 /mnt/sysroot

[root@centos6 ~]#mkdir –pv

/mnt/sysroot/{boot,dev,sys,proc,etc,lib,lib64,bin,sbin,tmp,var,usr,opt,home,root

,mnt,media}

#复制bash等命令和相关库文件,如:

bash,ifconfig,insmod,ping,mount,ls,cat,df,lsblk,blkid,tree,fdisk

[root@centos6 ~]#mkdir /mnt/sysroot/{dev,proc,etc,sys,lib,home,root}


## #准备网卡驱动

[root@centos6 ~]#ethtool -i eth0

driver: e1000

version: 7.3.21-k8-NAPI

firmware-version:

bus-info: 0000:02:01.0

supports-statistics: yes

supports-test: yes

supports-eeprom-access: yes

supports-register-dump: yes

supports-priv-flags: no

[root@centos6 ~]#modinfo -n e1000

/lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko


[root@centos6 ~]#cp /lib/modules/2.6.32-

754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/


[root@centos6 ~]#chroot /mnt/sysroot

## 准备一台新的虚拟机  将前一虚拟机sdb硬盘对应的vmdk文件增加进去,删除原有磁盘,开机启动



# /proc 目录和内核参数管理     

/proc目录:内核把自己内部状态信息及统计信息,以及可配置参数通过proc伪文件系统加以输出

## 内核参数:

只读:只用于输出信息

可写:可接受用户指定“新值”来实现对内核某功能或特性的配置

# 默认配置文件:/etc/sysctl.conf 及以下文件

/run/sysctl.d/*.conf

/etc/sysctl.d/*.conf

/usr/local/lib/sysctl.d/*.conf

/usr/lib/sysctl.d/*.conf

/lib/sysctl.d/*.conf

/etc/sysctl.conf

# 建议把参数放在总配置文件/etc/sysctl.conf和/etc/sysctl.d/自己创建一个文件后缀为.conf

## /proc/sys 设置

sysctl命令用于查看或设定此目录中诸多参数

选项

# 临时设置某参数    就是临时启动某个参数

sysctl -w net.ipv4.ip_forward=1   就是临时启动某个参数

# 不关机情况下启动test.conf文件里的内核参数(因test.conf是自己创建的 所以需要加路径)  如在etc/syscet.conf 下编辑的内核参数  可直接用sysctl -p 就可直接实现不重启 启动

sysctl -p /etc/sysctl.d/test.conf

# 查看所有生效的参数

sysctl -a

## 常用的内核参数:

net.ipv4.ip_forward

net.ipv4.icmp_echo_ignore_all

net.ipv4.ip_nonlocal_bind #允许应用程序可以监听本地不存在的IP

vm.drop_caches

fs.file-max = 1020000  系统打开文件次数    


# sys目录

/sys目录:

使用sysfs文件系统,为用户使用的伪文件系统,输出内核识别出的各硬件设备的相关属性信息,也有内核对硬件特性的设定信息;有些参数是可以修改的,用于调整硬件工作特性

udev通过此路径下输出的信息动态为各设备创建所需要设备文件,udev是运行用户空间程序 专用工具:udevadmin, hotplug

udev为设备创建设备文件时,会读取其事先定义好的规则文件,一般在/etc/udev/rules.d及/usr/lib/udev/rules.d目录下

# 内核模块管理和编译

单内核体系设计、但充分借鉴了微内核设计体系的优点,为内核引入模块化机制

内核组成部分:

1. kernel:内核核心,一般为bzImage,通常在/boot目录下  名称为 vmlinuz-VERSION-RELEASE

2. kernel object:内核对象,一般放置于  /lib/modules/VERSION-RELEASE/

3. 辅助文件:ramdisk  initrd:从CentOS 5 版本以前    initramfs:从CentOS6 版本以后

# 内核模块命令

lsmod命令:

显示由核心已经装载的内核模块

显示的内容来自于: /proc/modules文件

# 范例 [root@centos8 ~]#lsmod

Module Size Used by

uas 28672 0

usb_storage 73728 1 uas

nls_utf8 16384 0

isofs 45056 0

# 显示:名称、大小,使用次数,被哪些模块依赖


# modinfo命令:管理内核模块

配置文件:/etc/modprobe.conf, /etc/modprobe.d/*.conf  

选项:

-n:只显示模块文件路径

-p:显示模块参数

-a:作者

-d:描述

范例:

# [root@centos8 ~]#lsmod |grep xfs

xfs 1474560 2

libcrc32c 16384 1 xfs

# [root@centos8 ~]#modinfo xfs

filename: /lib/modules/4.18.0-147.el8.x86_64/kernel/fs/xfs/xfs.ko.xz

license: GPL

description: SGI XFS with ACLs, security attributes, no debug enabled

author: Silicon Graphics, Inc.

alias: fs-xfs

rhelversion: 8.1

# 装载或卸载内核模块

modprobe [ -C config-file ] [ modulename ] [ module parame-ters... ]

modprobe [ -r ] modulename…



# depmod命令:内核模块依赖关系文件及系统信息映射文件的生成工具

# insmod命令:可以安装模块,需要指定模块文件路径,并且不自动解决依赖模块

范例:

insmod `modinfo –n exportfs`

insmod `modinfo –n xfs`

# rmmod命令:卸载模块

范例:

rmmod xfs

rmmod exportfs


# Busybox

简单的说BusyBox就好像是个大工具箱,它集成压缩了Linux 的许多工具和命令,也包含了 Android 系统的自带的shell

# Busybox使用

busybox后直接跟命令,如 busybox ls

直接将busybox重命名,如 cp busybox tar

创建符号链接,如 ln -s busybox rm

# busybox的安装

以上方法中,第三种方法最方便,但为busybox中每个命令都创建一个软链接,相当费事,busybox提供自动方法:busybox编译成功后,执行make install,则会产生一个_install目录,其中包含了busybox及每个命令的软链接

busybox编译安装

# [root@centos7 ~]#yum -y install gcc gcc-c++ glibc glibc-devel make pcre pcredevel openssl openssl-devel systemd-devel zlib-devel glibc-static ncurses-devel

                                安装相对的依赖包

# [root@centos7 ~]#wget https://busybox.net/downloads/busybox-1.31.1.tar.bz2

                         下载busybox

# [root@centos7 ~]#tar xvf busybox-1.31.1.tar.bz2

                          解压

# [root@centos7 ~]#cd busybox-1.31.1/

     进入此目录

# [root@centos7 busybox-1.31.1]#make menuconfig 

# 按下面选择,把busybox编译也静态二进制、不用共享库:Settings -->Build Options -->[*] Build static binary (no sharedlibs)

# [root@centos7 busybox-1.31.1]#make #如果出错,执行make clean后,重新执行上面命令

# [root@centos7 busybox-1.31.1]#ls

# [root@centos7 busybox-1.31.1]#make install

# [root@centos7 busybox-1.31.1]#pwd

/root/busybox-1.31.1

# [root@centos7 busybox-1.31.1]#ls

applets debianutils loginutils

qemu_multiarch_testing

applets_sh docs mailutils README

arch e2fsprogs Makefile runit

archival editors Makefile.custom scripts

AUTHORS examples Makefile.flags selinux

busybox findutils Makefile.help shell

busybox.links include make_single_applets.sh

size_single_applets.sh

busybox_unstripped init miscutils sysklogd

busybox_unstripped.map _install modutils testsuite

busybox_unstripped.out INSTALL networking TODO

Config.in klibc-utils NOFORK_NOEXEC.lst TODO_unicode

configs libbb NOFORK_NOEXEC.sh util-linux

console-tools libpwdgrp printutils

coreutils LICENSE procps

# [root@centos7 busybox-1.31.1]#ll busybox -h

-rwxr-xr-x 1 root root 2.6M May 13 14:46 busybox

# [root@centos7 busybox-1.31.1]#ls _install/

bin linuxrc sbin usr

# [root@centos7 busybox-1.31.1]#ls _install/bin

arch dd grep login netstat rmdir tar

ash df gunzip ls nice rpm

touch

base64 dmesg gzip lsattr nuke run-parts true

busybox dnsdomainname hostname lzop pidof scriptreplay

umount

cat dumpkmap hush makemime ping sed

uname

chattr echo ionice mkdir ping6 setarch

usleep

chgrp ed iostat mknod pipe_progress setpriv vi

chmod egrep ipcalc mktemp printenv setserial

watch

chown false kbd_mode more ps sh zcat

conspy fatattr kill mount pwd sleep

cp fdflush link mountpoint reformime stat

cpio fgrep linux32 mpstat resume stty

cttyhack fsync linux64 mt rev su

date getopt ln mv rm sync

# [root@centos7 busybox-1.31.1]#find _install/ -type l |wc -l

396

# [root@centos7 busybox-1.31.1]#du -sh _install/

2.6M _install/

# [root@centos7 busybox-1.31.1]#mkdir /mnt/sysroot/

# [root@centos7 busybox-1.31.1]#cp -a _install/* /mnt/sysroot/

# centos8 目前不支持  可以再7上安装完 把程序直接拷贝到8上

# 自制小linux的时候可不用吧命令所依赖的库拷贝过去   直接把buysbox拷贝过去就可以


# systemd  在7和8上  第一个进程就变成systemd

Systemd新特性

系统引导时实现服务并行启动

按需启动守护进程

自动化的服务依赖关系管理

同时采用socket式与D-Bus总线式激活服务

socket与服务程序分离

向后兼容sysv init脚本

使用systemctl 命令管理,systemctl命令固定不变,不可扩展,非由systemd启动的服务,systemctl无法与之通信和控制

系统状态快照

# systemd核心概念:unit

unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息

unit的类型

# [root@centos8 ~]#systemctl -t help      常用的就是前三个  剩下的就是系统自动

Available unit types:

service

socket

target

device

mount

automount

swap

timer

path

slice

scope

service unit: 文件扩展名为.service, 用于定义系统服务

Socket unit: .socket, 定义进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动

Target unit: 文件扩展名为.target,用于模拟实现运行级别

Device unit: .device, 用于定义内核识别的设备

Mount unit: .mount, 定义文件系统挂载点

Snapshot unit: .snapshot, 管理系统快照

Swap unit: .swap, 用于标识swap设备

Automount unit: .automount,文件系统的自动挂载点

Path unit: .path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务,如:spool 目录


# unit配置文件

/usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/

/lib/systemd/system: ubutun的对应目录

/run/systemd/system:系统执行过程中所产生的服务脚本,比上面目录优先运行

/etc/systemd/system:管理员建立的执行脚本,类似于/etc/rcN.d/Sxx的功能,比上面目录优先运行


# systemctl管理系统服务service unit    后边可以跟多个服务    

# 启动:相当于service name start

systemctl start name.service

# 停止:相当于service name stop      如果关闭一个服务他会对停止后的服务不能访问  对停止前的服务不影响  如ssh  不是所有服务都一样 只是个别服务

systemctl stop name.service

# 重启:相当于service name restart

systemctl restart name.service

# 查看状态:相当于service name status

systemctl status name.service

# 禁止自动和手动启动:

systemctl mask name.service

# 取消禁止

systemctl unmask name.service

# 查看某服务当前激活与否的状态:

systemctl is-active name.service

# 查看所有已经激活的服务:

systemctl list-units --type|-t service

# 查看所有服务:

systemctl list-units --type service --all|-a

# 设定某服务开机自启,相当于chkconfig name on

systemctl enable name.service

# 设定某服务开机禁止启动:相当于chkconfig name off

systemctl disable name.service

# 查看所有服务的开机自启状态,相当于chkconfig --list

systemctl list-unit-files --type service

# 用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list name

ls /etc/systemd/system/*.wants/name.service

# 查看服务是否开机自启:

systemctl is-enabled name.service

# 列出失败的服务

systemctl --failed --type=service

# 开机并立即启动或停止

systemctl enable --now postfix

systemctl disable --now postfix

# 查看服务的依赖关系:

systemctl list-dependencies name.service

# 杀掉进程:

systemctl kill unitname


# 显示服务   相当于centos6 chkconfig --list

systemctl list-unit-files --type service --all

1. loaded Unit配置文件已处理

2. active(running) 一次或多次持续处理的运行

3. active(exited) 成功完成一次性的配置

4. active(waiting) 运行中,等待一个事件

5. inactive 不运行

6. enabled 开机启动

7. disabled 开机不启动

8. static 开机不启动,但可被另一个启用的服务激活

9. indirect 重定向到别处


# systemctl命令示例

# 显示所有单元状态

systemctl 或 systemctl list-units

# 只显示服务单元的状态

systemctl --type=service

# 显示sshd服务单元

systemctl –l status sshd.service

# 验证sshd服务当前是否活动

systemctl is-active sshd

# 启动,停止和重启sshd服务

systemctl start sshd.service

systemctl stop sshd.service

systemctl restart sshd.service

# 重新加载配置

systemctl reload sshd.service

# 列出活动状态的所有服务单元

systemctl list-units --type=service

# 列出所有服务单元

systemctl list-units --type=service --all

# 查看服务单元的启用和禁用状态

systemctl list-unit-files --type=service

# 列出依赖的单元

systemctl list-dependencies sshd

验证sshd服务是否开机启动

systemctl is-enabled sshd

禁用network,使之不能自动启动,但手动可以

systemctl disable network

# 启用network

systemctl enable network

# 禁用network,使之不能手动或自动启动

systemctl mask network

# 启用network

systemctl unmask network


# Centos7 8 运行级别

target units:相当于CentOS 6之前的runlevel ,unit配置文件:.target

ls /usr/lib/systemd/system/*.target

systemctl list-unit-files --type target --all

# 对应的模式

0 ==> runlevel0.target, poweroff.target

1 ==> runlevel1.target, rescue.target

2 ==> runlevel2.target, multi-user.target

3 ==> runlevel3.target, multi-user.target

4 ==> runlevel4.target, multi-user.target

5 ==> runlevel5.target, graphical.target

6 ==> runlevel6.target, reboot.target

# 查看依赖性

systemctl list-dependencies graphical.target

# 级别切换:相当于 init N

systemctl isolate name.target

# 进入默认target

systemctl default

# 切换至字符模式

systemctl isolate multi-user.target

注意:只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切换(修改文件需执行systemctldaemon-reload才能生效)

# 修改默认级别:相当于修改 /etc/inittab

systemctl set-default name.target

# 切换至紧急救援模式:

systemctl rescue

# 切换至emergency模式:

systemctl emergency

说明:rescue.target 比emergency 支持更多的功能,例如日志等

传统命令init,poweroff,halt,reboot都成为 systemctl的软链接

# 关机

systemctl halt、systemctl poweroff

# 重启:

systemctl reboot

# 挂起:

systemctl suspend

# 休眠:

systemctl hibernate

# 休眠并挂起:

systemctl hybrid-sleep


# 范例:禁用ctrl+alt+delete 重启快捷键

[root@centos8 ~]#ls -l /lib/systemd/system/ctrl-alt-del.target

lrwxrwxrwx. 1 root root 13 May 23 2019 /lib/systemd/system/ctrl-alt-del.target-> reboot.target

[root@centos8 ~]#systemctl mask ctrl-alt-del.target

Created symlink /etc/systemd/system/ctrl-alt-del.target → /dev/null.

[root@centos8 ~]#init q

[root@centos8 ~]#systemctl daemon-reload


# centos7 版本以后启动引导顺序

1. UEFi或BIOS初始化,运行POST开机自检

2. 选择启动设备

3. 引导装载程序, centos7是grub2,加载装载程序的配置文件:

/etc/grub.d/

/etc/default/grub

/boot/grub2/grub.cfg

4. 加载initramfs驱动模块

5. 加载内核选项

6. 内核初始化,centos7使用systemd代替init

7. 执行initrd.target所有单元,包括挂载/etc/fstab

8. 从initramfs根文件系统切换到磁盘根目录

9. systemd执行默认target配置,配置文件/etc/systemd/system/default.target

10. systemd执行sysinit.target初始化系统及basic.target准备操作系统

11. systemd启动multi-user.target下的本机与服务器服务

12. systemd执行multi-user.target下的/etc/rc.d/rc.local

13. Systemd执行multi-user.target下的getty.target及登录服务

14. systemd执行graphical需要的服务


# 通过systemd-analyze 工具可以了解启动的详细过程

范例:

[root@centos8 ~]#systemd-analyze blame

1.862s kdump.service

1.047s tuned.service

666ms dracut-initqueue.service

523ms auditd.service

379ms initrd-switch-root.service

314ms sssd.service

302ms systemd-rfkill.service

219ms NetworkManager-wait-online.service

211ms polkit.service

178ms systemd-udev-trigger.service


范例:生成网页

systemd-analyze plot > boot.html    用sz boot.html 传到windows    需要安装lrzsz安装包


# 设置内核参数

设置内核参数,只影响当次启动

启动时,到启动菜单,按e键,找到在linux 开头的行后添加systemd.unit=desired.target

比如:systemd.unit=emergency.target 或 systemd.unit=rescue.target


# 破解 CentOS 7和8的 root 密码

1. 启动时任意键暂停启动

按e键进入编辑模式

将光标移动linux 开始的行,添加内核参数rd.break

按ctrl-x启动

mount –o remount,rw /sysroot

chroot /sysroot

passwd root

# 如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行

touch /.autorelabel

exit

reboot

2. 启动时任意键暂停启动

按e键进入编辑模式

将光标移动linux 开始的行,改为rw init=/sysroot/bin/sh

按ctrl-x启动

chroot /sysroot

passwd root

#如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行

touch /.autorelabel

exit

reboot


# 实现GRUB2安全

#添加grub密码

# [root@centos8 ~]#grub2-setpassword     会自动在当前目录生成一个user.cfg的文件   相当于centos6 上边在文件中添加密码

Enter password:

Confirm password:

# [root@centos8 ~]#ls -l /boot/grub2/

total 32

drwxr-xr-x 2 root root 4096 Jan 19 15:17 fonts

-rw-r--r-- 1 root root 5101 Jan 19 15:18 grub.cfg

-rw-r--r-- 1 root root 1024 Jan 19 15:18 grubenv

drwxr-xr-x 2 root root 12288 Jan 19 15:17 i386-pc

-rw------- 1 root root 298 Jan 19 18:20 user.cfg

# [root@centos8 ~]#ls -l /boot/grub2/user.cfg

-rw------- 1 root root 298 Jan 19 18:20 /boot/grub2/user.cfg

# [root@centos8 ~]#cat /boot/grub2/user.cfg

GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.60AAA29A65F4DC77E8861EF25BDE2034C9B30CE1

E07EE688D7F30460E7E87E7356B0893A6DFFB250B27D2EB9D3ED3E9207199C494D7882E2E8C772C8

2E2DDB7A.5E42FD69FA04293DECD68F077E83875A8E4572A7FBB89BA9F161B15EAFE54FBA963FE5D

52E16764944823396231803E5118DA1D9CAF3EB73C175A7D7A3682A90

# 清空grub密码

[root@centos8 ~]#cat /dev/null > /boot/grub2/user.cfg


# 修复grub2

GRUB2:CentOS 7,8及ubuntu1804都使用

引导提示时可以使用命令行界面,可从文件系统引导

主要配置文件:/boot/grub2/grub.cfg

修复配置文件:grub2-mkconfig > /boot/grub2/grub.cfg

# grub2-install /dev/sda #BIOS环境

# grub2-install #UEFI环境


# 实战案例1:centos 7 ,8 破坏MBR后进行恢复

dd if=/dev/zero of=/dev/sda bs=1 count=446

光盘进入救援模式

grub2-install --root-directory=/mnt/sysimage /dev/sda


# 实战案例2:entos 7 ,8删除/boot/grub2/*所有内容进行恢复

光盘进入救援模式

chroot /mnt/sysimage

grub2-install /dev/sda

grub2-mkconfig -o /boot/grub2/grub.cfg


# 实战案例3:CentOS 7 ,8 删除/boot/下所有文件后进行恢复

1. 光盘救援模式下安装grub2

特别说明:Centos8 必须先grub,再安装kernel,否则安装kernel-core时会提示grub出错

chroot /mnt/sysimage

mount /dev/sr0 /mnt

grub2-install /dev/sda

2. 安装Kernel

# CentOS 7

rpm –ivh /mnt/Packages/kernel-3.10.0-1062.el7.x86_64.rpm --force

# CentOS 8

rpm -ivh /mnt/BaseOS/Packages/kernel-core-4.18.0-147.el8.x86_64.rpm --force

3. 修复grub配置文件

生成grub2.cfg文件

grub2-mkconfig –o /boot/grub2/grub.cfg

4. 退出重启

exit

exit


# 编译安装最新版内核

# [root@centos8 ~]#yum -y install gcc make ncurses-devel flex bison openssl-devel elfutils-libelf-devel  bc perl  安装所依赖的包

# [root@centos8 ~]#tar xf linux-5.6.12.tar.xz -C /usr/local/src

# [root@centos8 ~]#cd /usr/local/src

# [root@centos8 ~]#cd /usr/src/linux

# [root@centos8 linux]#cp /boot/config-$(uname -r) ./.config

# [root@centos8 linux]#vim .config

# 修改下面两行,CentOS7无需修改

# CONFIG_MODULE_SIG is not set   吧这一行注释掉

CONFIG_SYSTEM_TRUSTED_KEYS=""    把引号里的东西删掉

# [root@centos8 linux]#make menuconfig

# [root@centos8 linux]#time make -j 2   安装

# 或者两步实现:make -j 2 bzImage ; make -j 2 modules

......

LD [M] sound/xen/snd_xen_front.ko

LD [M] virt/lib/irqbypass.ko

real 82m52.128s

user 133m37.982s

sys 25m46.311s

# [root@centos8 linux]#pwd

/usr/local/src/linux

# [root@centos8 linux]#du -sh .

15G .

# [root@centos8 linux]#make modules_install

# [root@centos8 linux]#ls /lib/modules

4.18.0-147.el8.x86_64 5.6.12-wanglinux-6.6.6

# [root@centos8 linux]#du -sh /lib/modules/5.6.12-wanglinux-6.6.6/

3.5G /lib/modules/5.6.12-wanglinux-6.6.6/

# [root@centos8 linux]#make install

# [root@centos8 linux]#ls /boot

config-4.18.0-147.el8.x86_64

efi

grub2

initramfs-0-rescue-5b85fc7444b240a992c42ce2a9f65db5.img

initramfs-4.18.0-147.el8.x86_64.img

initramfs-4.18.0-147.el8.x86_64kdump.img

initramfs-5.6.12-wanglinux-6.6.6.img

loader

lost+found

System.map

System.map-4.18.0-147.el8.x86_64

System.map-5.6.12-wanglinux-6.6.6

vmlinuz

vmlinuz-0-rescue-5b85fc7444b240a992c42ce2a9f65db5

vmlinuz-4.18.0-147.el8.x86_64

vmlinuz-5.6.12-wanglinux-6.6.6

# [root@centos8 ~]#ls /boot/loader/entries/

5b85fc7444b240a992c42ce2a9f65db5-0-rescue.conf

# reboot 重启  选最新版的内核启动