centos、Ubuntu minimal install tuning shell script

该脚本仅支持centos7及Ubuntu1804:
  1 #!/bin/bash
  2 #
  3 #*************************************************************************************
  4 #Author:                                                      wanghaomiao
  5 #QQ:                                                          1074035975
  6 #Date:                                                        2022-04-19
  7 #FileName                                                     reset_1.sh
  8 #URL:                                                         wanghaomiao.com.cn
  9 #Description:                                                 The test script
 10 #Copyreght (C):                                               2022 All rightsreserved
 11 #*************************************************************************************
 12 COLOR='\033[1;'"$[$RANDOM%7+31]"'m'
 13 COLOR_END='\e[0m'
 14 NETWORK_NAME=`ip a | sed -nr '/^2/s#.* (.*): .*#\1#p'`
 15 Ubuntu_NETWORK_DIR="/etc/netplan/00-installer-config.yaml"
 16 Centos_NETWORK_DIR="/etc/sysconfig/network-scripts/ifcfg-$NETWORK_NAME"
 17 
 18 
 19 
 20 set_sshd(){
 21 #whoami | grep root &> /dev/null
 22 #if [ $? -eq 0 ]; then
 23 #if [[ `whoami` == "root" ]]; then
 24 #    sed -ri.bak -e 's/.*(UseDNS ).*/\1no/' -e 's/.*(GSSAPIAuthentication ).*/\1no/' -e 's/.*(PermitRootLogin ).*/\1yes/' /etc/ssh/sshd_config
 25 #else
 26     sudo  sed -ri.bak -e 's/.*(UseDNS ).*/\1no/' -e 's/.*(GSSAPIAuthentication ).*/\1no/' -e 's/.*(PermitRootLogin ).*/\1yes/' /etc/ssh/sshd_config
 27 #fi
 28 if [ $? -eq 0 ]; then
 29     echo -e "$COLOR ssh service setup completed!restart the service! $COLOR_END"
 30 else
 31     echo -e "$COLOR SSH service setting failed, please try again,exit! $COLOR_END"
 32 fi
 33 sudo systemctl restart sshd
 34 }
 35 
 36 set_vimrc(){
 37 echo -ne "$COLOR"
 38 echo -e "Please enter relevant information to set vimrc file"
 39 read -p "pleace enter your UserName:" UserName
 40 read -p "pleace enter your QQ:" QQ
 41 read -p "pleace enter your UserURL:" UserURL
 42 echo -ne "$COLOR_END"
 43 #
 44 #if [[ `whoami` == "root" ]]; then
 45 #cat >~/.vimrc <<EOF
 46 #set ts=4
 47 #set expandtab
 48 #set ignorecase
 49 #autocmd BufNewFile *.sh exec ":call SetTitile()"
 50 #func SetTitile()
 51 #    if expand("%:e") == 'sh'
 52 #    call setline(1,"#!/bin/bash")
 53 #    call setline(2,"#")
 54 #    call setline(3,"#*************************************************************************************")
 55 #    call setline(4,"#Author:                                                       $UserName")
 56 #    call setline(5,"#QQ:                                                           $QQ")
 57 #    call setline(6,"#Date:                                                         ".strftime("%Y-%m-%d"))
 58 #    call setline(7,"#FileName                                                      ".expand("%"))
 59 #    call setline(8,"#URL:                                                          $UserURL")
 60 #    call setline(9,"#Description:                                                  The test script")
 61 #    call setline(10,"#Copyreght (C):                                                ".strftime("%Y")." All rightsreserved")
 62 #    call setline(11,"#*************************************************************************************")
 63 #    call setline(12,"")
 64 #    endif
 65 #endfunc
 66 #autocmd BufNewFile * normal G
 67 #EOF
 68 #else
 69 sudo cat >~/.vimrc <<EOF
 70 set ts=4
 71 set expandtab
 72 set ignorecase
 73 autocmd BufNewFile *.sh exec ":call SetTitile()"
 74 func SetTitile()
 75     if expand("%:e") == 'sh'
 76     call setline(1,"#!/bin/bash")
 77     call setline(2,"#")
 78     call setline(3,"#*************************************************************************************")
 79     call setline(4,"#Author:                                                       $UserName")
 80     call setline(5,"#QQ:                                                           $QQ")
 81     call setline(6,"#Date:                                                         ".strftime("%Y-%m-%d"))
 82     call setline(7,"#FileName                                                      ".expand("%"))
 83     call setline(8,"#URL:                                                          $UserURL")
 84     call setline(9,"#Description:                                                  The test script")
 85     call setline(10,"#Copyreght (C):                                                ".strftime("%Y")." All rightsreserved")
 86     call setline(11,"#*************************************************************************************")
 87     call setline(12,"")
 88     endif
 89 endfunc
 90 autocmd BufNewFile * normal G
 91 EOF
 92 #fi
 93 if [ $? -eq 0 ]; then
 94     echo -e "$COLOR .vimrc setup completed! $COLOR_END"
 95 else
 96     echo -e "$COLOR .vimrc setting failed,please try again,exit! $COLOR_END"
 97     exit
 98 fi
 99 }
