综合练习--第十周

1、编写脚本,接受二个位置参数,magedu和/www,判断系统是否有magedu,如果没有则自动创建magedu用户,并自动设置家目录为/www

位置变量:

  • $1, $2, … 对应第1个、第2个等参数,超过9个就要用{}包起来。如第10个参数,${10}
  • $0 命令本身,包括路径
  • $* 传递给脚本的所有参数,全部参数合为一个字符串
  • $@ 传递给脚本的所有参数,每个参数为独立字符串
  • $# 传递给脚本的参数的个数
[root@centos8 scripts]#vim create_user.sh
#!/bin/bash
if [ $# -ne 2 ];then
	echo "请输入两个位置变量,第一位为需创建的用户,第二位为其家目录"
	exit
fi
id $1 &> /dev/null
if [ $? -ne 0 ]; then
	useradd -d /$2 $1
	echo "用户$1创建成功,其家目录是$2"
else
	echo "用户$1已存在"
fi
[root@centos8 scripts]#bash -n create_user.sh   ##语法校验

##测试
[root@centos8 scripts]#bash  create_user.sh magedu www 
用户magedu创建成功,其家目录是www
[root@centos8 scripts]#getent passwd magedu
magedu:x:1001:1001::/www:/bin/bash
[root@centos8 scripts]#bash create_user.sh magedu
请输入两个位置变量,第一位为需创建的用户,第二位为其家目录
[root@centos8 scripts]#bash create_user.sh magedu www
用户magedu已存在

2、使用expect实现自动登录系统。

expect来自expect安装包,使用该命令需要确保已安装包文件

[root@centos8 scripts]#yum info expect
Last metadata expiration check: 1:07:10 ago on Mon 25 Jan 2021 09:53:54 AM CST.
Installed Packages
Name         : expect
Version      : 5.45.4
Release      : 5.el8
Architecture : x86_64
Size         : 578 k
Source       : expect-5.45.4-5.el8.src.rpm
Repository   : @System
From repo    : base
Summary      : A program-script interaction and testing utility
URL          : https://core.tcl.tk/expect/index
License      : Public Domain
Description  : Expect is a tcl application for automating and testing
             : interactive applications such as telnet, ftp, passwd, fsck,
             : rlogin, tip, etc. Expect makes it easy for a script to
             : control another program and interact with it.
             : 
             : This package contains expect and some scripts that use it.
[root@centos8 scripts]#cat ssh_expect 
#!/usr/bin/expect
set ip  10.0.0.7
set user root
set password admin123
set timeout 10
spawn ssh $user@$ip
expect {
    
    
	"yes/no" {
    
     send "yes\n";exp_continue }
	"password" {
    
     send "$password\n" }

}
interact

expect中相关命令

  • spawn 启动新的进程
  • expect 从进程接收字符串
  • send 用于向进程发送字符串
  • interact 允许用户交互
  • exp_continue 匹配多个字符串在执行动作后加此命令

3、简述linux操作系统启动流程 *

centos6启动流程

  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控制主机

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需要的服务

4、破解centos7 密码。

方法1:

  1. 启动时任意键暂停启动
  2. 按e键进入编辑模式
  3. 将光标移动linux16 开始的行,添加内核参数rd.break
  4. 按ctrl-x启动
  5. mount –o remount,rw /sysroot
  6. chroot /sysroot
  7. passwd root
  8. exit
  9. reboot

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

  1. touch /.autorelabel

在这里插入图片描述

方法2:光盘救援模式

  1. 启动时快速按Esc进入光盘救援模式
  2. 进入troubleshooting
  3. 进入rescue模式
  4. 按1continue
  5. chroot /mnt/sysimage 进入真实根目录
  6. passwd root
  7. exit
  8. exit

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_50904580/article/details/111627910