案例一:删除某一个文件
# rm <file>
案例二:删除某一个目录以及目录里的所有文件
# rm -r <directory>
案例三:非交互式删除某一个目录以及目录里的所有文件
# rm -rf <directory>
案例四:非交互式删除当前目录下的所有隐藏文件
# rm -rf .#*
案例五:非交互式删除所有特定后缀名称的文件
# rm -rf *txt
(补充:这里以非交互式删除所有以 txt 作为后缀名称的文件为例)
# rm <file>
# rm -r <directory>
# rm -rf <directory>
# rm -rf .#*
# rm -rf *txt
(补充:这里以非交互式删除所有以 txt 作为后缀名称的文件为例)
作者:朱明宇
名称:自动化部署 GRUB2 Bootloader 密码(openSUSE & SLES 版)
作用:自动化部署 GRUB2 Bootloader 密码(openSUSE & SLES 版)
1. 给此脚本添加执行权限
2. 执行此脚本
3. 当启动系统时,左下角出现以下内容时按下 “E”:
C: Command Line E: Edit Entry
1. 登录的账号是 root
2. 登录的密码是 eternalcenter
#!/bin/bash
echo "#!/bin/sh
exec tail -n +3 \$0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
set superusers=\"root\"
password_pbkdf2 root grub.pbkdf2.sha512.10000.B857B79D02FF55CA3A69B8485C1A4A427424630C804CC8A89134520A34E056D7882A778F6BC8AD856CB95DF8B99BE25F9FEAD899D826BB3915FB20BAD682D10A.4A1CF49B7F815A0578031CCA2CE98C66BDFBEAB0AE2721531BB54AFC6CFFA990FBD9062F41C006376C283B717FEC1BD9BB1AFB882AF91B5F4A812459D7974D1F
export superusers
set unrestricted_menu=\"y\"
export unrestricted_menu" > /etc/grub.d/40_custom
grep "rd.shell=0" /etc/default/grub
if [ $? -ne 0 ];then
sed -i '/GRUB_CMDLINE_LINUX_DEFAULT="/s/"$/ rd.shell=0"/' /etc/default/grub
fi
grep "showopts" /etc/default/grub
if [ $? -ne 0 ];then
sed -i '/GRUB_CMDLINE_LINUX_DEFAULT="/s/"$/ showopts"/' /etc/default/grub
fi
grep "LANG=C" /etc/default/grub
if [ $? -ne 0 ];then
sed -i '/GRUB_CMDLINE_LINUX_DEFAULT="/s/"$/ LANG=C"/' /etc/default/grub
fi
grub2-mkconfig -o /boot/grub2/grub.cfg
在创建 KVM 虚拟机之前要先安装 KVM 并创建 KVM 虚拟网络
在 openSUSE 官网上下载安装系统所需要的镜像:
https://software.opensuse.org/distributions/leap
主要用于批量克隆出新的 KVM 机器,节约创建新虚拟机的时间
(只在真机上执行以下步骤)
# qemu-img create -f qcow2 /var/lib/libvirt/images/template_opensuse_leap_15_10g.qcow2 10G
(补充:这里以创建 10G 大小的 template_opensuse_leap_15_10g.qcow2 硬盘文件为例)
(只在真机上执行以下步骤)
# ls /var/lib/libvirt/images/ | grep template_opensuse_leap_15_10g.qcow2
(补充:这里以确认 template_opensuse_leap_15_10g.qcow2 硬盘文件为例)
(只在真机上执行以下步骤)
# virt-manager
(只在真机上执行以下步骤)
(步骤略)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(补充:这里以使用 openSUSE-Leap-15.2-DVD-x86_64.iso 系统镜像为例)
(只在真机上执行以下步骤)
(补充:这里以设置 2048 MiB 内容和 2 核 CPU 为例)
(只在真机上执行以下步骤)
(补充:这里以使用 template_opensuse_leap_15_10g.qcow2 硬盘文件为例)
(只在真机上执行以下步骤)
(注意:虚拟网络必须提前创建好)
(补充:这里以将虚拟机命名为 template_opensuse_leap_15_10g 并使用 0 网络为例)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
(只在真机上执行以下步骤)
1) 一定要使用刚刚创建的 template_opensuse_leap_15_10g.qcow2 作为安装虚拟机的硬件文件
2) 虚拟机网络 “0” 要提前创建好
3) 只分一个分区,只设置一个挂载点挂载到根 “/”,使用标准硬盘,硬盘格式是 XFS
4) 选择最小化安装系统
(只在虚拟机上执行以下步骤)
# vi /etc/default/grub
将全部内容修改如下:
# If you change this file, run 'grub2-mkconfig -o /boot/grub2/grub.cfg' afterwards to update
# /boot/grub2/grub.cfg.
# Uncomment to set your own custom distributor. If you leave it unset or empty, the default
# policy is to determine the value from /etc/os-release
GRUB_DISTRIBUTOR=
GRUB_DEFAULT=saved
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=8
GRUB_CMDLINE_LINUX_DEFAULT="splash=silent mitigations=auto quiet"
GRUB_SERIAL_COMMAND="serial --unit=1 --speed=115200"
GRUB_CMDLINE_LINUX="biosdevname=0 net.ifnames=0 console=tty0 console=ttyS0,115200n8"
GRUB_DISABLE_LINUX_UUID="true"
GRUB_ENABLE_LINUX_LABEL="true"
GRUB_DISABLE_RECOVERY="true"
# Uncomment to automatically save last booted menu entry in GRUB2 environment
# variable `saved_entry'
# GRUB_SAVEDEFAULT="true"
#Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
# GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"
#Uncomment to disable graphical terminal (grub-pc only)
GRUB_TERMINAL="gfxterm"
# The resolution used on graphical terminal
#note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
GRUB_GFXMODE="auto"
# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
# GRUB_DISABLE_LINUX_UUID=true
#Uncomment to disable generation of recovery mode menu entries
# GRUB_DISABLE_RECOVERY="true"
#Uncomment to get a beep at grub start
# GRUB_INIT_TUNE="480 440 1"
GRUB_BACKGROUND=
GRUB_THEME=/boot/grub2/themes/openSUSE/theme.txt
SUSE_BTRFS_SNAPSHOT_BOOTING="true"
GRUB_DISABLE_OS_PROBER="false"
GRUB_ENABLE_CRYPTODISK="n"
GRUB_CMDLINE_XEN_DEFAULT="vga=gfx-1024x768x16"
(只在虚拟机上执行以下步骤)
# grub2-mkconfig -o grub
(只在虚拟机上执行以下步骤)
# blkid
/dev/vda1: UUID="53ee2f87-89b8-4cd7-a4dc-0957d28f4831" TYPE="xfs" PARTUUID="3d8377ef-01"
(补充:这里的 UUID 是: 53ee2f87-89b8-4cd7-a4dc-0957d28f4831)
(只在虚拟机上执行以下步骤)
# vi /etc/fstab
将以下内容:
......
UUID=53ee2f87-89b8-4cd7-a4dc-0957d28f4831 / xfs defaults 0 0
(补充:这里的 UUID 是: 53ee2f87-89b8-4cd7-a4dc-0957d28f4831)
修改为:
......
/dev/vda1 / xfs defaults 0 0
(只在虚拟机上执行以下步骤)
# zypper -n rm firewalld-*
(只在虚拟机上执行以下步骤)
# zypper -n in growpart
# vim /etc/init.d/after.local
创建以下内容:
/usr/bin/growpart /dev/sda1
/usr/sbin/xfs_growfs /
# chmod 755 /etc/init.d/after.local
(只在虚拟机上执行以下步骤)
# zypper mr -da
(只在虚拟机上执行以下步骤)
# zypper ar -fcg http://10.0.0.254/openSUSE-Leap-15/ lan
(注意: http://10.0.0.254/openSUSE-Leap-15/ 需要根据真实环境的情况进行更改)
(只在虚拟机上执行以下步骤)
# zypper ref
(只在虚拟机上执行以下步骤)
# hostnamectl set-hostname template_opensuse_leap_15_10g
(只在虚拟机上执行以下步骤)
# systemctl start serial-getty@ttyS0
# systemctl enable serial-getty@ttyS0
(只在虚拟机上执行以下步骤)
# history -c
(只在虚拟机上执行以下步骤)
# poweroff
删除配置文件里 cdrom 相关的部分
# vim /etc/libvirt/qemu/template_opensuse_leap_15_10g.xml
删除以下内容:
......
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/test/iso/openSUSE-Leap-15.2-DVD-x86_64.iso'/>
<target dev='sda' bus='sata'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
......
(只在真机上执行以下步骤)
(步骤略)
作者:朱明宇
名称:批量设置官方软件源(openSUSE 版)
作用:批量设置官方软件源(openSUSE 版)
1. 服务器清单 $add_repo_servers_list.txt 每个服务器名占用 1 行,并和此脚本放在同一目录下
2. 在此脚本的分割线内写入相应的内容
3. 给此脚本添加执行权限
4. 执行此脚本
add_repo_servers_list.txt #指定存放要设置官方软件源的文件
1. 此脚本执行前必须要先保证执行此脚本的用户能无密码 ssh 远程这些远程服务器
2. 服务器的系统需要是 openSUSE 15.2 版本
3. 服务器系统要配置好可用的软件源(最好是软件数量最多的官方版本)
4. 这些远程服务器要能够连接外网
#!/bin/bash
####################### Separator ########################
add_repo_servers_list.txt
####################### Separator ########################
cat add_repo_servers_list.txt
read -p "will add opensuse_leap_15.2 repo please input y " a
echo $a
if [ "$a" != "y" ];then
echo "you don't agree so exit now"
exit
fi
for i in `awk '{print $1}' add_repo_servers_list.txt`
do
ssh $i '
sudo -u root su - root -c "zypper mr -da"
sudo -u root su - root -c "zypper ar -fcg http://download.opensuse.org/distribution/leap/15.2/repo/oss/ OpenSUSE_Leap_152_x64_update-oss"
sudo -u root su - root -c "zypper ar -fcg http://download.opensuse.org/distribution/leap/15.2/repo/non-oss/ OpenSUSE_Leap_152_x64_update-non-oss"
sudo -u root su - root -c "zypper ar -fcg http://download.opensuse.org/update/leap/15.2/oss/ OpenSUSE_Leap_152_x64_oss"
sudo -u root su - root -c "zypper ar -fcg http://download.opensuse.org/update/leap/15.2/non-oss/ OpenSUSE_Leap_152_x64_non-oss"
sudo -u root su - root -c "zypper ref"
done
客户端电脑
外网服务器
内网服务器
客户端电脑 外网服务器 内网服务器
内网服务器建立到外网服务器的 SSH 通道
客户端电脑通过 SSH 外网服务器连接到内网服务器
内网服务器就算没有公网 IP 地址也能被 SSH 上
1) 所有服务器的系统都需要是 Linux 版本
2) 所有服务器都要关闭防火墙
3) 内网服务器要能够 ping 通外网服务器
4) 客户端电脑要能够 ping 通外网服务器
(分别在内网服务器和外网服务器上执行以下步骤)
# useradd <user for intranet penetration>
(分别在内网服务器和外网服务器上执行以下步骤)
# passwd <user for intranet penetration>
(分别在内网服务器和外网服务器上执行以下步骤)
# su - <user for intranet penetration>
(分别在内网服务器和外网服务器上执行以下步骤)
$ ssh-keygen
(分别在内网服务器和外网服务器上执行以下步骤)
$ exit
(只在内网服务器上执行以下步骤)
# su - <user for intranet penetration>
(只在内网服务器上执行以下步骤)
$ ssh-copy-id <IP address of Internet server>
(只在内网服务器上执行以下步骤)
$ exit
(只在外网服务器上执行以下步骤)
# su - <user for intranet penetration>
(只在外网服务器上执行以下步骤)
$ ssh-copy-id 127.0.0.1
(只在外网服务器上执行以下步骤)
$ exit
# su - <user for intranet penetration>
$ ssh -X -fCNR 11000:localhost:22 <IP address of Internet server>
(补充:这里以使用用于内网穿透的用户将内网服务器的 22 端口影射到外网服务器的 11000 端口为例)
(
注意:如果在内网服务器和外网服务器上创建的用于 SSH 内网穿透的用户名称不一样,则需要在最后 SSH 时添加上外网服务器上用于 SSH 的用户名
# su - <user for intranet penetration>
$ ssh -X -fCNR 11000:localhost:22 <user for intranet penetration>@<IP address of Internet server>
)
或者:
# ps -aux | grep -v grep | grep "11000:localhost:22 <IP address of Internet server>" &> /dev/null || su - <user for intranet penetration> -c 'ssh -X -fCNR 11000:localhost:22 <IP address of Internet server>'
(
注意:如果在内网服务器和外网服务器上创建的用于 SSH 内网穿透的用户名称不一样,则需要在最后 SSH 时添加上外网服务器上用于 SSH 的用户名
# ps -aux | grep -v grep | grep "11000:localhost:22 <IP address of Internet server>" &> /dev/null || su - <user for intranet penetration> -c 'ssh -X -fCNR 11000:localhost:22 <user for intranet penetration>@<IP address of Internet server>'
)
(补充:这里以使用用于内网穿透的用户将内网服务器的 22 端口影射到外网服务器的 11000 端口为例)
# su - <user for intranet penetration>
$ ssh -X -fCNL *:10000:localhost:11000 localhost
或者:
# ps -aux | grep -v grep | grep "*:10000:localhost:11000 localhost" || su - <user for intranet penetration> -c 'ssh -X -fCNL *:10000:localhost:11000 localhost'
(补充:这里以使用用于内网穿透的用户将外网服务器的 10000 端口影射到外网服务器的 11000 端口为例)
# ssh -p 10000 <user for intranet penetration>@<IP address of Internet server>
(补充:SSH 成功后,客户端电脑就可以直接 SSH 到内网服务器中了)