文章目录
- 背景
- 运行要求
- 安装脚本
- 注意事项
- 完整代码
文章转载自如有乐享 & 萌咖 & 狗仔小分队的博客。
特别注意: OpenVZ构架不适用,安装之前备份重要数据。
背景
适用于由GRUB引导的CentOS,Ubuntu,Debian系统.
使用官方发行版去掉模板预装的软件。
同时也可以解决内核版本与软件不兼容的问题。
只要有root权限,还您一个纯净的系统。
运行要求
- Debian/Ubuntu/CentOS系统(由 GRUB引导);
- wget用来下载文件,获取公网IP;
- ip获取网关,掩码等;
- sed awk grep处理文本流;
- VNC安装系统(此项为可选)。
安装脚本
全自动安装默认 root 密码: Vicer,安装完成后请立即更改密码。
Debian 7 x32
1
2
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d 7 –v 32
|
Debian 7 x64
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d 7 –v 64
|
Debian 8 x32
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d 8 –v 32
|
Debian 8 x64
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d 8 –v 64
|
Debian 9 x32
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d 9 –v 32
|
Debian 9 x64
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d 9 –v 64
|
Ubuntu 14.04 x32
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d trusty –v 32
|
Ubuntu 14.04 x64
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d trusty –v 64
|
Ubuntu 16.04 x32
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d xenial –v 32
|
Ubuntu 16.04 x64
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d xenial –v 64
|
Ubuntu 17.04 x32
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d zesty –v 32
|
Ubuntu 17.04 x64
1
|
wget —no–check–certificate –qO DebianNET.sh ‘’ && chmod –x DebianNET.sh && bash DebianNET.sh –d zesty –v 64
|
CentOS 6+ X64
1
|
wget xiaofd.github.io/centos.sh && bash centos.sh |
安装时间(Vultr 2.5刀机器 重装大概15分钟) root用户密码为 xiaofd.win
注意事项
确保安装了所需软件:
1
2
3
4
5
|
/Ubuntu:
apt–get install –y gawk sed grep
/CentOS:
yum install –y gawk sed grep
|
如果出现了错误,请运行:
1
2
3
4
5
|
/Ubuntu:
apt–get update
/CentOS:
yum update
|
在安装Ubuntu时,可能会遇到:
Getting the time form a network time server...
界面进度条很长时间不会动,可以等待它超时或者更换别的版本.
该问题是Ubuntu系统的问题.
完整代码
#!/bash while [[ $# -ge 1 ]]; do case $1 in -v|--ver) shift VERtmp="$1" shift ;; -d|--debian|--ubuntu) shift vDEBtmp="$1" shift ;; -p|--password) shift WDtmp="$1" shift ;; -a|--auto) shift INStmp='auto' ;; -m|--manual) shift INStmp='manual' ;; -apt|--mirror) shift isMirror='1' tmpMirror="$1" shift ;; *) echo -ne " Usage:ntbash $0/--debian [7/33[33m33[04mwheezy33[0m|8/jessie|9/stretch]nttt/--ver [32/33[33m33[04mi38633[0m|64/amd64]nttt/--mirrornttt/--autonttt/--manualn" exit 1; ;; esac done [ $EUID -ne 0 ] && echo "Error:This script must be run as root!" && exit 1 [ -f /grub.cfg ] && GRUBOLD='0' && GRUBDIR='/grub' && GRUBFILE='grub.cfg' [ -z $GRUBDIR ] && [ -f /grub.cfg ] && GRUBOLD='0' && GRUBDIR='/grub2' && GRUBFILE='grub.cfg' [ -z $GRUBDIR ] && [ -f /grub.conf ] && GRUBOLD='1' && GRUBDIR='/grub' && GRUBFILE='grub.conf' [ -z $GRUBDIR -o -z $GRUBFILE ] && echo "Error! Not Found grub path." && exit 1 [ -n $vDEBtmp ] && { [ "$vDEBtmp" == '7' -o "$vDEBtmp" == 'wheezy' ] && linuxdists='debian' && vDEB='wheezy'; [ "$vDEBtmp" == '8' -o "$vDEBtmp" == 'jessie' ] && linuxdists='debian' && vDEB='jessie'; [ "$vDEBtmp" == '9' -o "$vDEBtmp" == 'stretch' ] && linuxdists='debian' && vDEB='stretch'; [ "$vDEBtmp" == 'precise' ] && linuxdists='ubuntu' && vDEB='precise'; [ "$vDEBtmp" == 'trusty' ] && linuxdists='ubuntu' && vDEB='trusty'; [ "$vDEBtmp" == 'wily' ] && linuxdists='ubuntu' && vDEB='wily'; [ "$vDEBtmp" == 'xenial' ] && linuxdists='ubuntu' && vDEB='xenial'; [ "$vDEBtmp" == 'yakkety' ] && linuxdists='ubuntu' && vDEB='yakkety'; [ "$vDEBtmp" == 'zesty' ] && linuxdists='ubuntu' && vDEB='zesty'; } [ -n $vDEBtmp ] && { [ "$VERtmp" == '32' -o "$VERtmp" == 'i386' ] && VER='i386'; [ "$VERtmp" == '64' -o "$VERtmp" == 'amd64' ] && VER='amd64'; } [ -z $linuxdists ] && linuxdists='debian' [ -n $isMirror ] && [ "$isMirror" == '1' ] && [ -n $tmpMirror ] && { tmpDebianMirror="$(echo -n "$tmpMirror" |grep -Eo '.*.(w+)')" echo -n "$tmpDebianMirror" |grep -q '://' [ $? -eq '0' ] && { DebianMirror="$(echo -n "$tmpDebianMirror" |awk -F'://' '{print $2}')" } || { DebianMirror="$(echo -n "$tmpDebianMirror")" } } || { [[ $linuxdists == 'debian' ]] && DebianMirror='' [[ $linuxdists == 'ubuntu' ]] && DebianMirror='' } [ -z $DebianMirrorDirectory ] && [ -n $DebianMirror ] && [ -n $tmpMirror ] && { DebianMirrorDirectory="$(echo -n "$tmpMirror" |awk -F''${DebianMirror}'' '{print $2}' |sed 's//$//g')" } [ "$DebianMirrorDirectory" == '/' ] && [ -n $DebianMirror ] && { [[ $linuxdists == 'debian' ]] && DebianMirrorDirectory='/debian' [[ $linuxdists == 'ubuntu' ]] && DebianMirrorDirectory='/ubuntu' } [ -z $DebianMirrorDirectory ] && [ -n $DebianMirror ] && { [[ $linuxdists == 'debian' ]] && DebianMirrorDirectory='/debian' [[ $linuxdists == 'ubuntu' ]] && DebianMirrorDirectory='/ubuntu' } [ -n $INStmp ] && { [ "$INStmp" == 'auto' ] && inVNC='n' [ "$INStmp" == 'manual' ] && inVNC='y' } [ -n $WDtmp ] && myPASSWORD="$WDtmp" [ -z $vDEB ] && vDEB='wheezy'; [ -z $VER ] && VER='i386'; [ -z $myPASSWORD ] && myPASSWORD='Vicer' clear && echo -e "n33[36m# Install33[0mn" [ -z $inVNC ] && ASKVNC(){ inVNC='y'; echo -ne "33[34mCan you login VNC?33[0me[33m[e[32mye[/n]e[0m " read inVNCtmp [[ -n "$inVNCtmp" ]] && inVNC=$inVNCtmp [ "$inVNC" == 'y' -o "$inVNC" == 'Y' ] && inVNC='y' [ "$inVNC" == 'n' -o "$inVNC" == 'N' ] && inVNC='n' } [ "$inVNC" == 'y' -o "$inVNC" == 'n' ] || ASKVNC; [[ $linuxdists == 'debian' ]] && LinuxName='Debian' [[ $linuxdists == 'ubuntu' ]] && LinuxName='Ubuntu' [ "$inVNC" == 'y' ] && echo -e "33[34mManual Mode33[0m insatll 33[33m$LinuxName33[0m [33[33m$vDEB33[0m] [33[33m$VER33[0m] in VNC. " [ "$inVNC" == 'n' ] && echo -e "33[34mAuto Mode33[0m insatll 33[33m$LinuxName33[0m [33[33m$vDEB33[0m] [33[33m$VER33[0m]. " echo -e "n[33[33m$vDEB33[0m] [33[33m$VER33[0m] Downloading..." [ -z $DebianMirror ] && echo -ne "33[31mError! 33[0mGet debian mirror fail! n" && exit 1 [ -z $DebianMirrorDirectory ] && echo -ne "33[31mError! 33[0mGet debian mirror directory fail! n" && exit 1 wget --no-check-certificate -qO '/initrd.gz' "$DebianMirror$/$/installer-$/$/$/initrd.gz" [ $? -ne '0' ] && echo -ne "33[31mError! 33[0mDownload 'initrd.gz' failed! n" && exit 1 wget --no-check-certificate -qO '/linux' "$DebianMirror$/$/installer-$/$/$/linux" [ $? -ne '0' ] && echo -ne "33[31mError! 33[0mDownload 'linux' failed! n" && exit 1 DEFAULTNET="$(ip route show |grep -o 'default via [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.*' |head -n1 |sed 's/proto.*|onlink.*//g' |awk '{print $NF}')" [ -n "$DEFAULTNET" ] && IPSUB="$(ip addr |grep ''${DEFAULTNET}'' |grep 'global' |grep 'brd' |head -n1 |grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/[0-9]{1,2}')" IPv4="$(echo -n "$IPSUB" |cut -d'/' -f1)" NETSUB="$(echo -n "$IPSUB" |grep -o '/[0-9]{1,2}')" GATE="$(ip route show |grep -o 'default via [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' |head -n1 |grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}')" [ -n "$NETSUB" ] && MASK="$(echo -n '/1,/2,/3,/4,/5,/6,/7,/8,/9,/10,/11,/12,/13,/14,/15,/16,/17,/18,/19,/20,/21,/22,/23,/24,/25,/26,/27,/28,/29,/30,/31,/32' |grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'${NETSUB}'' |cut -d'/' -f1)" [ -n "$GATE" ] && [ -n "$MASK" ] && [ -n "$IPv4" ] || { echo "Not found `ip command`, It will use `route command`." ipNum() { local IFS='.' read ip1 ip2 ip3 ip4 <<<"$1" echo $((ip1*(1<<24)+ip2*(1<<16)+ip3*(1<<8)+ip4)) } SelectMax(){ ii=0 for IPITEM in `route -n |awk -v OUT=$1 '{print $OUT}' |grep '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'` do NumTMP="$(ipNum $IPITEM)" eval "arrayNum[$ii]='$NumTMP,$IPITEM'" ii=$[$ii+1] done echo ${arrayNum[@]} |sed 's/s/n/g' |sort -n -k 1 -t ',' |tail -n1 |cut -d',' -f2 } [[ -z $IPv4 ]] && IPv4="$(ifconfig |grep 'Bcast' |head -n1 |grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' |head -n1)" [[ -z $GATE ]] && GATE="$(SelectMax 2)" [[ -z $MASK ]] && MASK="$(SelectMax 3)" [ -n "$GATE" ] && [ -n "$MASK" ] && [ -n "$IPv4" ] || { echo "Error! Not configure network. " exit 1 } } [ -f /interfaces ] && { [[ -z "$(sed -n '/iface.*inet /p' /interfaces)" ]] && AutoNet='1' || AutoNet='0' [ -d /interfaces.d ] && { ICFGN="$(find /interfaces.d -name '*.cfg' |wc -l)" || ICFGN='0' [ "$ICFGN" -ne '0' ] && { for NetCFG in `ls -1 /*.cfg` do [[ -z "$(cat $NetCFG | sed -n '/iface.*inet /p')" ]] && AutoNet='1' || AutoNet='0' [ "$AutoNet" -eq '0' ] && break done } } } [ -d /network-scripts ] && { ICFGN="$(find /network-scripts -name 'ifcfg-*' |grep -v 'lo'|wc -l)" || ICFGN='0' [ "$ICFGN" -ne '0' ] && { for NetCFG in `ls -1 /ifcfg-* |grep -v 'lo$' |grep -v ':[0-9]{1,}'` do [[ -n "$(cat $NetCFG | sed -n '/BOOTPROTO.*[dD][hH][cC][pP]/p')" ]] && AutoNet='1' || { AutoNet='0' && . $NetCFG [ -n $NETMASK ] && MASK="$NETMASK" [ -n $GATEWAY ] && GATE="$GATEWAY" } [ "$AutoNet" -eq '0' ] && break done } } [ ! -f $/$GRUBFILE ] && echo "Error! Not Found $GRUBFILE. " && exit 1 [ ! -f $/$GRUBFILE.old ] && [ -f $/$GRUBFILE.bak ] && mv -f $/$GRUBFILE.bak $/$GRUBFILE.old mv -f $/$GRUBFILE $/$GRUBFILE.bak [ -f $/$GRUBFILE.old ] && cat $/$GRUBFILE.old >$/$GRUBFILE || cat $/$GRUBFILE.bak >$/$GRUBFILE [ "$GRUBOLD" == '0' ] && { CFG0="$(awk '/menuentry /{print NR}' $/$GRUBFILE|head -n 1)" CFG2="$(awk '/menuentry /{print NR}' $/$GRUBFILE|head -n 2 |tail -n 1)" CFG1="" for CFGtmp in `awk '/}/{print NR}' $/$GRUBFILE` do [ $CFGtmp -gt "$CFG0" -a $CFGtmp -lt "$CFG2" ] && CFG1="$CFGtmp"; done [ -z "$CFG1" ] && { echo "Error! read $GRUBFILE. " exit 1 } sed -n "$CFG0,$CFG1"p $/$GRUBFILE >/grub.new [ -f /grub.new ] && [ "$(grep -c '{' /grub.new)" -eq "$(grep -c '}' /grub.new)" ] || { echo -ne "33[31mError! 33[0mNot configure $GRUBFILE. n" exit 1 } sed -i "/menuentry.*/cmenuentry 'Install OS [$vDEB $VER]' --class debian --class gnu-linux --class gnu --class os {" /grub.new [ "$(grep -c '{' /grub.new)" -eq "$(grep -c '}' /grub.new)" ] || { echo "Error! configure append $GRUBFILE. " exit 1 } sed -i "/echo.*/d" /grub.new } [ "$GRUBOLD" == '1' ] && { CFG0="$(awk '/title /{print NR}' $/$GRUBFILE|head -n 1)" CFG1="$(awk '/title /{print NR}' $/$GRUBFILE|head -n 2 |tail -n 1)" [ -n $CFG0 ] && [ -z $CFG1 -o $CFG1 == $CFG0 ] && sed -n "$CFG0,$"p $/$GRUBFILE >/grub.new [ -n $CFG0 ] && [ -z $CFG1 -o $CFG1 != $CFG0 ] && sed -n "$CFG0,$CFG1"p $/$GRUBFILE >/grub.new [ ! -f /grub.new ] && echo "Error! configure append $GRUBFILE. " && exit 1 sed -i "/title.*/ctitle 'Install OS [$vDEB $VER]'" /grub.new sed -i '/^#/d' /grub.new } [ -n "$(grep 'initrd.*/' /grub.new |awk '{print $2}' |tail -n 1 |grep '^/')" ] && Type='InBoot' || Type='NoBoot' LinuxKernel="$(grep 'linux.*/' /grub.new |awk '{print $1}' |head -n 1)" [ -z $LinuxKernel ] && LinuxKernel="$(grep 'kernel.*/' /grub.new |awk '{print $1}' |head -n 1)" LinuxIMG="$(grep 'initrd.*/' /grub.new |awk '{print $1}' |tail -n 1)" [ "$Type" == 'InBoot' ] && { sed -i "/$LinuxKernel.*//c\t$LinuxKernel\t/boot/linux auto=true hostname=$linuxdists domain= -- quiet" /grub.new sed -i "/$LinuxIMG.*//c\t$LinuxIMG\t/boot/initrd.gz" /grub.new } [ "$Type" == 'NoBoot' ] && { sed -i "/$LinuxKernel.*//c\t$LinuxKernel\t/linux auto=true hostname=$linuxdists domain= -- quiet" /grub.new sed -i "/$LinuxIMG.*//c\t$LinuxIMG\t/initrd.gz" /grub.new } sed -i '$a\n' /grub.new [ "$inVNC" == 'n' ] && { GRUBPATCH='0' [ -f /interfaces -o -d /network-scripts ] && { sed -i ''${CFG0}'i\n' $/$GRUBFILE sed -i ''${CFG0}'r /grub.new' $/$GRUBFILE [ -z $AutoNet ] && echo "Error, Not found interfaces config." && exit 1 [ -f $/grubenv ] && sed -i '/g' $/grubenv [ -d /tmp ] && rm -rf /tmp mkdir -p / cd / gzip -d < ../initrd.gz | cpio --extract --verbose --make-directories --no-absolute-filenames >>/null 2>&1 cat >/preseed.cfg<<EOF d-i /locale string en_US d-i /layoutcode string us d-i /xkb-keymap string us d-i /choose_interface select auto d-i /disable_autoconfig boolean true d-i /dhcp_failed note d-i /dhcp_options select Configure network manually d-i /get_ipaddress string $IPv4 d-i /get_netmask string $MASK d-i /get_gateway string $GATE d-i /get_nameservers string 8.8.8.8 d-i /no_default_route boolean true d-i /confirm_static boolean true d-i /country string manual d-i / string $DebianMirror d-i / string $DebianMirrorDirectory d-i / string d-i /root-login boolean ture d-i /make-user boolean false d-i /root-password password $myPASSWORD d-i /root-password-again password $myPASSWORD d-i /allow-password-weak boolean true d-i /encrypt-home boolean false d-i /utc boolean true d-i /zone string US/Eastern d-i /ntp boolean true d-i /early_command string debconf-set /disk "$(list-devices disk |head -n1)"; debconf-set /bootdev string "$(list-devices disk |head -n1)"; umount /media || true; d-i /mount_style select uuid d-i /init_automatically_partition select Guided - use entire disk d-i /method string regular d-i /device_remove_lvm boolean true d-i /device_remove_md boolean true d-i /choose_recipe select atomic d-i /confirm_write_new_label boolean true d-i /choose_partition select finish d-i /confirm boolean true d-i /confirm_nooverwrite boolean true d-i /confirm boolean true d-i /confirm_nooverwrite boolean true d-i /allow_unauthenticated boolean true tasksel /first multiselect minimal d-i /update-policy select none d-i /include string openssh-server d-i /upgrade select none popularity-contest /participate boolean false d-i /only_debian boolean true d-i /bootdev string default d-i /reboot_in_progress note d-i /reboot boolean true d-i /late_command string sed -i 's/^.*PermitRootLogin.*/PermitRootLogin /g' /sshd_config; sed -i 's/^.*PasswordAuthentication.*/PasswordAuthentication /g' /sshd_config; EOF [ "$AutoNet" -eq '1' ] && { sed -i '/netcfg/d' /preseed.cfg sed -i '/netcfg/d' /preseed.cfg sed -i '/netcfg/get_.*/d' /preseed.cfg sed -i '/netcfg/d' /preseed.cfg } [ "$vDEB" == 'trusty' ] && GRUBPATCH='1' [ "$vDEB" == 'wily' ] && GRUBPATCH='1' [ "$GRUBPATCH" == '1' ] && { sed -i 's/^d-i grub-installer/bootdev string /g' /preseed.cfg } [ "$GRUBPATCH" == '0' ] && { sed -i 's/debconf-set grub-installer/bootdev.*";//g' /preseed.cfg } [ "$linuxdists" == 'debian' ] && { sed -i '/user-setup/d' /preseed.cfg sed -i '/user-setup/d' /preseed.cfg sed -i '/pkgsel/d' /preseed.cfg sed -i 's/umount /media.*;//g' /preseed.cfg } rm -rf ../initrd.gz find . | cpio -H newc --create --verbose | gzip -9 > ../initrd.gz rm -rf /tmp } } [ "$inVNC" == 'y' ] && { sed -i '$i\n' $/$GRUBFILE sed -i '$r /grub.new' $/$GRUBFILE echo -e "n33[33m33[04mIt will reboot! nPlease look at VNC! nSelect33[0m33[32m Install OS [$vDEB $VER] 33[33m33[4mto install system.33[04mnn33[31m33[04mThere is some information for you.nDO NOT CLOSE THE WINDOW! 33[0mn" echo -e "33[35mIPv4ttNETMASKttGATEWAY33[0m" echo -e "33[36m33[04m$IPv433[0mt33[36m33[04m$MASK33[0mt33[36m33[04m$GATE33[0mnn" read -n 1 -p "Press Enter to reboot..." INP if [ "$INP" != '' ] ; then echo -ne 'b n' echo ""; fi } chown root:root $/$GRUBFILE chmod 444 $/$GRUBFILE sleep 3 && reboot >/null 2>&1