Linux shell脚本编程及系统启动实践
Posted 1874
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux shell脚本编程及系统启动实践相关的知识,希望对你有一定的参考价值。
1、编写脚本,接受二个位置参数,magedu和/www,判断系统是否有magedu,如果没有则自动创建magedu用户,并自动设置家目录为/www
[root@test qiuhom]#cat checkuser.sh #!/bin/bash user=$1 [ $UID -ne 0 ] && echo "this script must root run it" && exit 3 [ $# -ne 2 ] && echo "usage bash $0 user homedir" && exit 4 if [[ $2 =~ ^(\\/.*) ]];then homedir=$2 else echo "input homedir must directory" exit 6 fi if getent passwd $user &> /dev/null ;then echo "$user is exist" exit 7 else useradd $user -m -d $homedir [ $? -eq 0 ] && echo $user is create successful fi [root@test qiuhom]#sh checkuser.sh tom usage bash checkuser.sh user homedir [root@test qiuhom]#sh checkuser.sh qiuhom /home/qiuhom qiuhom is exist [root@test qiuhom]#sh checkuser.sh tom tom input homedir must directory [root@test qiuhom]#sh checkuser.sh magedu /www magedu is create successful [root@test qiuhom]#getent passwd magedu magedu:x:1004:1004::/www:/bin/bash [root@test qiuhom]#ll /www/ -d drwx------ 3 magedu magedu 78 11月 26 22:13 /www/ [root@test qiuhom]#ll /www/ -a 总用量 16 drwx------ 3 magedu magedu 78 11月 26 22:13 . dr-xr-xr-x. 22 root root 4096 11月 26 22:13 .. -rw-r--r-- 1 magedu magedu 18 10月 31 2018 .bash_logout -rw-r--r-- 1 magedu magedu 193 10月 31 2018 .bash_profile -rw-r--r-- 1 magedu magedu 231 10月 31 2018 .bashrc drwxr-xr-x 4 magedu magedu 39 11月 3 14:33 .mozilla [root@test qiuhom]#
2、使用expect实现自动登录系统。
[root@ansible_centos6 ~]# ip a s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:41:ff:c0 brd ff:ff:ff:ff:ff:ff inet 192.168.0.128/24 brd 192.168.0.255 scope global eth1 inet6 fe80::20c:29ff:fe41:ffc0/64 scope link valid_lft forever preferred_lft forever [root@ansible_centos6 ~]# cat autologin.exp #!/usr/bin/expect set ip [lindex $argv 0] set user [lindex $argv 1] set port [lindex $argv 2] set passwd [lindex $argv 3] set timeout 10 spawn ssh $user@$ip -p $port expect { "yes/no" {send "yes\\n";exp_continue} "password" {send "$passwd\\n"} } interact [root@ansible_centos6 ~]# expect autologin.exp 192.168.0.99 qiuhom 41319 admin spawn ssh qiuhom@192.168.0.99 -p 41319 qiuhom@192.168.0.99\'s password: [qiuhom@test ~]$ip a s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:30:18:51:af:3c brd ff:ff:ff:ff:ff:ff inet 192.168.0.99/24 brd 192.168.0.255 scope global noprefixroute enp2s0 valid_lft forever preferred_lft forever inet 172.16.1.2/16 brd 172.16.255.255 scope global noprefixroute enp2s0:0 valid_lft forever preferred_lft forever inet6 fe80::230:18ff:fe51:af3c/64 scope link valid_lft forever preferred_lft forever 3: enp3s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000 link/ether 00:30:18:51:af:3d brd ff:ff:ff:ff:ff:ff 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:bd:55:ad:ff brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever [qiuhom@test ~]$
3、简述linux操作系统启动流程
此图片来自网络
4、破解centos7 密码。
方式一:
1)启动时按任意键停止启动,按e键进入编辑模式;
2)将光标移动到linux16开始的行,添加内核参数rd.break
3)按ctrl + x 启动
4)以只读方式重新挂载根;mount -o remount,rw /sysroot
说明:可以看到我们Ctrl + x 重新进入系统正在的根在/sysroot上,并且是只读挂载
5)切换根chroot /sysroot
6)更改密码 passwd root
7) touch /.autorelabel ,并退出当前切换后的根分区
说明:可以看到我们不退出当前切换后的根分区是不能重启的
8) 重新启动系统;reboot
方式二:
1)启动时按任意键暂停启动,并按e键进入编辑模式
2)将光标移动至linux16开始的行,并在末尾追加 rw init=/sysroot/bin/sh
3)Ctrl+x启动
说明:用以上启动方式,我们可以省略重新以读写方式挂载/sysroot,后续直接切换根,更改密码
4)切换根chroot /sysroot
5)更改root密码 passwd root
6)更新/.autorelabel文件的时间戳touch /.autorelabel (此文件的作用是下次重新启动它会重新标记selinux的文件系统)
7)exit 并重启
以上是关于Linux shell脚本编程及系统启动实践的主要内容,如果未能解决你的问题,请参考以下文章