十个常用的linux脚本命令(详解Linux系统标准化设置--脚本一键设置)

概述

Linux有很多标准化的内容,这里总结了下用一键脚本来设置,下面的命令请看懂后再执行。


Linux自动化部署需求

系统环境标准化

  • 标准化 - 字符集
  • 标准化 - 命令行
  • 标准化 - 内核参数
  • 标准化 - 系统参数

分区标准化

  • 标准化 - 标准化分区

系统配置标准化

  • 标准化 - yum环境
  • 标准化 - 系统服务
  • 标准化 - 主机名
  • 标准化 - VIM
  • 标准化 - 用户
  • 标准化 - SSH
  • 标准化 - 时间
  • 标准化 - SELINUX
  • 标准化 - 关闭ctrl alt del快捷键

软件标准化

  • 标准化 - 基础软件包
  • 标准化 - 常用软件包
  • 标准化 - 设置公钥

01系统环境标准化

1、系统环境标准化 - 字符集

需求:字符集设置 en_US.utf8

一键优化脚本配置

Centos 7.x

#update system character localectl set-locale LANG=en_US.utf8

注意 Centos 6 和 Centos 7 之间配置的差异

2、系统环境标准化 - 命令行

需求:配置shell,便于定位当前目录

一键优化脚本

#modify PS1 echo 'export PS1="[ \033[01;33m\u\033[0;36m@\033[01;34m\h \033[01;31m\w\033[0m ]\033[0m \n#"' >> /etc/profile

3、系统环境标准化 - 内核参数

需求:优化网卡 优化swap

一键优化脚本配置

#tune kernel parametres cat >> /etc/sysctl.conf << EOF net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.ip_local_port_range = 10000 65000 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_fin_timeout = 30 vm.swappiness=10 vm.max_map_count = 262144 EOF

4、系统环境标准化 - 系统参数

需求:打开文件描述符

一键优化脚本配置

#set the file limit cat >> /etc/security/limits.conf << EOF * soft nofile 65535 * hard nofile 65535 EOF


02分区标准化

1、分区标准化 - 标准化分区

需求:

  • 虚拟机或服务器

/boot 200M~1G /swap 1G~8G / 剩余全部

  • Oracle服务器

/boot 200M~1G /swap >16G / 40G /data(Oracle数据目录) 剩余全部


03系统配置标准化

1、系统配置标准化 - YUM环境

需求:

  • 使用内网YUM源
  • 如无内网YUM源,配置外网YUM源

一键优化脚本

假设没有内网repo,就配置阿里YUM源

#clean OS default repo mkdir /etc/yum.repos.d/old && mv /etc/yum.repos.d/C* /etc/yum.repos.d/old/ #add repo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo function_writelog_judgment "[add aliyun mirrors base]"

2、系统配置标准化 - 系统服务

需求:关闭无用服务

一键优化脚本

Centos 7.x

目前无优化方案

Centos 6.x

#set system start service LANG=en for chkoff in `chkconfig --list|grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $chkoff off;done for chkoff in crond network rsyslog sshd;do chkconfig --level 3 $chkoff on;done

3、系统配置标准化 - VIM

需求:VIM基础配置,并增加易读性

一键优化脚本(结尾拷贝到普通用户环境变量)

#modify vimrc cat >> /root/.vimrc << EOF syntax enable syntax on set ruler set number set cursorline set cursorcolumn set hlsearch set incsearch set ignorecase set nocompatible set wildmenu set paste set nowrap set expandtab set tabstop=2 set shiftwidth=4 set softtabstop=4 set gcr=a:block-blinkon0 set guioptions-=l set guioptions-=L set guioptions-=r set guioptions-=R highlight CursorLine cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE highlight CursorColumn cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE EOF cp /root/.vimrc /home/oracle/

4、系统配置标准化 - 用户

需求:

  • 建立日常管理用户
  • 为用户设置sudo权限
  • 所有用户使用相同密码(无CMDB情况下)
  • 用户设置强密码

一键优化脚本

#add default user useradd hwb -u 2019 echo 'hwb@)!&' | passwd --stdin hwb && history -c #set sudo authority echo "" >> /etc/sudoers echo "#set sudo authority" >> /etc/sudoers echo "hwb ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

5、系统配置标准化 - SSH

需求:

  • 禁止Root远程登录
  • 关闭DNS解析
  • 不允许空密码
  • 修改SSH默认端口
  • 关闭GSSAPI校验

一键优化脚本

\cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date %F` sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config sed -i 's%#PermitRootLogin yes%PermitRootLogin no%g' /etc/ssh/sshd_config sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%g' /etc/ssh/sshd_config #sed -i 's%#Port 22%Port 52020%g' /etc/ssh/sshd_config

6、系统配置标准化 - 时间

需求

  • 所有服务器每分钟与时间服务器进行同步

一键优化脚本

echo "* 4 * * * /usr/sbin/ntpdate ${ntp_server}> /dev/null 2>&1" >> /var/spool/cron/root

7、系统配置标准化 - Selinux

需求:关闭selinux

一键优化脚本

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

8、系统配置标准化 - 关闭ctrl alt del快捷键

需求:关闭ctrl alt del快捷键

一键优化脚本

centos 7.x

mv /usr/lib/systemd/system/ctrl-alt-del.target /usr/lib/systemd/system/ctrl-alt-del.target.bak

centos 6.x

mv /etc/init/control-alt-delete.conf /etc/init/control-alt-delete.conf.bak


04软件标准化

1、软件标准化 - 常用软件包

需求:安装常用软件

一键优化脚本

yum -y install ntp wget tree telnet sysstat sysstat iptraf ncurses-devel openssl-devel zlib-devel OpenIPMI-tools nmap screen

2、软件标准化 - 升级软件包

需求:升级当前软件包

一键优化脚本

yum -y update

3、配套软件标准化 - 公钥(管理机免密钥)

需求:

  • 设置管理机(Ansible)的公钥
  • 便于上线后,进行个性化配置(配置文件修改)

一键优化脚本

wget http://xx.xx.xx.xx/ansible_key -O /tmp/ansible_key cat /tmp/ansible_key >> /home/hwb/.ssh/authorized_keys rm -f /tmp/ansible_key


篇幅有限,这块就先介绍到这了,如果大家想做公司标准化这一块,建议还是根据公司实际情况去具体设置,最后再根据需求统一写成一个shell脚本,这样一个标准化的脚本就建立起来了。后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注一下~

,

免责声明:本网站为个人非盈利性网站,内容和图片均来源于网络,如内容或图片侵犯了您的权益,请及时与本站联系删除!