@linux系统启动优化及网络调试(system)

1|0

1|01. 开机启动流程

1. 按下电源 2. BIOS自检 3. MBR引导 4. 进入GRUB菜单 5. 加载内核 Kernel 6. systemd init进程 7. 读取运行级别 [root@qls ~]# ll /etc/systemd/system/default.target lrwxrwxrwx 1 root root 37 Jul 6 02:16 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target [root@qls ~]# ll /usr/lib/systemd/system/runlevel*.target lrwxrwxrwx. 1 root root 15 Jul 6 02:14 /usr/lib/systemd/system/runlevel0.target -> poweroff.target lrwxrwxrwx. 1 root root 13 Jul 6 02:14 /usr/lib/systemd/system/runlevel1.target -> rescue.target lrwxrwxrwx. 1 root root 17 Jul 6 02:14 /usr/lib/systemd/system/runlevel2.target -> multi-user.target lrwxrwxrwx. 1 root root 17 Jul 6 02:14 /usr/lib/systemd/system/runlevel3.target -> multi-user.target lrwxrwxrwx. 1 root root 17 Jul 6 02:14 /usr/lib/systemd/system/runlevel4.target -> multi-user.target lrwxrwxrwx. 1 root root 16 Jul 6 02:14 /usr/lib/systemd/system/runlevel5.target -> graphical.target lrwxrwxrwx. 1 root root 13 Jul 6 02:14 /usr/lib/systemd/system/runlevel6.target -> reboot.target 8. 初始化系统 [root@qls ~]# ll /usr/lib/systemd/system/sysinit.target -rw-r--r--. 1 root root 518 Oct 31 2018 /usr/lib/systemd/system/sysinit.target 9. 并行启动开机自启动的服务 串行启动 10. 运行getty文件,显示登陆界面 [root@qls ~]# ll /usr/lib/systemd/system/getty.target -rw-r--r--. 1 root root 460 Oct 31 2018 /usr/lib/systemd/system/getty.target Centos-6 和 Centos-7的区别 init管理进程 systemd管理进程 串行启动服务 并行启动服务 启动效率慢,容易故障 启动效率高,不容易出现故障

1|02. 系统运行级别

[root@qls ~]# ll /usr/lib/systemd/system/runlevel*.target lrwxrwxrwx. 1 root root 15 Jul 6 02:14 /usr/lib/systemd/system/runlevel0.target -> poweroff.target lrwxrwxrwx. 1 root root 13 Jul 6 02:14 /usr/lib/systemd/system/runlevel1.target -> rescue.target lrwxrwxrwx. 1 root root 17 Jul 6 02:14 /usr/lib/systemd/system/runlevel2.target -> multi-user.target lrwxrwxrwx. 1 root root 17 Jul 6 02:14 /usr/lib/systemd/system/runlevel3.target -> multi-user.target lrwxrwxrwx. 1 root root 17 Jul 6 02:14 /usr/lib/systemd/system/runlevel4.target -> multi-user.target lrwxrwxrwx. 1 root root 16 Jul 6 02:14 /usr/lib/systemd/system/runlevel5.target -> graphical.target lrwxrwxrwx. 1 root root 13 Jul 6 02:14 /usr/lib/systemd/system/runlevel6.target -> reboot.target 0 poweroff #关机 1 rescue #单用户模式 2 multi-user #多用户模式 3 multi-user #多用户模式 4 multi-user #多用户模式 5 graphical #图形界面模式 桌面模式 6 reboot #重启 #如何查看系统运行级别 Centos-6 [root@qls ~]# runlevel N 3 [root@qls ~]# init 5 [root@qls ~]# runlevel 3 5 [root@qls ~]# init 3 Centos-7 [root@qls ~]# systemctl get-default multi-user.target #修改运行级别 [root@qls ~]# systemctl set-default graphical.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target. [root@qls ~]# systemctl get-default graphical.target [root@qls ~]# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target. [root@qls ~]# systemctl get-default multi-user.target

1|03. system介绍

systemd system daemon #主配置文件目录 [root@qls ~]# ll /etc/systemd/system/ -d drwxr-xr-x. 12 root root 4096 Aug 5 09:05 /etc/systemd/system/ #系统服务配置文件 [root@qls ~]# ll /usr/lib/systemd/system/ -d drwxr-xr-x. 26 root root 12288 Jul 30 19:51 /usr/lib/systemd/system/ #程序,进程运行时产生的一些文件 [root@qls ~]# ll /run/systemd/system/ -d drwxr-xr-x 5 root root 160 Aug 5 09:30 /run/systemd/system/ #启动 [root@qls ~]# systemctl start nginx #停止 [root@qls ~]# systemctl stop nginx #状态 [root@qls ~]# systemctl status nginx #重启 [root@qls ~]# systemctl restart nginx #平滑重启 [root@qls ~]# systemctl reload nginx #禁用某个服务 [root@qls ~]# systemctl mask nginx #解除禁用 [root@qls ~]# systemctl unmask nginx #开机自启动 [root@qls ~]# systemctl enable nginx #取消开机自启动 [root@qls ~]# systemctl disable nginx status状态 active (running) #运行 Active: inactive (dead) #不在运行 disabled #开机不自启 enabled #开机自启 failed #失败 #显示当前运行级别下开机自启动的服务 [root@qls ~]# systemctl list-unit-files

