!参考
由于之前的线上的版本是CentOS 6,鉴于该系统实在老旧目前正慢慢的往CentOS7和Ubuntu16上升级。

一般如果使用云服务器,厂商都会对镜像做一些优化,不太需要我们怎么操心。

新版的系统CentOS7和Ubuntu16对比CentOS6进步不是一点点,至少烦人的Python2和Gcc版本过低问题,在这里是完全没有了。

一、SSHD调整

1.1、配置文件

  • 备份配置文件

    1
    $ cp -pv /etc/ssh/sshd_config{,.old}
  • 修改端口

    1
    2
    $ sed -i "s/#Port 22$/Port 5831/g" /etc/ssh/sshd_config
    $ sed -i "s/Port 22$/Port 5831/g" /etc/ssh/sshd_config
  • 禁用 DNS查询

    1
    2
    3
    $ -i "s/#UseDNS yes/UseDNS no/g" /etc/ssh/sshd_config

    $ sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/g" /etc/ssh/sshd_config
  • 密钥登陆

    1
    2
    3
    4
    5
    $ sed -i "s/#AuthorizedKeysFile/AuthorizedKeysFile/g" /etc/ssh/sshd_config

    # 设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权。这通常是必要的,因为新手经常会把自己的目录和文件设成任何人都有写权限。

    $ sed -i "s/#StrictModes yes/StrictModes yes/g" /etc/ssh/sshd_config
  • 开启或者禁止密码登陆

    1
    2
    3
    4
    # 建议是能用密钥,就不需要在用密码了。如果是个人用户,保存好密钥,修改好端口那么服务器正常 就不会被人从SSHD这边攻破;
    # 如果是独立IP的企业用户或者有堡垒机,那么在开启密码登陆的时候 应该要做好IP/权限限制
    $ sed -i "s/#PasswordAuthentication yes/PasswordAuthentication yes/g" /etc/ssh/sshd_config$
    $ sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
  • 禁止空密码

    1
    2
    $ sed -i "s/PermitEmptyPasswords yes/PermitEmptyPasswords no/g" /etc/ssh/sshd_config
    $ sed -i "s/#PermitEmptyPasswords no/PermitEmptyPasswords no/g" /etc/ssh/sshd_config
  • 开启或者禁止root登陆

    1
    2
    3
    4
    # 现有的Redhat系的Linux都是默认允许root登陆的;
    # Ubuntu的Linux默认都是禁止root登陆(但是大部分的云服务器商,在初始化的时候都会默认开)。
    $ sed -i "s/#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config
    $ sed -i "s/PermitRootLogin no/PermitRootLogin yes/g" /etc/ssh/sshd_config
  • 最新版本的Redhat/CentOS 7.4已经弃用RSAAuthentication,这里就提一下。

    1
    $ sed -i "s/#RSAAuthentication yes/RSAAuthentication yes/g" /etc/ssh/sshd_config

    二、SSHD密钥生成

  • 直接使用ssh-keygen生成密钥

    1
    2
    3
    $ ssh-keygen -b 4096
    or
    $ ssh-keygen -t rsa -b 4096
  • 将生成好的私钥自己保存好,公钥复制到机器上去,比如:

    1
    2
    3
    4
    5
    $ ssh_rsa="ssh-dss AAAAB3NzaC1k ... Zk="
    $ mkdir /root/.ssh
    $ chmod 700 /root/.ssh
    $ echo ${ssh_rsa} >> /root/.ssh/authorized_keys
    $ chmod 600 /root/.ssh/authorized_keys

    三、常用的软件

    现在的云厂商在他们默认的镜像里面已经安装好大部分的软件包。

  • Redhat/CentOS

Redhat默认没有的依赖包还是很多的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#常用的epel源一般是要安装上去,如果你不放心这种非官方维护的源,那么就不要安装了。
$ yum -y install epel-release


# 生成缓存
$ yum makecache

#常用的工具包
$ yum -y install wget bash-completion zsh git mlocate man unzip procps-ng\
net-tools bind-utils tcpdump nmap mtr psmisc tree screen dmidecode\
at openssl openssl-devel readline-devel telnet ntpdate coreutils\
deltarpm nss