100 
101 set_selinux_firewalld(){
102 sudo sed -ri.bak 's/(SELINUX=).*/\1disabled/' /etc/selinux/config
103 sudo systemctl disable --now firewalld
104 if [ $? -eq 0 ];then
105     echo -e "$COLOR SElinux and firewalld are turend off! $COLOR_END"
106 else
107     echo -e "$COLOR SELinux and firewall shutdown failed. Please try again,exit! $COLOR_END"
108     exit
109 fi
110 }
111 
112 set_hostname(){
113 read -p "Please enter the host name to set: " HostName
114 sudo hostnamectl set-hostname $HostName
115 if [ $? -eq 0 ];then
116     echo -e "$COLOR Hostname setting completed,pleace re login! $COLOR_END"
117 else
118     echo -e "$COLOR Hostname setting failed,please try again,exit! $COLOR_END"
119     exit
120 fi
121 }
122 
123 set_date(){
124 read -p "Please enter the time zone to set: " ZONE
125 sudo timedatectl set-timezone $ZONE
126 if [ $? -eq 0 ];then
127     echo -e "$COLOR Time zone $ZONE settings completed! $COLOR_END"
128 else
129     echo -e "$COLOR Time zone $ZONE setting failed, please try again,exit! $COLOR_END"
130     exit
131 fi
132 }
133 
134 set_aliyun_yum(){
135 sudo yum repolist
136 sudo yum -y install wget
137 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.buckup
138 sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
139 sudo yum clean all
140 sudo yum makecache
141 
142 if [ $? -eq 0 ];then
143     echo -e "$COLOR Alibaba cloud Yum source settings completed! $COLOR_END"
144 else
145     echo -e "$COLOR Alibaba colod Yum source settings failed,please try again,exit! $COLOR_END"
146     exit
147 fi
148 }
149 
150 set_qinghuayun_yum(){
151 sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
152          -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
153          -i.bak \
154          /etc/yum.repos.d/CentOS-*.repo
155 sudo yum clean all
156 sudo yum makecache
157 
158 if [ $? -eq 0 ];then
159     echo -e "$COLOR Tsinghua mirror Yum source settings completed! $COLOR_END"
160 else
161     echo -e "$COLOR Tsinghua mirror Yum source settings failed,please try again,exit! $COLOR_END"
162     exit
163 fi
164 }
165 
166 
167 set_yum(){
168 echo -ne "$COLOR"
169 cat <<EOF
170 1) 阿里镜像源
171 2) 清华镜像源
172 3) 返回上一级目录
173 4) 退出
174 EOF
175 echo -ne "$COLOR_END"
176 read -p "请输入菜单编号(1-4): " YUM_MENU
177 
178 case $YUM_MENU in
179 1)
180     set_aliyun_yum
181     ;;
182 2)
183     set_qinghuayun_yum
184     ;;
185 3)
186     set_run
187     ;;
188 4)
189     exit
190     ;;
191 *)
192     echo -ne "$COLOR Input error, please re-enter! $COLOR_END"
193 esac
194 }
195 
196 set_aliyun_apt(){
197 sudo sed -ri.bak 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
198 if [ $? -eq 0 ];then
199     echo -e "$COLOR Alibaba cloud apt source settings completed! $COLOR_END"
200 else
201     echo -e "$COLOR Alibaba colod apt source settings failed,please try again,exit! $COLOR_END"
202     exit
203 fi
204 sudo apt update
205 }
206 
207 set_qinghuayun_apt(){
208 sudo sed -ri.bak 's@http://cn.archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list
209 if [ $? -eq 0 ];then
210     echo -e "$COLOR Tsinghua mirror apt source settings completed! $COLOR_END"
211 else
212     echo -e "$COLOR Tsinghua mirror apt source settings failed,please try again,exit! $COLOR_END"
213     exit
214 fi
215 sudo apt update 
216 }
217 
218 set_apt(){
219 echo -ne "$COLOR"
220 cat <<EOF
221 1) 阿里镜像源
222 2) 清华镜像源
223 3) 返回上一级
224 4) 退出
225 EOF
226 echo -ne "$COLOR_END"
227 read -p "请输入菜单编号(1-4): " APT_MENT
228 
229 case $APT_MENT in
230 1)
231     set_aliyun_apt
232     set_run
233     ;;
234 2)
235     set_qinghuayun_apt
236     ;;
237 3)
238     set_run
239     ;;
240 4)
241     exit
242     ;;
243 *)
244     echo -ne "$COLOR Input error, please re-enter! $COLOR_END"
245 esac
246 }
247 
248 set_aliyun_epel(){
249 sudo mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup &> /dev/null
250 sudo mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup &> /dev/null
251 sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
252 if [ $? -eq 0 ];then
253     echo -e "$COLOR Alibaba cloud epel source settings completed! $COLOR_END"
254 else
255     echo -e "$COLOR Alibaba colod epel source settings failed,please try again,exit! $COLOR_END"
256     exit
257 fi
258 }
259 
260 set_qinghuayun_epel(){
261 sudo yum install epel-release
262 dudo sed -e 's!^metalink=!#metalink=!g' \
263          -e 's!^#baseurl=!baseurl=!g' \
264          -e 's!//download\.fedoraproject\.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
265          -e 's!//download\.example/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
266          -e 's!http://mirrors!https://mirrors!g' \
267          -i /etc/yum.repos.d/epel*.repo
268 if [ $? -eq 0 ];then
269     echo -e "$COLOR Tsinghua mirror epel source settings completed! $COLOR_END"
270 else
271     echo -e "$COLOR Tsinghua mirror epel source settings failed,please try again,exit! $COLOR_END"
272     exit
273 fi
274 }
275 
276 set_epel(){
277 echo -ne "$COLOR"
278 cat <<EOF
279 1) 阿里镜像源
280 2) 清华镜像源
281 3) 返回上一级
282 4) 退出
283 EOF
284 echo -ne "$COLOR_END"
285 read -p "请输入菜单编号(1-4):" EPEL_MENT
286 
287 case $EPEL_MENT in
288 1)
289     set_aliyun_epel
290     ;;
291 2)
292     set_qinghuayun_epel
293     ;;
294 3)
295     set_run
296     ;;
297 4)
298     exit
299     ;;
300 *)
301     echo -ne "$COLOR Input error, please re-enter! $COLOR_END"
302 esac
303 }
304 
305 set_Ubuntu1804_software_install(){
306 sudo apt -y purge ufw lxd lxd-client lxcfs lxc-common
307 if [ $? -eq 0 ];then
308     sudo apt -y install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip
309     if [ $? -eq 0 ];then
310         echo -e "$COLOR software installation completed! $COLOR_END"
311     else
312         echo -e "$SOLOR sofrware installation failed,please try again,exit! $COLOR_END"
313         exit
314     fi
315 else
316     echo -e "$SOLOR sofrware installation failed,please try again,exit! $COLOR_END"
317     exit
318 fi
319 }
320 
321 set_Centos7_software_install(){
322 sudo yum -y install gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages redhat-lsb-core postfix mailx bash-completion man-pages
323 if [ $? -eq 0 ];then
324     echo -e "$COLOR software installation completed! $COLOR_END"
325 else
326     echo -e "$SOLOR sofrware installation failed,please try again,exit! $COLOR_END"
327     exit
328 fi
329 }
330 
331 set_Ubuntu1804_eth0(){
332 eval sudo sed -ri.bak 's/$NETWORK_NAME/eth0/g' $Ubuntu_NETWORK_DIR
333 sudo sed -i -r 's/(GRUB_CMDLINE_LINUX=.*)"/\1net.ifnames=0 biosdevname=0"/g' /etc/default/grub
334 sudo update-grub
335 if [ $? -eq 0 ];then
336     echo -e "$COLOR The network card name has been changed.about to reboot! $COLOR_END"
337     sudo reboot
338 else
339     echo -e "$COLOR The network card name change failed, please try again,exit! $COLOR_END"
340     exit
341 fi
342 }
343 
344 set_Centos7_eth0(){
345 sudo mv /etc/sysconfig/network-scripts/ifcfg-$NETWORK_NAME $Centos_NETWORK_DIR /dev/null
346 sudo sed -ri.bak 's/(GRUB_CMDLINE_LINUX=.*quiet).*/\1 net.ifnames=0 biosdevname=0"/' /etc/default/grub
347 eval sudo sed -ri.bak 's/${NETWORK_NAME}/eth0/g' $Centos_NETWORK_DIR
348 sudo grub2-mkconfig -o /boot/grub2/grub.cfg
349 if [ $? -eq 0 ];then
350     echo -e "$COLOR The network card name has been changed. about to reboot! $COLOR_END"
351     sudo reboot
352 else
353     echo -e "$COLOR The network card name change failed, please try again,exit! $COLOR_END"
354     exit
355 fi
356 }
357 
358 
359 set_Ubuntu1804_ip(){
360 read -p "Please enter ip address and netmask(IP/NETMASK): " IP
361 read -p "Please enter gateway address: " GATEWAY
362 read -p "Please enter dns address(DNS1;DNS2;...): " DNS
363 sudo chmod 666 /etc/netplan/00-installer-config.yaml
364 sudo cat > $Ubuntu_NETWORK_DIR << EOF
365 # This is the network config written by 'subiquity'
366 network:
367   ethernets:
368     eth0:
369       dhcp4: no
370       addresses: [$IP]
371       gateway4: $GATEWAY
372       nameservers:
373               addresses: [$DNS]
374   version: 2
375 EOF
376 if [ $? -eq 0 ]; then
377     echo -e "$COLOR IP address settings completed,the network service will be restarted soon.SSH users need to reconnect $COLOR_END"
378 else
379     echo -e "$COLOR IP address settings failed,please try again!exit $COLOR_END"
380     exit
381 fi
382 sudo netplan apply
383 sudo chmod 644 /etc/netplan/00-installer-config.yaml
384 }
385 
386 set_Centos7_ip(){
387 read -p "please input ip address: " IP
388 read -p "please input netmask address: " NETMASK
389 read -p "please input gateway address: " GATEWAY
390 read -p "Please input DNS address: " DNS1
391 sudo cat > $Centos_NETWORK_DIR << EOF
392 TYPE="Ethernet"
393 BOOTPROTO="static"
394 DEFROUTE="yes"
395 NAME="eth0"
396 DEVICE="eth0"
397 ONBOOT="yes"
398 IPADDR="$IP"
399 NETMASK="$NETMASK"
400 GATEWAY="$GATEWAY"
401 DNS1="$DNS1"
402 EOF
403 if [ $? -eq 0 ]; then
404     echo -e "$COLOR IP address settings completed,the network service will be restarted soon.SSH users need to reconnect $COLOR_END"
405 else
406     echo -e "$COLOR IP address settings failed,please try again!exit $COLOR_END"
407     exit
408 fi
409 sudo systemctl restart network
410 }
411 
412 
413 set_di(){
414 sudo sed -ri.bak 's/#set bell-style none/set bell-style none/' /etc/inputrc | sudo  grep '#set bell-style none' /etc/inputrc
415 if [ $? -eq 1 ];then
416     echo -e "$COLOR Mainboard alarm turned off,about to reboot! $COLOR_END"
417     sudo reboot
418 else
419     echo -e "$COLOR Mainboard alarm closing failed,please try again,exit! $COLOR_END"
420     exit
421 fi
422 }
423 
424 set_env(){
425 sudo echo 'PS1="[\[\e[1;32m\]\t\[\e[0m\] \u@\h \W]\\\$"' >> ~/.bashrc
426 if [ $? -eq 0 ]; then
427     echo -e "$COLOR The command prompt is modified. Please log in again! $COLOR_END"
428 else
429     echo -e "$COLOR Command prompt modification failed, please try again! $COLOR_END"
430 fi
431 }
432 
433 set_sudoers(){
434 echo -ne "$COLOR"
435 read -p "Please enter the name of the user to be authorized: " USERNAME
436 echo -ne "$COLOR_END"
437 cat > /etc/sudoers.d/$USERNAME <<EOF
438 $USERNAME ALL=(ALL) NOPASSWD: ALL
439 EOF
440 if [ $? -eq 0 ];then
441     echo -e "$COLOR The user authorization is completed and needs to log in again! $COLOR_END"
442 else
443     echo -e "$COLOR User authorization failed, please try again! $COLOR_END"
444 fi
445 }
446 
447 ubuntu_run(){
448 #PS3="Please enter the number(1-11):"
449 #MENU="
450 #优化sshd服务
451 #设置.vimrc文件
452 #设置主机名
453 #时间同步
454 #更改apt源
455 #常用软件安装
456 #设置网卡名称为eth0
457 #设置IP地址
458 #关闭主板报警
459 #设置命令提示符
460 #退出
461 #"
462 
463 
464 echo -ne "$COLOR"
465 cat << EOF
466 1) 优化sshd服务
467 2) 设置.vimrc文件
468 3) 设置主机名
469 4) 时间同步
470 5) 更改apt源
471 6) 常用软件安装
472 7) 设置网卡名称为eth0
473 8) 设置IP地址
474 9) 关闭主板报警
475 10) 设置命令提示符
476 11) 退出
477 EOF
478 echo -ne "$COLOR_END"
479 
480 read -p "Please enter the number(1-11): " MENU
481 #select menu in $MENU;do
482 case $MENU in
483 1)
484 #    if [[ `whoami` == "root" ]];then
485 #    set_sshd
486 #    ubuntu_run
487 #    else
488 #    systempasswd
489     set_sshd
490 #    fi
491     ;;
492 2)
493 #    if [[ `whoami` == "root" ]];then
494 #    set_vimrc
495 #    ubuntu_run
496 #    else
497 #    systempasswd
498     set_vimrc
499 #    fi
500     ;;
501 3)
502 #    if [[ `whoami` == "root" ]];then
503 #    set_hostname
504 #    ubuntu_run
505 #    else
506 #    systempasswd
507     set_hostname
508 #    fi
509     ;;
510 4)
511 #    if [[ `whoami` == "root" ]];then
512 #    set_date
513 #    ubuntu_run
514 #    else
515 #    systempasswd
516     set_date
517 #    fi
518     ;;
519 5)
520 #    if [[ `whoami` == "root" ]];then
521 #    set_apt
522 #    ubuntu_run
523 #    else
524 #    systempasswd
525     set_apt
526 #    fi
527     ;;
528 6)
529 #    if [[ `whoami` == "root" ]];then
530 #    set_Ubuntu1804_software_install
531 #    ubuntu_run
532 #    else
533 #    systempasswd
534     set_Ubuntu1804_software_install
535 #    fi
536     ;;
537 7)
538 #    if [[ `whoami` == "root" ]];then
539 #    set_Ubuntu1804_eth0
540 #    else
541 #    systempasswd
542     set_Ubuntu1804_eth0
543 #    fi
544     ;;
545 8)
546 #    if [[ `whoami` == "root" ]];then
547 #    set_Ubuntu1804_ip
548 #    ubuntu_run
549 #    else
550 #    systempasswd
551     set_Ubuntu1804_ip
552 #    fi
553     ;;
554 9)
555 #    if [[ `whoami` == "root" ]];then
556 #    set_di
557 #    ubuntu_run
558 #    else
559 #    systempasswd
560     set_di
561 #    fi
562     ;;
563 10)
564 #    if [[ `whoami` == "root" ]];then
565 #    set_env
566 #    ubuntu_run
567 #    else
568 #    systempasswd
569     set_env
570 #    fi
571     ;;
572 11)
573 ##    break
574     exit
575     ;;
576 *)
577     echo -e "$COLOR Input error, please re-enter! $COLOR_END"
578 esac
579 #done
580 }
581 
582 centos_root_run(){
583 #PS3="Please enter the number(1-12):"
584 #MENU="
585 #优化sshd服务
586 #设置.vimrc文件
587 #关闭Selinux及防火墙
588 #设置主机名
589 #时间同步
590 #更改yum源
591 #常用软件安装
592 #设置网卡名称为eth0
593 #设置IP地址
594 #关闭主板报警
595 #设置命令提示符
596 #退出
597 #"
598 
599 
600 echo -ne "$COLOR"
601 cat << EOF
602 1) 优化sshd服务
603 2) 设置.vimrc文件
604 3) 关闭Selinux及防火墙
605 4) 设置主机名
606 5) 时间同步
607 6) 更改yum源
608 7) 常用软件安装
609 8) 设置网卡名称为eth0
610 9) 设置IP地址
611 10) 关闭主板报警
612 11) 设置命令提示符
613 12) 设置epel源
614 13) 设置普通用户sudo权限
615 14) 退出
616 EOF
617 echo -ne "$COLOR_END"
618 
619 read -p "Please enter the number(1-14): " MENU
620 #select menu in $MENU;do
621 case $MENU in
622 1)
623 #    if [[ `whoami` == "root" ]];then
624 #    set_sshd
625 #    centos_run
626 #    else
627 #    systempasswd
628     set_sshd
629     centos_root_run
630 #    fi
631     ;;
632 2)
633 #    if [[ `whoami` == "root" ]];then
634 #    set_vimrc
635 #    centos_run
636 #    else
637 #    systempasswd
638     set_vimrc
639     centos_root_run
640 #    fi
641     ;;
642 3)
643 #    if [[ `whoami` == "root" ]];then
644 #    set_selinux_firewalld
645 #    centos_run
646 #    else
647 #    systempasswd
648     set_selinux_firewalld
649     centos_root_run
650 #    fi
651     ;;
652 4)
653 #    if [[ `whoami` == "root" ]];then
654 #    set_hostname
655 #    centos_run
656 #    else
657 #    systempasswd
658     set_hostname
659     centos_root_run
660 #    fi
661     ;;
662 5)
663 #    if [[ `whoami` == "root" ]];then
664 #    set_date
665 #    centos_run    
666 #    else
667 #    systempasswd
668     set_date
669     centos_root_run
670 #    fi
671     ;;
672 6)
673 #    if [[ `whoami` == "root" ]];then
674 #    set_yum
675 #    centos_run
676 #    else
677 #    systempasswd
678     set_yum
679     centos_root_run
680 #    fi
681     ;;
682 7)
683 #    if [[ `whoami` == "root" ]];then
684 #    set_Centos7_software_install
685 #    centos_run
686 #    else
687 #    systempasswd
688     set_Centos7_software_install
689     centos_root_run
690 #    fi
691     ;;
692 8)
693 #    if [[ `whoami` == "root" ]];then
694 #    set_Centos7_eth0
695 #    centos_run
696 #    else
697 #    systempasswd
698     set_Centos7_eth0
699 #    fi
700     ;;
701 9)
702 #    if [[ `whoami` == "root" ]];then
703 #    set_Centos7_ip
704 #    else
705 #    systempasswd
706     set_Centos7_ip
707 #    fi
708     ;;
709 10)
710 #    if [[ `whoami` == "root" ]];then
711 #    set_di
712 #    centos_run
713 #    else
714 #    systempasswd
715     set_di
716 #    fi
717     ;;
718 11)
719 #    if [[ `whoami` == "root" ]];then
720 #    set_env
721 #    centos_run
722 #    else
723 #    systempasswd
724     set_env
725     centos_root_run
726 #    fi
727    ;;
728 12)
729     set_epel
730     centos_root_run
731     ;;
732 13)
733     set_sudoers
734     centos_root_run
735     ;;
736 14)
737 #    break
738     exit
739     ;;
740 *)
741     echo -e "$COLOR Input error, please re-enter! $COLOR_END"
742 esac
743 #done
744 }
745 
746 centos_user_run(){
747 #PS3="Please enter the number(1-12):"
748 #MENU="
749 #优化sshd服务
750 #设置.vimrc文件
751 #关闭Selinux及防火墙
752 #设置主机名
753 #时间同步
754 #更改yum源
755 #常用软件安装
756 #设置网卡名称为eth0
757 #设置IP地址
758 #关闭主板报警
759 #设置命令提示符
760 #退出
761 #"
762 
763 
764 echo -ne "$COLOR"
765 cat << EOF
766 1) 优化sshd服务
767 2) 设置.vimrc文件
768 3) 关闭Selinux及防火墙
769 4) 设置主机名
770 5) 时间同步
771 6) 更改yum源
772 7) 常用软件安装
773 8) 设置网卡名称为eth0
774 9) 设置IP地址
775 10) 关闭主板报警
776 11) 设置命令提示符
777 12) 设置epel源
778 13) 退出
779 EOF
780 echo -ne "$COLOR_END"
781 
782 read -p "Please enter the number(1-14): " MENU
783 #select menu in $MENU;do
784 case $MENU in
785 1)
786 #    if [[ `whoami` == "root" ]];then
787 #    set_sshd
788 #    centos_run
789 #    else
790 #    systempasswd
791     set_sshd
792     centos_user_run
793 #    fi
794     ;;
795 2)
796 #    if [[ `whoami` == "root" ]];then
797 #    set_vimrc
798 #    centos_run
799 #    else
800 #    systempasswd
801     set_vimrc
802     centos_user_run
803 #    fi
804     ;;
805 3)
806 #    if [[ `whoami` == "root" ]];then
807 #    set_selinux_firewalld
808 #    centos_run
809 #    else
810 #    systempasswd
811     set_selinux_firewalld
812     centos_user_run
813 #    fi
814     ;;
815 4)
816 #    if [[ `whoami` == "root" ]];then
817 #    set_hostname
818 #    centos_run
819 #    else
820 #    systempasswd
821     set_hostname
822     centos_user_run
823 #    fi
824     ;;
825 5)
826 #    if [[ `whoami` == "root" ]];then
827 #    set_date
828 #    centos_run    
829 #    else
830 #    systempasswd
831     set_date
832     centos_user_run
833 #    fi
834     ;;
835 6)
836 #    if [[ `whoami` == "root" ]];then
837 #    set_yum
838 #    centos_run
839 #    else
840 #    systempasswd
841     set_yum
842     centos_user_run
843 #    fi
844     ;;
845 7)
846 #    if [[ `whoami` == "root" ]];then
847 #    set_Centos7_software_install
848 #    centos_run
849 #    else
850 #    systempasswd
851     set_Centos7_software_install
852     centos_user_run
853 #    fi
854     ;;
855 8)
856 #    if [[ `whoami` == "root" ]];then
857 #    set_Centos7_eth0
858 #    centos_run
859 #    else
860 #    systempasswd
861     set_Centos7_eth0
862 #    fi
863     ;;
864 9)
865 #    if [[ `whoami` == "root" ]];then
866 #    set_Centos7_ip
867 #    else
868 #    systempasswd
869     set_Centos7_ip
870 #    fi
871     ;;
872 10)
873 #    if [[ `whoami` == "root" ]];then
874 #    set_di
875 #    centos_run
876 #    else
877 #    systempasswd
878     set_di
879 #    fi
880     ;;
881 11)
882 #    if [[ `whoami` == "root" ]];then
883 #    set_env
884 #    centos_run
885 #    else
886 #    systempasswd
887     set_env
888     centos_user_run
889 #    fi
890    ;;
891 12)
892     set_epel
893     centos_user_run
894     ;;
895 13)
896 #    break
897     exit
898     ;;
899 *)
900     echo -e "$COLOR Input error, please re-enter! $COLOR_END"
901 esac
902 #done
903 }
904 
905 systempasswd(){
906 read -p "Pleace enter your system password: " PASSWD
907 echo $PASSWD | sudo -S echo ****** &> /dev/null
908 if [ $? -eq 0 ] ;then
909     set_run
910 else
911     echo -ne "$COLOR Please add sudo permission and try again! $COLOR_END"
912     exit
913 fi
914 }
915 
916 system_os(){
917 sudo sed -nr '/^ID=/s#ID="?([[:alpha:]]+)"?#\1#p' /etc/os-release
918 }
919 
920 set_run(){
921 if [[ `system_os` == 'centos' ]];then
922     if [[ `whoami` == 'root' ]];then
923         centos_root_run
924     else
925         centos_user_run
926     fi
927 elif [[ `system_os` == 'ubuntu' ]];then
928     ubuntu_run
929 else
930     echo -ne "$COLOR 无法识别该系统,请重试! $COLOR_END"
931 fi
932 }
933 
934 
935 if [[ `whoami` == "root" ]];then
936     set_run
937 else
938     systempasswd
939     set_run
940 fi

 

posted @ 2022-04-21 12:43  浩~浩  阅读(61)  评论(0编辑  收藏  举报