1|04. system设置

#使用system进行管理关机重启操作 [root@qls ~]# systemctl reboot [root@qls ~]# systemctl poweroff #如何设置主机名 [root@qls ~]# hostname qls [root@qls ~]# hostnamectl Static hostname: qls Icon name: computer-vm Chassis: vm Machine ID: 6b11e94f1fe04eb89579c1c41f6f5307 Boot ID: 783be43c5a44420ead4bd26792a6d22c Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86-64 [root@qls ~]# hostnamectl set-hostname web01 [root@qls ~]# logout Connection closing...Socket close. Connection closed by foreign host. Disconnected from remote host(虚拟机-10.0.0.100) at 09:54:12. Type `help' to learn how to use Xshell prompt. [C:\~]$ Connecting to 10.0.0.100:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Wed Aug 5 17:51:59 2020 from 10.0.0.1 [root@web01 ~]# [root@web01 ~]# cat /etc/hostname web01 Centos-6修改 临时生效 [root@web01 ~]# hostname qls [root@web01 ~]# logout Connection closing...Socket close. Connection closed by foreign host. Disconnected from remote host(虚拟机-10.0.0.100) at 09:55:10. Type `help' to learn how to use Xshell prompt. [C:\~]$ Connecting to 10.0.0.100:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Wed Aug 5 09:54:16 2020 from 10.0.0.1 [root@qls ~]# cat /etc/hostname web01 #如何修改字符集 [root@qls ~]# echo $LANG en_US.UTF-8 [root@qls ~]# localectl set-locale LANG=zh_CN.UTF-8 [root@qls ~]# logout Connection closing...Socket close. Connection closed by foreign host. Disconnected from remote host(虚拟机-10.0.0.100) at 09:57:21. Type `help' to learn how to use Xshell prompt. [C:\~]$ Connecting to 10.0.0.100:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Wed Aug 5 09:55:13 2020 from 10.0.0.1 [root@qls ~]# [root@qls ~]# cat /etc/locale.conf LANG=zh_CN.UTF-8 [root@qls ~]# yum install -y sl 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 软件包 sl-5.02-1.el7.x86_64 已安装并且是最新版本 无须任何处理 #临时修改变量 [root@qls ~]# export LANG=en_US.UTF-8 [root@qls ~]# echo $LANG en_US.UTF-8 #系统日志管理 [root@qls ~]# journalctl -xe #查看当前服务的日志 [root@qls ~]# tail /var/log/messages Aug 5 10:01:32 qls systemd: Stopped The nginx HTTP and reverse proxy server. Aug 5 10:01:32 qls systemd: Starting The nginx HTTP and reverse proxy server... Aug 5 10:01:32 qls nginx: nginx: [emerg] unknown directive "isten" in /etc/nginx/nginx.conf:39 Aug 5 10:01:32 qls nginx: nginx: configuration file /etc/nginx/nginx.conf test failed Aug 5 10:01:32 qls systemd: nginx.service: control process exited, code=exited status=1 Aug 5 10:01:32 qls systemd: Failed to start The nginx HTTP and reverse proxy server. Aug 5 10:01:32 qls systemd: Unit nginx.service entered failed state. Aug 5 10:01:32 qls systemd: nginx.service failed. Aug 5 10:02:01 qls systemd: Started Session 19 of user root. Aug 5 10:03:01 qls systemd: Started Session 20 of user root. #查看日志的最后20行内容 [root@qls ~]# journalctl -n 20 #实时查看日志的输出 [root@qls ~]# journalctl -f #显示日志的级别日志 [root@qls ~]# journalctl -p alert crit debug emerg err info notice warning [root@qls ~]# journalctl -p err -- Logs begin at Wed 2020-08-05 17:51:37 CST, end at Wed 2020-08-05 10:05:01 CST. -- Aug 05 17:51:41 qls kernel: sd 2:0:0:0: [sda] Assuming drive cache: write through Aug 05 17:51:41 qls kernel: sd 2:0:1:0: [sdb] Assuming drive cache: write through Aug 05 17:51:41 qls kernel: sd 2:0:2:0: [sdc] Assuming drive cache: write through Aug 05 17:51:44 qls kernel: piix4_smbus 0000:00:07.3: SMBus Host Controller not enabled! Aug 05 09:54:13 web01 sshd[7162]: error: Received disconnect from 10.0.0.1 port 50011:0: Aug 05 09:55:11 qls sshd[7333]: error: Received disconnect from 10.0.0.1 port 50033:0: Aug 05 09:57:22 qls sshd[7369]: error: Received disconnect from 10.0.0.1 port 50041:0: Aug 05 09:59:29 qls sshd[7419]: error: Received disconnect from 10.0.0.1 port 50067:0: Aug 05 10:00:51 qls sshd[7473]: error: Received disconnect from 10.0.0.1 port 50097:0: Aug 05 10:01:32 qls systemd[1]: Failed to start The nginx HTTP and reverse proxy server. #只查看某个服务的日志 [root@qls ~]# journalctl -u sshd -- Logs begin at Wed 2020-08-05 17:51:37 CST, end at Wed 2020-08-05 10:06:01 CST. -- Aug 05 17:51:56 qls systemd[1]: Starting OpenSSH server daemon... Aug 05 17:51:56 qls sshd[6981]: Server listening on 0.0.0.0 port 22. Aug 05 17:51:56 qls sshd[6981]: Server listening on :: port 22. Aug 05 17:51:56 qls systemd[1]: Started OpenSSH server daemon. Aug 05 17:51:59 qls sshd[7162]: Accepted password for root from 10.0.0.1 port 50011 ssh2 Aug 05 09:54:16 web01 sshd[7333]: Accepted password for root from 10.0.0.1 port 50033 ssh2 Aug 05 09:55:12 qls sshd[7369]: Accepted password for root from 10.0.0.1 port 50041 ssh2 Aug 05 09:57:26 qls sshd[7419]: Accepted password for root from 10.0.0.1 port 50067 ssh2 Aug 05 09:59:31 qls sshd[7473]: Accepted password for root from 10.0.0.1 port 50097 ssh2 Aug 05 10:00:52 qls sshd[7525]: Accepted password for root from 10.0.0.1 port 50111 ssh2