#接下是开发工具全家桶,基本上这些安装完,后面编译什么东西都很顺利了。。。
$ yum -y install vim gcc gcc-c++ make cmake autoconf automake \
texinfo pcre pcre-devel zlib-devel zlib ncurses ncurses-devel\
bison bison-devel curl curl-devel glibc glibc-devel glib2 glib2-devel\
bzip2 bzip2-devel libxslt libxslt-devel libxml2 libxml2-devel \
libxml2-static gd gd-devel freetype freetype-devel libjpeg libjpeg-devel\
libpng libpng-devel openldap openldap-devel openssl openssl-devel \
readline-devel expat-devel gettext-devel libicu-devel perl-Digest-SHA \
deltarpm perl-devel nss
  • Ubuntu

Ubuntu/Debian默认安装的依赖包基本是完全满足我们正常的需求了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ sudo apt-get update

#development tools
$ apt-get -y install build-essential git autoconf automake cmake pkg-config \
libperl-dev python-dev python3-dev python-pip python3-pip libssl-dev \
libpcre3-dev libpcrecpp0 libxslt1-dev libcurl4-openssl-dev libxml2 \
libxml2-dev libreadline-dev libreadline6-dev libtinfo-dev

#Security tools
$ apt-get -y install nmap libpam-pwquality

#network tools
$ apt-get -y install traceroute tcpdump nload

#imgs & font
$ apt-get -y install imagemagick libjpeg-dev libjpeg8-dev libpng12-dev libgd2-dev \
libfreetype6 libfreetype6-dev

#常用的工具
$ apt-get -y install unzip lrzsz screen tree sysv-rc-conf unattended-upgrades

$ apt-get -y autoremove
$ update-grub
  • 更新CA证书
    1
    $ update-ca-certificates --fresh
  • 设置自动更新安全补丁(可选)
    1
    2
    3
    4
    5
    $ vim /etc/apt/apt.conf.d/10periodic
    APT::Periodic::Update-Package-Lists "1";
    APT::Periodic::Download-Upgradeable-Packages "1";
    APT::Periodic::AutocleanInterval "7";
    APT::Periodic::Unattended-Upgrade "1";

    四、修改sudo, 支持sudo无密码(可选)

    主要是针对Ubuntu的用户,大部分的Redhat/CentOS用户都喜欢root。
    1
    2
    3
    4
    $ sudo cp -pv /etc/sudoers /etc/sudoers.old
    # 建议是用visudo来修改/etc/sudoers。
    $ sudo vim /etc/sudoers
    xiaoqiang ALL=(ALL) NOPASSWD: ALL

    五、网络

  • 时间同步

时间同步的话,建议如果对时间敏感的业务要做一个计划任务

1
$ /usr/sbin/ntpdate -u pool.ntp.org >/dev/null 2>&1;/sbin/hwclock --systohc >/dev/null 2>&1
  • 修改DNS

如果使用云服务器,正常是不建议直接修改他们已经配置好的DNS。

我们可以在原先的基础上加上几条用来备份。

  • Redhat/CentOS
    1
    2
    $ vim /etc/resolv.conf
    dns-nameservers 1.1.1.1
  • Ubuntu

不能直接修改/etc/resolv.conf(你下一次重启或者重启网络服务之后,就会失效,如果你需要一个临时的连接一个dns的话,那么这是一个好的选择)

方法1

1
2
$ sudo vim /etc/network/interfaces
dns-nameservers 1.1.1.1

方法2

1
2
$ sudo vim /etc/resolvconf/resolv.conf.d/base
nameserver 1.1.1.1

修改完之后保存,然后执行

1
$ sudo resolvconf -u

然后,你会发现/etc/resolv.conf文件中多了几行,这几行是resolve程序自动写入的:

1
2
3
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 1.1.1.1

六、系统优化

  • limits.conf

修改open files , 默认为1024

1
2
3
4
5
6
7
8
先使用`ulimit -n`查看当前的open files,如果是默认的值,则修改
$ sudo ulimit -n
$ sudo ulimit -n 65535
$ sudo vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
  • swapfile分区

小内存的机器,难免会用到swap,这里先看看怎么创建swap分区

