Linux基础优化

Linux基础优化与安全重点小结

  1. 不用root登录管理系统,而以普通用户登录通过sudo授权管理。
    更改默认的远程连接SSH服务端口,禁止root用户远程连接,甚至要更改SSH服务只监听内网IP。
    (/etc/ssh/ssh_config 修改 prot 22)保存重启
    注意iptables让自己允许连接
  1. 定时自动更新服务器的时间,使其和互联网时间同步。
    crontab -e crontab -l
    echo '*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' >/var/spool/cron/root
  1. 配置yum更新源,从国内更新源下载安装软件包。
    默认国外的yum源(软件仓库)比较慢,所以换成国内的。
    备份
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    下载新的CentOS-Base.repo 到/etc/yum.repos.d/
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
    CentOS 6
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
    或者
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
    CentOS 7
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    或者
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    之后运行yum makecache生成缓存,此步骤可以不执行。
  1. 备份(如有配置其他epel源)
    mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
    mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
    下载新repo 到/etc/yum.repos.d/
    epel(RHEL 7)
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    epel(RHEL 6)
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
    更换查看帮助的网站mirrors.aliyun.com
  1. 关闭SELinux及iptables(在工作场景中,如果有外部IP一般要打开iptables,高并发高流量的服务器可能无法开启)。
    SELinux(Security-Enhanced Linux)是美国国家安全局(NSA)对于强制访问控制的实现,这个功能让系统管理员又爱又恨,这里我们还是把它给关闭了吧,至于安全问题,后面通过其他手段来解决,这也是大多数生产环境的做法。

关闭方式如下

永久关闭selinux
# 备份
cp /etc/selinux/config /etc/selinux/config.bak
# sed修改,看看结果,不加-i
sed 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
# 确认并使用 sed -i 修改文件内容
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
# 检查结果
grep "disabled" /etc/selinux/config
临时关闭selinux
setenforce 0
# 数字0表示Permissive,即给出警告提示,但不会阻止操作,相当于disabled。
# 数字1表示Enforcing,即表示SElinux为开启状态。
getenforce # 查看命令
命令说明:
setenforce:用于命令行管理SELinux的级别,后面的数字表示设置对应的级别。
getenforce:查看SELinux当前的级别状态。

提示:修改配置SElinux后,要想使其生效,必须要重启系统。因此,可配合使用setenforce 0这个临时使其关闭的命令,这样在重启前后都可以使得SElinux关闭生效了,也就是说无须立刻重启服务器了,在生产场景下Linux机器是不能随意重启的(不要给自己找任何理由重启)。

  • 调整文件描述符的数量,进程及文件的打开都会消耗文件描述符数量。
  • 定时自动清理邮件临时目录垃圾文件,防止磁盘的inodes数被小文件占满(注意Centos6和Centos5要清除的目录不同)。
  • 精简并保留必要的开机自启动服务(如crond、sshd、network、rsyslog、sysstat)。
  • Linux内核参数优化/etc/sysctl.conf,执行sysctl -p生效。
  • 更改系统字符集为“zh_CN.UTF-8”,使其支持中文,防止出现乱码问题。
  • 锁定关键系统文件如/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow、/etc/inittab,处理以上内容后把-chattr、lsattr改名为luffy,转移走,这样就安全多了。
  • 清空/etc/issue、/etc/issue.net,去除系统及内核版本登录前的屏幕显示。
  • 清除多余的系统虚拟用户账号。
  • 为grub引导菜单加密码。
  • 禁止主机被ping。
  • 打补丁并升级有已知漏洞的软件。 新系统 yum –y install 已经在线上用的服务器 web服务器能够停止。

你可能感兴趣的:(Linux基础优化)