服务器的ssh配置
ssh作为登录服务器的第一道安全设置,一定要重视
一、服务器SSH服务安全设置
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 26 27 28
| $ vim /etc/ssh/sshd_config
PasswordAuthentication no
Port 2233
MaxAuthTries 3
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile ~/.ssh/authorized_keys
PermitEmptyPasswords no
GSSAPIAuthentication no
UsePAM no
X11Forwarding no
GatewayPorts yes
Subsystem sftp /usr/libexec/openssh/sftp-server -l INFO
UseDNS no
|
二、更改文件权限
1 2 3 4
| # 设置authorized_keys文件权限600 $ chmod 600 .ssh/authorized_keys # 设置.ssh目录权限700 $ chmod 700 .ssh
|
三、重启sshd服务
1
| $ systemctl restart sshd
|
四、查看日志
1 2
| $ tail -f /var/log/message $ tail -f /var/log/secure
|
五、禁止编辑文件-可选
1 2 3 4 5 6 7
| $ chattr +i /etc/ssh/sshd_config $ chattr +i /home/www/.ssh/authorized_keys
$ chattr -i /etc/ssh/sshd_config $ chattr -i /home/www/.ssh/authorized_keys
|
客户端的ssh配置
一、本地生成密钥对,-C备注信息
1
| ssh-keygen -t rsa -b 3072 -P '' -f ~/.ssh/id_rsa -C "zhangsan@centos" >/dev/null 2>&1
|
二、复制公钥到服务器
可以手动粘贴公钥内容到服务器上的.ssh/authorized_keys
文件中;也可以使用如下命令ssh-copy-id
自动复制
需要输入服务器的登录密码
默认22端口:ssh-copy-id -i id_rsa.pub 用户名@远程服务器IP
更改了端口:ssh-copy-id -i id_rsa.pub "-p 2233 用户名@远程服务器IP"
三、本地测试登录
ssh '用户名@远程服务器IP' -p 2233
四、客户端ssh快捷登录配置
建议先添加本地hosts地址解析,名称自定义,如websrv 远程服务器IP
配置个人ssh服务的config文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $ vim ~/.ssh/config
Host websrv HostName websrv Port 2233 User root PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa
TCPKeepAlive=yes ServerAliveInterval=15 ServerAliveCountMax=6 StrictHostKeyChecking=no Compression=yes ForwardAgent=yes
|
本地登录测试,可以简写为ssh websrv
附:阿里云配置实例
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 26 27
| $ grep "^[^#]" /etc/ssh/sshd_config
Port 2233 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key MaxAuthTries 3 RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys PermitEmptyPasswords no ChallengeResponseAuthentication no GSSAPIAuthentication no GSSAPICleanupCredentials no UsePAM no GatewayPorts yes X11Forwarding no AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server -l INFO UseDNS no AddressFamily inet PermitRootLogin yes SyslogFacility AUTHPRIV PasswordAuthentication no
|