Linux/CentOS服务器安全配置通用指南

2019-01-16 21:00:31于丽

/etc/pam.d/login后添加

auth required pam_tally2.so deny=6 unlock_time=180 even_deny_root root_unlock_time=180

登录失败5次锁定180秒,根据需要设置是否包括root。

5.5 Linux登录IP限制

(由于要与某一固定IP或IP段绑定,暂未设置)

更严格的限制是在sshd_config中定死允许ssh的用户和来源ip:

## allowed ssh users sysmgr AllowUsers sysmgr@172.29.73.* 或者使用tcpwrapper: vi /etc/hosts.deny sshd:all vi /etc/hosts.allow sshd:172.29.73.23 sshd:172.29.73.

6. Linux配置只能使用密钥文件登录

使用密钥文件代替普通的简单密码认证也会极大的提高安全性:

[dir@username ~]$ ssh-keygen -t rsa -b 2048 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): //默认路径,回车 Enter passphrase (empty for no passphrase): //输入你的密钥短语,登录时使用 Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 3e:fd:fc:e5:d3:22:86:8e:2c:4b:a7:3d:92:18:9f:64 root@ibpak.tp-link.net The key's randomart image is: +--[ RSA 2048]----+ | | … | o++o..oo..o| +-----------------+

将公钥重命名为authorized_key:

$ mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys

下载私钥文件 id_rsa 到本地(为了更加容易识别,可重命名为hostname_username_id_rsa),保存到安全的地方。以后 username 用户登录这台主机就必须使用这个私钥,配合密码短语来登录(不再使用 username 用户自身的密码)

另外还要修改/etc/ssh/sshd_config文件

打开注释

RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys

我们要求 username 用户(可以切换到其他用户,特别是root)必须使用ssh密钥文件登录,而其他普通用户可以直接密码登录。因此还需在sshd_config文件最后加入:

Match User itsection PasswordAuthentication no

重启sshd服务

# service sshd restart

另外提醒一句,这对公钥和私钥一定要单独保存在另外的机器上,服务器上丢失公钥或连接端丢失私钥(或密钥短语),可能导致再也无法登陆服务器获得root权限!

7. Linux减少history命令记录

执行过的历史命令记录越多,从一定程度上讲会给维护带来简便,但同样会伴随安全问题

vi /etc/profile

找到 HISTSIZE=1000 改为 HISTSIZE=50

或每次退出时清理history,history -c