1|05. 单用户模式

忘记root密码 开机在grub界面输入 e 在linux16开头的行的最后面输入 init=/bin/bash enforcing=0 按 ctrl键 + x 重新挂载根分区 mount -o rw,remount / 修改密码 重启 exec /sbin/init #运行级别被设置为重启 [root@web01 ~]# systemctl set-default reboot.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/reboot.target. [root@web01 ~]# systemctl get-default reboot.target 开机在grub界面输入 e 在linux16开头的行的最后面输入 rd.break 按 ctrl键 + x 重新挂载根分区 mount -o rw,remount /sysroot 赋予权限 chroot /sysroot 修改运行级别 exit reboot

1|06. 救援模式

#系统损坏了,数据很重要 需要把数据拷贝出来 #MBR引导坏掉 dd if=/dev/zero of=/dev/sda bs=1 count=446 #修复MBR grub2-install /dev/sda grub2-mkconfig -o /boot/grub2/grub.cfg

1|07. 网卡命名规则

biosdevname=0 net.ifnames=1 设置为0 规则1:如果Firmware或者BIOS提供的设备索引信息可用就用此命名。比如eno1。否则使用规则2 规则2:如果Firmware或Bios的PCI-E扩展插槽可用就用此命名。比如ens1,否则使用规则3 规则3:如果硬件接口的位置信息可用就用此命名。比如enp2s0 规则4:根据MAC地址命名,比如enx7d3e9f。默认不开启。 规则5:上述均不可用时回归传统命名方式 上面的所有命名规则需要依赖于一个安装包:biosdevname #命令行修改网卡命名规则 mv /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-eth0 NAME=ens33 DEVICE=ens33 修改 NAME=eth0 DEVICE=eth0 grub2-mkconfig -o /boot/grub2/grub.cfg reboot

1|08. 系统优化

[root@qls ~]# ifconfig eth1 172.16.1.100/24 [root@qls ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1 [root@qls ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE=Ethernet BOOTPROTO=none NAME=eth1 DEVICE=eth1 ONBOOT=yes IPADDR=172.16.1.100 PREFIX=24 #更新源 [root@qls ~]# rm -rf /etc/yum.repos.d/* [root@qls ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo [root@qls ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@qls ~]# ll /etc/yum.repos.d/ total 8 -rw-r--r--. 1 root root 2523 Aug 5 20:09 CentOS-Base.repo -rw-r--r--. 1 root root 664 Aug 5 20:09 epel.repo #关闭firewalld及selinux [root@qls ~]# systemctl stop firewalld [root@qls ~]# systemctl disable firewalld [root@qls ~]# setenforce 0 [root@qls ~]# vi /etc/sysconfig/selinux [root@qls ~]# grep 'SELINUX' /etc/sysconfig/selinux # SELINUX= can take one of these three values: SELINUX=disabled #关闭NetworkManager网络 [root@qls ~]# systemctl stop NetworkManager [root@qls ~]# systemctl disable NetworkManager #同步时间 [root@qls ~]# crontab -l * * * * * /usr/bin/ntpdate ntp.aliyun.com &> /dev/null #配置文件介绍 <domain> <type> <item> <value> <domain>表示要限制的用户 <type>设定类型 <item>表示可选的资源 <value>表示要限制的值 #加大文件描述符 [root@qls ~]# echo '* - nofile 65535 ' >>/etc/security/limits.conf #检查结果 [root@qls ~]# tail -1 /etc/security/limits.conf

__EOF__

本文作者ଲ小何才露煎煎饺
本文链接https://www.cnblogs.com/zeny/p/15121628.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   ଲ小何才露煎煎饺  阅读(14)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示