Linux 第20天: (09月12日) 练习和作业
readlink 链接名 得出真实文件名
losetup -a 查看loop设备
vim /boot/grub/grub.conf
max_loop=100 修改loop设备数
password --md5 grub加上密码
r!grub-md5-crypt md5设置密码
grub-md5-crypt
openssl passwd -1 -salt "xxx"
getenforce 查看selinux状态
cat /proc/cmdline 查看所有生效的内核参数
grub菜单背景图修改
cp boot/grub/splash.xpm.gz /root
gzip -d splash.xpm.gz
file splash.xpm
more splash.xpm
yum list ImageMagick
yum -y install ImageMagick
convert -resize 640x480 -colors 14 win.jpb win.xpm
gzip win.xpm
cp win.xpm.gz /boot/grub/
vim /boot/grub/grub.conf
1、破解root口令,并为grub设置保护功能
破解root口令
重启按任意键进入grub界面, 按'e'进入编辑, 选第二项, 按'e'继续编辑
输入1代表进入单用户模式, 编辑完按'b'重启即可重设密码
设置grub全局密码
第一个title前面加入password 明文密码 或--md5 MD5密文(grub-md5-crypt)
设置grub局部密码
在相应title后面加入password 明文密码 或--md5 MD5密文(grub-md5-crypt)
编写grub.conf的title项绕过grub设置保护
grub菜单选"c"
grub>root (hd0,0)
grub>kernel /vmlinuz-2.6.32-642.e16.x86_64 root=/dev/sda2
grub>initrd /initramfs-2.6.32-642.e16.x86_64.img
破坏grub.conf文件, 进行修复启动
mv /boot/grub/grub.conf /root 破坏grub.conf文件
重启
grub>kernel (hd0,0)/vmlinuz-2.6.32-642.e16.x86_64 root=/dev/sda2
grub>initrd (hd0,0)/initramfs-2.6.32-642.e16.x86_64.img
修复之后重启再写一遍刚才的命令
2、破坏本机grub stage1阶段, 而后在救援模式下修复之
dd if=/dev/zero of=/dev/sda bs=1 count=446 破坏1阶段
重启
chroot /mnt/sysp_w_picpath/
ls -l /sbin/grub
grub-install /dev/sda
hexdump -C -n 512 -v /dev/sda
破坏本机grub stage1.5阶段, 进行修复
dd if=/dev/zero of=/dev/sda bs=512 count=2 seek=1 破坏1.5阶段
重启
hexdump -C -n 1024 -v /dev/sda
grub>root (hd0,0) 交互式方式修复, 依赖于/boot/grub/*1_5*文件修复
grub>setup (hd0)
quit
破坏本机grub stage2阶段, 进行修复
rm -fr /boot/grub 破坏2阶段
重启
cd /mnt/sysp_w_picpath/
chroot /mnt/sysp_w_picpath/
cd /boot
grub-install /dev/sda
重启
grub>kernel (hd0,0)/vmlinuz-2.6.32-642.e16.x86_64 root=/dev/sda2
grub>initrd (hd0,0)/initramfs-2.6.32-642.e16.x86_64.img
修复之后重启再写一遍刚才的命令
vim /boot/grub/grub.conf
default=0
timeout=5
title wanglinux
root (hd0,0)
kernel /vmlinuz-2.6.32-642.e16.x86_64 root=/dev/sda2
initrd /initramfs-2.6.32-642.e16.x86_64.img
破坏/sbin/init, 进行修复
mv /sbin/init /root 破坏/sbin/init
file /root/init
重启
grub append> root=/dev/sda2 init=/sbin/bash
mount -o remount,rw /
mv /root/init /sbin/
重启
3、删除vmlinuz和initramfs文件后无法启动, 两种方法恢复之
rm /boot/vmlinuz-2.6.32-642.e16.x86_64 initramfs-2.6.32-642.e16.x86_64.img
重启
ls /mnt/sysp_w_picpath/boot/
df
mkdir /mnt/cdrom 救援目录下建立挂载目录
mount /dev/cdrom /mnt/cdrom
ls
cd isolinux/
ls
cp vmlinuz /mnt/sysp_w_picpath/boot
cd /mnt/sysp_w_picpath/boot
ls
chroot /mnt/sysp_w_picpath/
mkinitrd /boot/initradmfs-`uname -r`.img`uname`
通过网络dhcp启动
chroot /mnt/sysp_w_picpath/
ls
cd /boot
ls
scp x.x.x.x :/boot/vmlinuz-2.6.32-642.e16.x86_64 /boot/initramfs-2.6.32-642.e16.x86_64.img /boot/
破坏本机grub stage1阶段, 删除/boot目录的所有文件, 并恢复之
dd if=/dev/zero of=/de/sda bs=446 count=1 破坏第1阶段
hexdump -n 512 -v /dev/sda
rm -rf /boot/ 删除boot目录所有文件
umount /boot
重启
mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
rmp -ivh /mnt/cdrom/Packages/kernel-2.6.32-642.e16.x86_64.rpm --root=/mnt/sysp_w_picpath --force|replacepkgs
cd /mnt/sysp_w_picpath/boot
ls
chroot /mnt/sysp_w_picpath/
grub-install /dev/sda
vim grub/grub.conf
default=0
timeout=3
title wanglinux
root (hd0,0)
kernel r!ls /boot/vmlinuz root=/dev/sda2
initrd r!ls /boot/initramfs
4、破坏本机grub stage1阶段, 删除/etc/fstab和/boot目录的所有文件,并恢复之
dd if=/dev/zero of=/de/sda bs=446 count=1 破坏第1阶段
hexdump -n 512 -v /dev/sda
rm -rf /boot/ 删除boot目录所有文件
rm -rf /etc/fstab 删除fstab文件
重启
df
lsblk 此时已无法使用lsblk
blkid
fdisk -l
pwd
df
mkdir /mnt/sda1
mkdir /mnt/sda2
mount /dev/sda1 /mnt/sda1
mount /dev/sda2 /mnt/sda2
cd /mnt/sda2
vi /mnt/sda2/etc/fstab
/dev/sda1 /boot ext4 defaults 1 1
/dev/sda2 / ext4 defaults 1 1
重启
chroot /mnt/sysp_w_picpath/
mount /dev/cdrom /mnt/
rpm -ivh /mnt/Packages/kernel-2.6.32-642.e16.x86_64.rpm --force
grub-install /dev/sda
vim /boot/grub/grub.conf
default=0
timeout=3
title wanglinux
root (hd0,0)
kernel /vmlinuz-2.6.32-642.e16.x86_64
initrd /initramfs-2.6.32-642.e16.x86_64.img
r!ls /boot/vmlinuz-2.6.32-642.e16.x86_64 /boot/initramfs-2.6.32-642.e16.x86_64.img
逻辑卷情况下的修复
lvscan
vgs
vgchange -ay 激活vg
ls /dev/vg0/*
pwd
mount /dev/vg0/root /mnt/sysp_w_picpath/
vi /mnt/sysp_w_picpath/etc/fstab
/dev/sda1 /boot ext4 defaults 1 1
/dev/vg0/root / ext4 defaults 1 2
/dev/vg0/usr /usr ext4 defaults 0 0
/dev/vg0/var /var ext4 defaults 0 0
重启
chroot /mnt/sysp_w_picpath/
df
mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
rpm -ivh /mnt/cdrom/Packages/kernel-2.6.32-642.e16.x86_64.rpm --force
vim /boot/grub/grub.conf
default=0
timeout=3
title wanglinux
root (hd0,0)
kernel /vmlinuz-2.6.32-642.e16.x86_64 root=/dev/vg0/root
initramfs-2.6.32-642.e16.x86_64.img
r!ls /boot/vmlinuz* /boot/initramfs*.img
5、增加新硬盘,在其上制作能单独运行kernel和bash的系统
1分区和建文件系统
lsblk
echo '---' > /sys/class/scsi_host/host2/scan
lsblk
fdisk /dev/sdb
mkfs.ext4 /dev/sdb1
mkfs.ext4 /dev/sdb2
2挂载boot
mkdir /mnt/boot
mount /dev/sdb1 /mnt/boot
cd /mnt/boot
cp /boot/vmlinuz-2.6.32-642.e16.x86_64 /boot/initramfs-2.6.32-642.e16.x86_64.img .
3安装grub
grub-install /dev/sdb --root-directory=/mnt
hexdump -C -n 512 -v /dev/sdb
4建立grub.conf
cd /mnt/boot/grub
vim grub.conf
default=0
timeout=3
title wanglinux
root (hd0,0)
kernel /vmlinuz-2.6.32-642.e16.x86_64 root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-642.e16.x86_64.img
r!ls /boot/vmlinuz-2.6.32-642.e16.x86_64 /boot/initramfs-2.6.32-642.e16.x86_64.img
5恢复内核和initramfs文件
cp /boot/vmlinuz-2.6.32-642.e16.x86_64 /mnt/boot/
cp /boot/initramfs-2.6.32-642.e16.x86_64.img /mnt/boot
6创建一级目录
mkdir /mnt/sysroot
mount /dev/sdb2 /mnt/sysroot/
cd /mnt/sysroot
mkdir -pv {bin,etc,sbin,dev,lib,lib64,proc,sys,tmp,root,home,usr,var,mnt,media,boot}
7建立fstab文件
vim /mnt/sysroot/etc/fstab
/dev/sda1 /boot ext4 defaults 1 1
/dev/sda2 / ext4 defaults 1 2
df
8、复制bash和相关库文件, 复制相关命令及相关库文件
/root/copycmd.sh
bash vim cat ls df mount umount rpm reboot hostname halt quit
chroot /mnt/sysroot
pwd
ls
自制U盘linux系统
lsblk
fdisk /dev/sdc
partx -a /dev/sdc
mkfs.ext4 /dev/sdc1
mkfs.ext4 /dev/sdc2
blkid
mount /dev/sdc1 /mnt/boot
mount /dev/sdc2 /mnt/sysroot
cd /mnt/boot
cp /boot/vmlinuz-2.6.32-642.e16.x86_64 /boot/initramfs-2.6.32-642.e16.x86_64.img .
grup-install /dev/sdc --root-directory=/mnt
vim grub.conf
default=0
timeout=3
title wanglinux
root (hd0,0)
kernel /vmlinuz-2.6.32-642.e16.x86_64 root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-642.e16.x86_64.img
r!ls /boot/vmlinuz-2.6.32-642.e16.x86_64 /boot/initramfs-2.6.32-642.e16.x86_64.img
cd /mnt/sysroot/
ls
mkdir -pv {etc,boot,dev,lib,lib64,sys,proc,home,root,mnt,usr,var,tmp}
/root/copycmd.sh
bash ls tree hostname mount shutdown poweroff cat vi umount quit
vi /etc/fstab
/dev/sda1 /boot ext4 defaults 1 1
/dev/sda2 / ext4 defaults 1 2
6、编译安装kernel,启用支持ntfs文件系统功能
cat /boot/config-2.6.32-642.e16.x86_64|grep -i ntfs
yum grouplist
yum -y groupinstall "Development Tools"
uname -r
lscpu
tar xvf linux-3.18.41.tar.xz -C /usr/src/
cd /usr/src
ls
ln -s linux-3.18.41/ linux
cd linux
ls
du -sh .
cp /boot/config-3.10.0-327.e17.x86_64 /usr/src/linux/.config
cat /usr/src/linux/.config
ll /usr/src/linux/.config
wc -l /usr/src/linux/.config
cd /usr/src/linux
ls
make menuconfig
yum -y install "*curses*"
make menuconfig
lscpu
make -j 4
du -sh /usr/src/linux-3.18.41/
find -name "*.ko" -ls |grep -i ntfs
cd /lib/modules
du -sh /lib/modules
cd /usr/src/linux-3.18.41
make modules_install /lib/modules/3.18.41-1.0-wanglinux
watch -n1 `du -sh /lib/modules/3.18.41-1.0-wanglinux/`
cd /lib/modules
cd /usr/src/linux
make install
lsmod |grep xfs
blkid
lsmod |grep ext4
grep -i ext4 /boot/config-2.6.32-642.e16.x86_64
locate ext4.ko
lsmod |grep e1000
rmmod e 1000
modprobe -r e1000
lsmod |grep usb
rmmod usb_storage
lsblk
modprobe usb_storage
lsblk
locate usb_storage.ko
grep -i usb_storage /boot/config-2.6.32-642.e16.x86_64
modinfo -n usb_storage|usb-storage
cat /proc/sys/net/ipv4/ip_forward
vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p
sysctl -w net.ipv4.ip_forward=1
sysctl -a |grep icmp
net.ipv4.icmp_echo_ignore_all=1
sysctl -p