1
2
3
4
$ sudo dd if=/dev/zero of=/tmp/swapfile06 bs=1M count=4000
$ sudo chmod 600 /tmp/swapfile06
$ sudo mkswap /tmp/swapfile06
$ sudo swapon /tmp/swapfile06

然后我们应该让系统优先使用内存,这样可以有效的避免OOM。

修改vm.swappiness,这个系统默认值为30。

1
2
3
4
$ sysctl -a | grep vm.swappines
$ sysctl -w vm.swappiness = 1
$ vim /etc/sysctl.conf
vm.swappiness = 1
  • sysctl.conf(可选)

sysctl.conf的调优就复杂了。网络上有很多关于sysctl.conf的调优教程。

个人认为那些教程有点误人子弟的味道,因为sysctl.conf涉及到内存,网络带宽,TCP/IP堆栈和虚拟内存系统等高级选项,用sysctl可以读取设置超过五百个系统变量。而且不同德内核版本sysctl.conf里面的参数也不一定相同。

在这种情况下其实是很难有一个通用的sysctl.conf来应对全部的业务场景。

下面是金山云Ubuntun16系统的sysctl.conf,经供参考。

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
$ cat sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 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 = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65535

net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth1.rp_filter = 0
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.eth1.arp_announce = 2
  • IP欺骗防护

    1
    2
    3
    4
    $ /etc/host.conf
    order hosts,bind
    multi on
    nospoof on

    七、密码复杂度

  • CentOS/Redhat的密码复杂度

    1
    2
    3
    4
    5
    6
    7
    8

    是通过配置/etc/pam.d/system-auth-ac或者/etc/pam.d/system-auth来实现的,下面是一个例子

    设置密码复杂度:密码至少14位;与前一次密码至少有三个位不同;大、小写字母,字数都至少2个。

    $ cp -rvp /etc/pam.d/system-auth{,.old}
    $ sed -i 's/password requisite/#password requisite/' /etc/pam.d/system-auth
    $ sed -i '/pam_cracklib.so/a \password requisite pam_cracklib.so try_first_pass retry=6 type= minlen=14 difok=3 ucredit=2 lcredit=2 dcredit=2' /etc/pam.d/system-auth
  • Ubuntu使用crablib实现系统密码复杂度管理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    安装软件包:

    $ apt-get install libpam-cracklib

    修改PAM配置文件:

    $ vim /etc/pam.d/common-password

    在 "password requisite pam_cracklib.so"后加上参数
    retry=N:用户最多可以几次输入密码后报错。默认是1次。
    difok=N:新密码有几个字符不能和旧密码相同,默认是5个。另外如果新密码有1/2的字符于旧不同,也会被接受。
    diginore=N:默认当新密码有23个字符时,difok选项会被忽略。
    minlen=N:最小密码长度。
    dcredit=N:阿拉伯数字个数。N>=0,代表新密码最多可以有多少个阿拉伯数字;N<0,最少要有多少个数字。
    ucredit=N:大写字母个数。N>=0,代表新密码最多可以有多少大写字母;N<0,最少要有多少个大写字母。
    lcredit=N:小写字母个数。N>=0,代表新密码最多可以有多少小写字母;N<0,最少要有多少个小写字母。
    ocredit=N:特殊字符个数。N>=0,代表新密码最多可以有多少特殊字符;N<0,最少要有多少个特殊字符。

    例如: 密码最多尝试3次,新密码最短8个字符,要有3个字符不能与旧密码相同,最少要有1个数字、1个大写字母、1个小写字母、1个特殊字符。
    password requisite pam_cracklib.so retry=3 minlen=8 difok=3 dcredit=-1 lcredit=-1 ocredit=-1 ucredit=-1

    八、防火墙/selinux

    防火墙学问就很深,改天写点教程吧,这里写不完的。。。

说说怎么临时和永久关闭Selinux(Selinux目前是redhat系的Linux预装的安全软件,功能十分强大,但是对新手还是要有一段时间的学习期)

  • 临时关闭
    1
    2
    3
    4
    5
    6
    $ getenforce
    Enforcing

    $ setenforce 0
    $ getenforce
    Permissive
  • 永久关闭:
    1
    2
    3
    4
    5
    $ vim /etc/sysconfig/selinux

    SELINUX=enforcing 改为 SELINUX=disabled

    重启系统reboot