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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
| #!/bin/bash
PORT=22 ALLOW_ROOT_LOGIN=yes ALLOW_PASS_LOGIN=yes
ROOT_PASS="123456"
HOSTNAME=chengdu-huayang-linux39-template-0-167.magedu.local
IPADDR=192.168.0.123 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 DNS1=223.6.6.6 AUTHOR=magedu QQ=1062670898,2967305595 DESCRIBE="A test Script from www.magedu.com" CURRENT_DIR=$(dirname $(readlink -f $0))
set_vimrc() { echo "开始配置vimrc环境" cat>~/.vimrc<<EOF set nu set cul set tabstop=2 set expandtab set shiftwidth=2 set ai set softtabstop=2 map <F2> :set paste <CR> o map <F3> :set expandtab <CR> :%retab! <CR> :wq! <CR> map <F4> :set binary <CR> :set noendofline <CR> :wq! <CR> autocmd BufNewFile *.sh exec ":call SetTitle()" function SetTitle() if expand("%:e") == 'sh' call setline(1,"#!/bin/bash") call setline(2,"#") call setline(3,"#********************************************************************") call setline(4,"#Author: $1") call setline(5,"#QQ: $2") call setline(6,"#Date: ".strftime("%Y-%m-%d")) call setline(7,"#FileName: ".expand("%")) call setline(8,"#URL: http://blog.mykernel.cn") call setline(9,"#Description: $3") call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved") call setline(11,"#********************************************************************") call setline(12,"") endif endfunc autocmd BufNewFile * normal G EOF
echo "配置成功" }
set_chinese() { echo "开始配置中文" yum -y install kde-l10n-Chinese glibc-common localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 echo ' export LANG=zh_CN.utf8 ' > /etc/profile.d/chinese.sh echo "配置成功" } set_chinese_ubuntu() { echo "开始配置中文" apt update if [ $? -ne 0 ]; then pkill apt-get pkill apt-get apt update fi apt-get install language-pack-zh* -y echo 'LANG="zh_CN.UTF-8"' > /etc/default/locale dpkg-reconfigure --frontend=noninteractive locales update-locale LANG=zh_CN.UTF-8 echo "配置成功" }
set_openssh_server() { echo "开始配置openssh-server" cat > /etc/ssh/sshd_config <<EOF Port ${1:-22} HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV PermitRootLogin ${2:-prohibit-password} AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication ${3:-yes} #PubkeyAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes UseDNS no AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server EOF echo "配置成功" } set_openssh_server_ubuntu() { echo "开始配置openssh-server" cat > /etc/ssh/sshd_config <<EOF Port ${1:-22} HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV PermitRootLogin ${2:-prohibit-password} AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication ${3:-yes} #PubkeyAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes UseDNS no AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/lib/openssh/sftp-server EOF echo "配置成功" }
set_root_passwd() { echo "开始配置root密码" echo "$1" | passwd --stdin root echo "配置成功" } set_root_passwd_ubuntu() { echo "开始配置root密码" echo "root:$1" | chpasswd echo "配置成功" }
set_hostname() { echo "开始配置主机名" echo "$1" > /etc/hostname hostnamectl set-hostname "$1" echo "配置成功" }
set_ethX() { echo "开始配置网卡名" sed -Ei.bak '/GRUB_CMDLINE_LINUX=/s/quiet.*$/quiet net.ifnames=0 biosdevname=0"/' /etc/default/grub grub2-mkconfig -o /boot/grub2/grub.cfg > /dev/null echo "配置成功" } set_ethX_ubuntu() { echo "开始配置网卡名" sed -Ei.bak '/GRUB_CMDLINE_LINUX=/s/quiet.*$/quiet net.ifnames=0 biosdevname=0"/' /etc/default/grub update-grub echo "配置成功" }
mask2cdr () { local x=${1##*255.} set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*} x=${1%%$3*} echo $(( $2 + (${#x}/4) )) }
set_ip() { echo "开始配置IP" cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF TYPE=Ethernet BOOTPROTO=static DEVICE=eth0 ONBOOT=yes IPADDR=$1 NETMASK=$2 GATEWAY=$3 #DNS1=192.168.0.1 EOF
cat > /etc/resolv.conf <<EOF nameserver $4 EOF
echo "配置成功" } set_ip_ubuntu() { echo "开始配置IP"
cat > /etc/netplan/01-netcfg.yaml <<EOF # This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: eth0: dhcp4: no addresses: [$1/$(mask2cdr $2)] gateway4: $3 nameservers: addresses: [$4] EOF netplan apply echo "配置成功" }
set_install_basepkgs() { echo "开始安装常用命令" yum remove -y NetworkManager* firewalld*
echo 'nameserver 114.114.114.114' > /etc/resolv.conf yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop ntpdate lsof echo "配置成功"
echo "关闭selinux" sed -Ei.bak '/SELINUX=/s/(SELINUX=)enforcing/\1disabled/' /etc/selinux/config echo "配置成功" }
set_install_basepkgs_ubuntu() { apt purge ufw lxd lxd-client lxcfs lxc-common -y apt 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 make openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip lsof make curl iputils-ping net-tools -y }
set_limit() { echo "开始优化系统资源限制" cat > /etc/security/limits.conf <<'EOF' root soft core unlimited root hard core unlimited root soft nproc 1000000 root hard nproc 1000000 root soft nofile 1000000 root hard nofile 1000000 root soft memlock 32000 root hard memlock 32000 root soft msgqueue 8192000 root hard msgqueue 8192000 * soft core unlimited * hard core unlimited * soft nproc 1000000 * hard nproc 1000000 * soft nofile 1000000 * hard nofile 1000000 * soft memlock 32000 * hard memlock 32000 * soft msgqueue 8192000 * hard msgqueue 8192000 EOF echo "配置成功" }
set_kernel_params() { cat > /etc/sysctl.conf <<'EOF'
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-arptables = 1
fs.may_detach_mounts = 1
kernel.msgmnb = 65536 kernel.msgmax = 65536
kernel.shmmax = 68719476736 kernel.shmall = 4294967296
net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_sack = 1 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144
net.core.somaxconn = 20480 net.core.optmem_max = 81920
net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_syn_retries = 3 net.ipv4.tcp_retries1 = 3 net.ipv4.tcp_retries2 = 15
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0 net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
vm.overcommit_memory = 1
vm.panic_on_oom=0 vm.swappiness = 10
net.ipv4.tcp_mem = 786432 1048576 1572864 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.ip_local_port_range = 10001 65000
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963 EOF echo "配置成功" }
set_mirrors_aliyun_ubuntu() { sed -i.bak -e 's@us.archive.ubuntu.com@mirrors.aliyun.com@g' -e 's@security.ubuntu.com@mirrors.aliyun.com@g' /etc/apt/sources.list apt update }
set_time_sync() { echo "开始配置时间同步" echo '*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &> /dev/null' > /var/spool/cron/root echo "配置成功" }
set_time_sync_ubuntu() { echo "开始配置时间同步" echo '*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &> /dev/null' > /var/spool/cron/crontabs/root echo "配置成功" }
function usage { cat << END $(basename $0) OPTION ... --port=<port> ssh服务监听的端口, 默认22 --allow-root-login=<prohibit-password|yes> ssh配置是否让root能登陆,默认prohibit-password. --allow-pass-login=<yes|no> ssh配置是否让pass能登陆,默认yes --root-password=<root_pass> 配置root登陆密码,默认123456 --hostname=<hostname> 配置主机名,cd-hy-linux39-centos-0-167.magedu.local --ipaddr=<ipaddr> 配置IP, 默认192.168.0.123 --netmask=<netmask> 配置掩码, 默认255.255.255.0 --gateway=<gateway> 配置网关,默认192.168.0.1 --dns=<dns> 配置DNS, 默认223.6.6.6 --author=<author> 配置vimrc生成脚本的作者, 默认magedu --qq=<author> 配置vimrc生成脚本的qq, 我的班主任萌萌老师: 2967305595 --desc=<desc> 配置vimrc生成脚本的描述, 默认: A test Script from magedu --chinese= 配置中文, openstack python2, 不要启用 --eth0= 配置eth0接口 --resourceslimit= 配置资源限制 --kernelparams= 配置内核优化 --basepkgs= 配置基础包 --umirror= 配置镜像加速, only ubuntu
# 所有功能 非中文 # bash $(basename $0) \ --port=22 --allow-root-login=yes --allow-pass-login=yes \ --root-password=123456 \ --hostname=ubuntu-template.magedu.local \ --ipaddr=192.168.0.123 --netmask=255.255.255.0 --gateway=192.168.0.1 \ --dns=223.6.6.6 \ --author=songliangcheng --qq=2192383945 --desc="A test toy" \ --resourceslimit=1 \ --kernelparams=1 \ --basepkgs=1 \ --chinese=0 \ --eth0=0 \ --umirror=1
示例:只配置主机名和vimrc和参数优化 中文 # bash $(basename $0) \ --hostname=ubuntu-template.magedu.local \ --author=songliangcheng --qq=2192383945 --desc="A test toy" \ --resourceslimit=1 \ --kernelparams=1 \ --basepkgs=1 \ --chinese=1 \ --eth0=0 \ --umirror=1 示例: 无操作 bash -x linux_template_install.sh --resourceslimit=0 --kernelparams=0 --basepkgs=0 --chinese=0 --eth0=0 --umirror=0
END
}
ssh=0 pass=0 hname=0 vimrc=0 net=0 chinese=0 eth0=0 resourceslimit=0 kernelparams=0 basepkgs=0 umirror=0 for option do case "$option" in -*=*) value=`echo "$option" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;; *) value="" ;; esac
case "$option" in --help) usage exit 1 ;; --port=*) PORT="$value" ssh=1 ;; --allow-root-login=*) ALLOW_ROOT_LOGIN="$value" ssh=1 ;; --allow-pass-login=*) ALLOW_PASS_LOGIN="$value" ssh=1 ;; --root-password=*) ROOT_PASS="$value" pass=1 ;; --hostname=*) HOSTNAME="$value" hname=1 ;; --ipaddr=*) IPADDR="$value" net=1 ;; --netmask=*) NETMASK="$value" net=1 ;; --gateway=*) GATEWAY="$value" net=1 ;; --dns=*) DNS1="$value" net=1 ;; --author=*) AUTHOR="$value" vimrc=1 ;; --qq=*) QQ="$value" vimrc=1 ;; --desc=*) DESCRIBE="$value" vimrc=1 ;; --chinese=*) chinese="$value" ;; --eth0=*) eth0="$value" ;; --resourceslimit=*) resourceslimit="$value" ;; --kernelparams=*) kernelparams="$value" ;; --basepkgs=*) basepkgs="$value" ;; --umirror=*) umirror="$value" ;; *) echo "$0: error: invalid option \"$option\"" usage exit 1 ;; esac done
[ -z "$option" ] && echo "$0: error: invalid option \"$option\"" && exit 1
centos_init() { if [ $vimrc -eq 1 ]; then
set_vimrc "$AUTHOR" "$QQ" "$DESCRIBE" fi
if [ $chinese -eq 1 ]; then
set_chinese fi
if [ $ssh -eq 1 ]; then
set_openssh_server $PORT $ALLOW_ROOT_LOGIN $ALLOW_PASS_LOGIN fi
if [ $pass -eq 1 ]; then
set_root_passwd "$ROOT_PASS" fi
if [ $hname -eq 1 ]; then
set_hostname "$HOSTNAME" fi
if [ $eth0 -eq 1 ]; then set_ethX fi
if [ $net -eq 1 ]; then
set_ip "$IPADDR" "$NETMASK" "$GATEWAY" "$DNS1" fi
if [ $basepkgs -eq 1 ]; then
set_install_basepkgs fi if [ $resourceslimit -eq 1 ]; then
set_limit fi if [ $kernelparams -eq 1 ]; then set_kernel_params fi
set_time_sync
if ! grep -q root_unlock_time=300 /etc/pam.d/sshd; then sed -i '/^#%PAM-1.0/a\auth required pam_tally2.so deny=3 unlock_time=300 even_deny_root root_unlock_time=300' /etc/pam.d/sshd fi } ubuntu_init() {
if [ $umirror -eq 1 ]; then
set_mirrors_aliyun_ubuntu fi
if [ $chinese -eq 1 ]; then
set_chinese_ubuntu fi
if [ $vimrc -eq 1 ]; then set_vimrc "$AUTHOR" "$QQ" "$DESCRIBE" fi
if [ $ssh -eq 1 ]; then set_openssh_server_ubuntu $PORT $ALLOW_ROOT_LOGIN $ALLOW_PASS_LOGIN fi
if [ $pass -eq 1 ]; then set_root_passwd_ubuntu "$ROOT_PASS" fi
if [ $hname -eq 1 ]; then set_hostname "$HOSTNAME" fi
if [ $eth0 -eq 1 ]; then set_ethX_ubuntu fi if [ $net -eq 1 ]; then set_ip_ubuntu "$IPADDR" "$NETMASK" "$GATEWAY" "$DNS1" fi if [ $basepkgs -eq 1 ]; then set_install_basepkgs_ubuntu fi if [ $resourceslimit -eq 1 ]; then
set_limit fi
if [ $kernelparams -eq 1 ]; then set_kernel_params fi
set_time_sync_ubuntu }
sed -i 's/^HISTSIZE=.*$/HISTSIZE=10000/g' /etc/profile
echo 'export HISTTIMEFORMAT="%F %T `whoami` "' > /etc/profile.d/secuirty.sh
echo TMOUT=600 >> /etc/profile.d/secuirty.sh
if which apt &> /dev/null; then ubuntu_init else centos_init fi cat << EOF # reboot 请reboot, 验证ip, 域名解析, kernel params, crontab, ssh, alias EOF
cd $CURRENT_DIR rm -f $0
|