linux 系统管理

开机自启动流程

1. 按下电源
2. BIOS自检  
3. MBR引导  
4. 进入GRUB菜单   
5. 加载内核 Kernel
6. systemd      init进程
7. 读取运行级别   
8. 运行初始化系统文件
9. 并行启动系统开机自启动的服务
10. 运行getty文件,显示登陆界面

# 系统启动默认配置文件
[root@localhost ~]# ll /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 41 Aug  5 10:58 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target

# 系统启动等级文件
[root@localhost ~]# ll /usr/lib/systemd/system/runlevel*.target
lrwxrwxrwx. 1 root root 15 Jul  5 00:11 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Jul  5 00:11 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Jul  5 00:11 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Jul  5 00:11 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Jul  5 00:11 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Jul  5 00:11 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Jul  5 00:11 /usr/lib/systemd/system/runlevel6.target -> reboot.target

# 系统初始化文件
[root@localhost ~]# ll /usr/lib/systemd/system/sysinit.target
-rw-r--r--. 1 root root 518 Oct 31  2018 /usr/lib/systemd/system/sysinit.target

# getty文件
[root@localhost ~]# 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管理进程
串行启动服务	           并行启动服务
启动效率慢,容易故障	   启动效率高,不容易出现故障 

系统运行级别

[root@localhost ~]# ll /usr/lib/systemd/system/runlevel*.target
lrwxrwxrwx. 1 root root 15 Jul  5 00:11 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Jul  5 00:11 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Jul  5 00:11 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Jul  5 00:11 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Jul  5 00:11 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Jul  5 00:11 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Jul  5 00:11 /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@localhost ~]# runlevel 
N 3
[root@localhost ~]# init 5
[root@localhost ~]# runlevel
3 5
[root@localhost ~]# init 3
[root@localhost ~]# runlenve
[root@localhost ~]# runlevel
5 3

# centos-7
[root@localhost ~]# systemctl get-default 
multi-user.target

# 修改运行级别
[root@localhost ~]# 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@localhost ~]# systemctl get-default 
graphical.target
[root@localhost ~]# 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@localhost ~]# systemctl get-default
multi-user.target

system系统调用命令

systemd 全称 system daemon 表示 守护系统进程

# 主配置文件目录
[root@localhost ~]# ll /etc/systemd/system/ -d
drwxr-xr-x. 12 root root 4096 Aug  5 15:21 /etc/systemd/system/

# 系统服务配置文件目录
[root@localhost ~]# ll /etc/systemd/system/ -d
drwxr-xr-x. 12 root root 4096 Aug  5 15:21 /etc/systemd/system/

# 程序,进程运行时产生的一些文件目录
[root@localhost ~]# ll /run/systemd/system/ -d
drwxr-xr-x 5 root root 160 Aug  5 15:01 /run/systemd/system/

systemctl命令选项:
restart      # 重新启动(先关闭在启动id会变) 示例:systemctl restart nginx      
start        # 开始启动 示例:systemctl start nginx   
status       # 服务状态 示例:systemctl status nginx
stop         # 服务停止 示例:systemctl stop nginx
reload       # 平滑重启(id不变 重新加载服务配置文件) 示例:systemctl reload nginx
mask         # 禁用某个服务 示例:systemctl mask nginx
unmask       # 解除某个禁用 示例:systemctl unmask nginx
enable       # 设置成开启自启动 示例:systemctl enable nginx
disable      # 取消开机自启动 示例:systemctl disable nginx

# status 状态说明
active (running)	    # 运行 
Active: inactive (dead)	    # 不在运行
disabled		    # 开机不自启
enabled			    # 开机自启
failed			    # 失败

# 显示当前运行级别下开机自启动的服务
systemctl list-unit-files
[root@localhost ~]# systemctl list-unit-files
UNIT FILE                                     STATE      # 单元文件状态  
proc-sys-fs-binfmt_misc.automount             static     # 静态的 不能作为开机自启动
tmp.mount                                     disabled   # 禁止开机自启动
auditd.service                                enabl      # 开机自启动的

hostname systemctl设置

# 使用systemctl进行关机重启操作
systemctl reboot    # 重启
systemctl poweroff  # 关机

# 如何设置主机名
hostnaem                                # 查看主机名
hostnamectl                             # 查看主机详细信息
hostnamectl set-hostname admin.local    # 修改主机名重启生效(永久)

# 主机名配置文件
/etc/hostname

如何修改字符集

# 查看本机字符集
[root@localhost ~]# echo $LANG
en_CN.UTF-8

# 改主机字符集
[root@localhost ~]# localectl set-locale LANG=zh_CN.UTF-8  # 重启生效
[root@localhost ~]# echo $LANG
en_CN.UTF-8

# 字符集配置文件 /ect/locale.conf

# export临时修改变量
[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     # 系统的安全日志 

# 查看日志的最后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   -u  sshd

单用户模式

#  忘记root密码 
开机在grub界面输入 e
在linux16开头的行的最后面输入  init=/bin/bash   enforcing=0      
按 ctrl键  +  x  
重新挂载根分区     mount  -o   rw,remount   /
修改密码  echo 'new passwd' | passwd --stdinn root
重启   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
reboo

救援模式

# 系统损坏了,数据很重要    需要把数据拷贝出来 
# 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:如果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

简单系统优化



[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


posted @ 2020-08-05 16:46  nick_xm  阅读(104)  评论(0编辑  收藏  举报