系统启动与基础命令

张开发
2026/4/13 12:40:22 15 分钟阅读

分享文章

系统启动与基础命令
系统启动原理启动流程计算机接通电源后系统固件UEFI 或传统 BIOS首先执行开机自检POST检测 CPU、内存、硬盘等核心硬件是否正常。-配置说明启动早期按特定快捷键如 F2可进入固件配置界面调整启动顺序、硬件参数等。固件按配置的启动顺序搜索启动设备读取磁盘主启动记录MBR中的引导加载器CentOS 7 默认为 GRUB2并将系统控制权移交 GRUB2。-配置说明通过grub2-install命令可将 GRUB2 安装为磁盘默认启动加载器。GRUB2 读取/boot/grub2/grub.cfg配置文件显示操作系统选择菜单。-配置说明不可直接编辑grub.cfg需通过修改/etc/grub.d/目录下的脚本、/etc/default/grub配置文件再执行grub2-mkconfig命令生成新的grub.cfg。引导加载器根据选中的启动项从磁盘加载内核vmlinuz和 initramfs 到内存并将内核参数、initramfs 内存地址传递给内核。- initramfs 是临时内存文件系统包含启动所需的硬件驱动、初始化脚本等内核通过它完成硬件初始化。-配置说明通过/etc/dracut.conf.d/目录、dracut命令生成 initramfslsinitrd命令可查看 initramfs 内容。initramfs 执行/sbin/initCentOS 7 中该文件为 systemd 的软链接作为系统首个进程PID 1。-配置说明可通过内核参数initcommand指定自定义初始化程序。systemd 加载内核命令行指定的 target或系统默认的default.target通常为文本/图形登录界面。-配置说明通过systemctl命令设置默认 target如systemctl set-default multi-user.target。default.target依赖sysinit.target该 target 完成系统基础初始化读取/etc/fstab挂载文件系统、激活日志服务systemd-journald等。-配置说明通过/etc/fstab配置文件设置文件系统开机自动挂载规则。default.target激活所有配置为开机自启的 systemd 单元如服务、定时器等。-配置说明通过systemctl enable 服务名设置服务开机自启。default.target激活getty.target打开 tty1 终端提供用户登录入口。使用本地ISO仓库安装图形化关联ISO镜像2.挂载光盘。[rootcentos ~19:31:58]# lsblk /dev/sr0NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr011:014.4G0rom[rootcentos ~19:32:12]# mkdir /dvd[rootcentos ~19:32:17]# mount /dev/sr0 /dvdmount: /dev/sr0 is write-protected, mounting read-only[rootcentos ~19:32:24]# ls /dvdCentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7 EFI images Packages RPM-GPG-KEY-CentOS-Testing-7 EULA isolinux repodata TRANS.TBL3.配置仓库指向本地/dvd目录# 备份原有仓库[rootcentos ~19:32:31]# mkdir /etc/yum.repos.d/bak[rootcentos ~19:32:39]# mv /etc/yum.repos.d/*repo /etc/yum.repos.d/bak# 创建本地仓库[rootcentos ~19:32:47]# vim /etc/yum.repos.d/dvd.repo[dvd]nameCentOS-7 dvdbaseurlfile:///dvdgpgcheck0enabled14.安装图形化环境组[rootcentos ~19:34:47]# yum remove -y open-vm-tools[rootcentos ~19:35:55]# yum groupinstall -y Server with GUI --skip-broken系统 target 机制目录和子目录、子文件之间关系目录包含子目录和子文件。service一个服务例如sshd提供远程执行命令、复制文件。target 是一个可以包含service、time等对象的特殊unit。systemctl start target激活该target包含的所有对象。systemd 以 target 类型单元对各类系统单元服务、挂载、设备等进行分组管理target 可嵌套依赖形成层级化的启动逻辑。传统 SysVinit 定义 7 个运行级别0-6CentOS 7 虽使用 systemd但仍保留对运行级别的兼容映射具体如下运行级别对应 systemd target作用描述0-关机halt终止所有进程并关闭电源对应命令shutdown -h now。1emergency.target/rescue.target单用户模式仅 root 可登录无网络服务用于系统修复如密码找回。2-多用户模式无 NFS支持多用户登录但不启动网络文件系统部分发行版与级别 3 功能一致。3multi-user.target完全多用户模式文本界面启动所有网络服务命令行登录无图形界面。4-预留级别默认未使用可自定义用途。5graphical.target图形化多用户模式在级别 3 基础上启动图形界面GNOME/KDE图形登录。6-重启reboot终止所有进程并重启对应命令shutdown -r now。切换当前系统的运行target#切换为文本界面[rootcentos ~13:31:33]# systemctl isolate multi-user.target#切换为图像界面[rootcentos ~13:35:33]# systemctl isolate graphical.targetROOT 密码重置重启系统中断 GRUB2 菜单倒计时选中第一个内核条目并按e编辑。定位到linux16开头的行行尾添加rd.break在 initramfs 移交控制权前中断按Ctrlx启动。系统进入 root shell此时根文件系统/sysroot为只读挂载重新挂载为读写bashswitch_root:/# mount -o rw,remount /sysroot切换根目录到 /sysroot实际系统根目录shswitch_root:/# chroot /sysroot设置新密码bashsh-4.2#passwd退出 chroot 环境并继续启动bash sh-4.2# exit switch_root:/# exitSELinuxfirewalld 管理网络数据包。Secure Enhance Linux给每个文件都贴上一个标签。应用程序httpd打标签A提供/usr/share/nginx/html目录给客户端/usr/share/nginx/html目录也打上标签A。生活场景门禁卡 702 打标签 A - 702 打标签A。Shell 特殊变量SHELL位置参数变量Shell 进程中的特殊状态变量$?作用获取执行上一个指令的执行状态返回值0为成功非零为失败这个变量最常用。[laomashell ~]$lshello script.sh showargs.sh[laomashell ~]$echo$?0[laomashell ~]$ls/root ls: 无法打开目录/root: 权限不够[laomashell ~]$echo$?2# man ls查看退出码含义[laomashell ~]$manls......Exit status:0ifOK,1ifminor problems(e.g., cannot access subdirectory),2ifserious trouble(e.g., cannot access command-line argument).Shell 内置变量命令bash Shell 包含一些内置命令。 这些内置命令在目录列表里是看不见的它们由 Shell 本身提供。常用的内部命令有echo、eval、exec、read、shift等。下面简单介绍几个最常用的内置命令的格式和功能。echoecho命令参数选项-n不换行输出内容。-e解析转义字符见下面的字符转义字符:\n换行。\t制表符tab)。\b退格。[rootcentos bin15:49:53]# echo -n hello worldhello world[rootcentos bin15:52:50][rootcentos bin15:56:45]# echo -en hell\nihell i[rootcentos bin15:59:04]#[rootcentos bin15:54:06]# echo -e hello\tworldhello world[rootcentos bin15:54:45]# echo -n hello ;echo -e \rworldworldread案例开发set_pass设置密码例如set_pass laoma提示用户输入密码然后设置为相应密码。[rootcentos bin16:13:27]# vim set_pass#!/bin/bashread-s-p请输入用户$1新密码:passwordecho$password|passwd--stdin$1[rootcentos bin16:14:49]# chmod x /root/bin/set_pass[rootcentos bin16:15:10]# set_pass cyh请输入用户 cyh 新密码:Changing passwordforuser cyh. passwd: all authentication tokens updated successfully.案例开发工具my_select模拟tzselect不需要时间判断。[rootcentos bin16:23:14]# vim my_select[rootcentos bin16:23:37]# chmod x my_select[rootcentos bin16:24:10]# my_selectmy_select: line13: o:commandnot found#? 5Pleaseselecta country.1)Afghanistan18)Israel35)Palestine2)Armenia19)Japan36)Philippines3)Azerbaijan20)Jordan37)Qatar4)Bahrain21)Kazakhstan38)Russia5)Bangladesh22)Korea(North)39)Saudi Arabia6)Bhutan23)Korea(South)40)Singapore7)Brunei24)Kuwait41)Sri Lanka8)Cambodia25)Kyrgyzstan42)Syria9)China26)Laos43)Taiwan10)Cyprus27)Lebanon44)Tajikistan11)East Timor28)Macau45)Thailand12)Georgia29)Malaysia46)Turkmenistan13)Hong Kong30)Mongolia47)United Arab Emirates14)India31)Myanmar(Burma)48)Uzbekistan15)Indonesia32)Nepal49)Vietnam16)Iran33)Oman50)Yemen17)Iraq34)Pakistan#? 6Pleaseselectone of the followingtimezone regions.1)Beijing Time2)Xinjiang Time#? 8The following information has been given: China Beijing Time ThereforeTZAsia/Shanghai will be used. Localtimeis now: Fri Apr1016:16:09 CST2026. Universal Time is now: Fri Apr1008:16:09 UTC2026. Is the above information OK?1)Yes2)No#? 1You canmakethis change permanentforyourself by appending the lineTZAsia/Shanghai;exportTZ to thefile.profileinyour home directory;thenlog out and loginagain. Here is that TZ value again, thistimeon standard output so that you can use the /usr/bin/tzselectcommandinshell scripts: Asia/Shanghai数值计算下面就给大家介绍一下常见的 Shell 算术运算符、-一元正号和负号。、-加法和减法。*、/、乘法、除法、取余取模。**幂运算。、–增加及减少可前置也可放在变量结尾。!、、||逻辑非取反)、逻辑与and)、逻辑或or)。、、、比较符号小于、小于等于、大于、大于等于。、!、比较符号相等、不相等对于字符串也可以表示相当于。、向左移位、向右移位。~、|、、^按位取反、按位异或、按位与、按位。、、-、*、/、%赋值运算符例如a1相当于aa1a-1相当于aa-1。Shell 中常见的算术运算命令$(())用于整数运算的常用运算符效率很高。$[]用于整数运算。bcLinux下的一个计算器程序适合整数及小数运算。**示例1**简单的数值计算。[rootcentos ~16:49:19]# echo $((11))2[rootcentos ~16:51:35]# echo $((6*3))18[rootcentos ~16:52:02]# ((i5))[rootcentos ~16:52:17]# ((ii*2))[rootcentos ~16:52:24]# echo $i10**示例2**复杂的数值计算。[rootcentos ~16:52:31]# ((a12**3-4*3))[rootcentos ~16:53:22]# echo $a-3[rootcentos ~16:53:28]# b$((a12**3-4%3))[rootcentos ~16:53:59]# echo $b8[rootcentos ~16:54:04]# a$((100*(1001)/2))[rootcentos ~16:54:41]# echo $a5050**示例3**特殊运算符号[rootcentos ~16:54:45]# a8;echo $((a1))9[rootcentos ~16:55:54]# echo $((a**2))81**示例4**比较和判断[rootcentos ~16:55:58]# ((38))[rootcentos ~16:56:33]# echo $?0[rootcentos ~16:56:38]# echo $((38))1[rootcentos ~16:56:48]# ((38))[rootcentos ~16:57:11]# echo $?1[rootcentos ~16:57:17]# echo $((38))0[rootcentos ~16:58:11]# echo $((33))1[rootcentos ~16:58:27]# if ((87 55));then echo yes;fiyes**示例5**变量前后使用–和特殊运算符的表达式[rootcentos ~17:00:33]# echo $((a))10[rootcentos ~17:00:43]# echo $a11[rootcentos ~17:00:48]# echo $((--a))10[rootcentos ~17:00:58]# echo $a10**示例6**通过 (())运算后赋值给变量[rootcentos ~17:01:04]# num99[rootcentos ~17:02:39]# echo $((numm1))1[rootcentos ~17:02:50]# echo $((num1))100[rootcentos ~17:02:56]# num$((num1))[rootcentos ~17:03:07]# echo $num100let 命令let运算命令的语法格式为let 表达式let表达式的功能等同于((表达式))示例[rootcentos ~ 17:03:24]# ii8 [rootcentos ~ 17:03:29]# echo $i i8 [rootcentos ~ 17:03:33]# i2 [rootcentos ~ 17:03:46]# let ii8 [rootcentos ~ 17:03:52]# echo $i 10bc 命令bc是UNIX/Linux下的计算器因此除了可以作为计算器来使用还可以作为命令行计算工具使用。示例[rootcentos ~17:03:55]# bcbc1.06.95 Copyright1991-1994,1997,1998,2000,2004,2006Free Software Foundation, Inc. This isfreesoftware with ABSOLUTELY NO WARRANTY. For detailstypewarranty.13*4-6/3^3%413scale41/3 .3333 quit综合案例通过一条命令计算输出123...10的表达式并计算出结果请使用bc命令计算。输出内容如1234567891055。[rootcentos ~16:44:45]# echo {1..10}12345678910[rootcentos ~16:48:09]# echo {1..10} | tr 12345678910[rootcentos ~16:48:21]# echo Today is $(date %A)Today is Friday[rootcentos ~16:48:52]# echo $[123]15[rootcentos ~16:49:11]# echo $[ $(echo {1..10} | tr ) ]55quit## 综合案例 通过一条命令计算输出 123...10 的表达式并计算出结果请使用bc命令计算。输出内容如1234567891055。 ~~~bash [rootcentos ~ 16:44:45]# echo {1..10} 1 2 3 4 5 6 7 8 9 10 [rootcentos ~ 16:48:09]# echo {1..10} | tr 12345678910 [rootcentos ~ 16:48:21]# echo Today is $(date %A) Today is Friday [rootcentos ~ 16:48:52]# echo $[123] 15 [rootcentos ~ 16:49:11]# echo $[ $(echo {1..10} | tr ) ] 55

更多文章