返回首页

新 Linux 服务器的安全加固

DigitalOcean Droplet 上手安全配置:非 root 用户、SSH 端口、密钥登录。

发布 2018年3月14日 标签 #linux #security #ssh

~/posts/securing-new-linux-server $ cat post.md

/ 语言 EN / 中文
/ 主题 / /

概览

新建一台服务器之后,要做一些基础的安全处理:建一个 root 之外的用户、改 SSH 端口、关掉密码登录等等。本文以 DigitalOcean 的 Droplet 为例。

登录

第一步是 SSH 登录目标服务器。DigitalOcean 的 Droplet 会把 root 的初始密码发到你的邮箱,用它登录后系统会要求改 root 密码——尽量复杂,反正之后基本不会再用。

新建非 root 用户

addgroup manager
useradd -d /home/deploy -s /bin/bash -m deploy
passwd deploy
usermod -a -G manager deploy

这四条会新建 manager 用户组,再在这个组下建用户 deploy、设密码、加入用户组。deploy 这个用户名只是示例,你可以叫任何方便记忆的名字。后续的服务部署基本都通过这个非 root 账户进行。

visudo

默认会用 nano 打开 /etc/sudoers。需要在 root ALL=(ALL:ALL) ALL 这一行下面加:

deploy    ALL=(ALL) NOPASSWD: ALL

这样 deploysudo 时不需要再输入密码。要保留密码确认就用 deploy ALL=(ALL:ALL) ALL

以 DigitalOcean 的默认镜像为例,编辑后的 sudoers 大致如下(注意你看到的不一定完全一样,找到对应位置加一行即可):

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
deploy  ALL=(ALL) NOPASSWD: ALL    # <== 加这行

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo    ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

这一步做完之后就可以登出,改用 deploy 重新登录:

exit
ssh deploy@<your-ip>

SSH 安全配置

理论上应该禁掉 SSH 密码登录(只用公钥),安全性提升明显。但如果换了一台没有对应私钥的电脑就会非常麻烦,所以下面给两套方案。生产环境建议用第一套(禁密码登录)。

方案一:禁用 SSH 密码登录

在本机生成密钥并把公钥推到服务器:

ssh-keygen
ssh-copy-id user@host

登录服务器,先备份再编辑 sshd_config

sudo cp /etc/ssh/sshd_config ~
sudo nano /etc/ssh/sshd_config

DigitalOcean 默认镜像里的配置大致是这样(关注几行需要改的):

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22                        # ==> 改
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin yes            # ==> 改为 no
StrictModes yes

RSAAuthentication yes          # ==> 保持 yes
PubkeyAuthentication yes       # ==> 保持 yes
#AuthorizedKeysFile %h/.ssh/authorized_keys  # ==> 取消注释

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no        # ==> 保持 no

# Change to yes to enable challenge-response passwords
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication yes     # ==> 改为 no

# ... 其余略

UsePAM yes
UseDNS no                      # ==> 加这行
AllowUsers deploy              # ==> 加这行

需要的改动总结:

  1. 把端口改成 > 1024<= 65535 的某个值(我习惯用 35245 之类)。
  2. PermitRootLogin no
  3. PermitEmptyPasswords no
  4. PasswordAuthentication no
  5. UseDNS no
  6. RSAAuthentication yes
  7. PubkeyAuthentication yes
  8. AuthorizedKeysFile .ssh/authorized_keys
  9. AllowUsers deploy

以上是按其他镜像可能需要的全部步骤说的;DigitalOcean 默认就已经做好了其中几项。

改完重启 sshd:

sudo service ssh restart           # 任选其一
sudo /etc/init.d/ssh restart       # 任选其一

然后在本机的 ~/.ssh/config 里加一段:

Host my-server
    HostName <your-ip>
    User deploy
    Port <your-port>

之后直接 ssh my-server 就行。

方案二:保留密码登录

这样做安全性会显著低于方案一,自己权衡。

参考上面那段 sshd_config 示例,只改三处:

  1. 改端口(同上)。
  2. PermitRootLogin no
  3. AllowUsers deploy
返回首页