RHCE——配置和保护OPENSSH服务

来源:互联网 发布:淘宝代购网店怎么开 编辑:程序博客网 时间:2024/06/02 03:09

SSH(OpenSSH Secure Shell)

Secure Shell示例

远程连接到服务器上

[student@localhost 桌面]$ ssh root@xx.xx.xx.xxroot@120.24.222.231's password: Last login: Sun Jul 30 21:05:09 2017 from 223.11.69.68Welcome to Alibaba Cloud Elastic Compute Service !

W命令可现实当前登陆到计算机的用户列表。
显示那些用户使用了SSH的登陆位置,以及操作内容

[root@iZtoz99sjuwqcnZ ~]# w -f 21:23:01 up 2 days,  4:51,  4 users,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     tty1      五16    2days  8.93s  0.00s xinit /etc/X11/xinit/xinitrc --root     pts/016    2days  0.04s  0.00s mysql -u root -proot     pts/1     21:05   17:25   0.03s  0.03s -bashroot     pts/2     21:20    5.00s  0.03s  0.00s w -f

SSH主机密钥

SSH通过公钥加密的方式保持通信安全。
在本地的 ~/.ssh/known_hosts 存储着之前连接服务器的公钥
如果服务器的公钥发生改变,用户需要更新~/.ssh/known_hosts 才能继续登陆
主机ID存储在实体客户端系统上的~/.ssh/known_hosts

[student@localhost 桌面]$ cat ~/.ssh/known_hosts 120.24.222.231 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXN....

主机密钥存储在SSH服务器上的/etc/ssh/ssh_host_ecdsa_key

[root@iZtoz99sjuwqcnZ ssh]# cat /etc/ssh/ssh_host_ecdsa_key-----BEGIN EC PRIVATE KEY-----MHcCAQEEIHlZ3jd8bCw/xn4/00tgtLGGfI52Lqt83Z6Y1RBy9BiuoAoGCCqGSM49AwEHoUQDQgAEtLBBvKVSy4mpj+AI4of09fNtNZ8ky5JUhiKzLQ5+y6SZ85P4SJzfJ7K6hHOD6XpaFcXg51MqI45icAVuFgkKuQ==-----END EC PRIVATE KEY-----

配置基于SSH密钥的身份验证

使用ssh-keygen命令生成密钥。这将生成密钥~/.ssh/id_rsa~/.ssh/id_rsa.pub.

SSH密钥演示

  1. 以Student的身份创建密钥对
  2. 将SSH公钥发送到服务器上账户上
  3. 运行SSH登陆命令,则不需要密码
[student@localhost 桌面]$ ssh-keygen[student@localhost 桌面]$ ssh-copy-id root@XX.xx.xx.xx[student@localhost 桌面]$ ssh root@xx.xx.xx.xx
[student@localhost 桌面]$ ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/home/student/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/student/.ssh/id_rsa.Your public key has been saved in /home/student/.ssh/id_rsa.pub.The key fingerprint is:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xxstudent@localhost.localdomainThe key's randomart image is:+--[ RSA 2048]----+|                 ||     .   .       ||    . o . .      ||   o o * o       ||  . . O S .      ||     = + o       ||    . . +        ||    .o + .       ||    E.o          |+-----------------+[student@localhost 桌面]$ ssh-copy-id root@XX.xx.xx.xx/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysroot@xx.xx.xx.xx's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'root@xx.xx.xx.xx'"and check to make sure that only the key(s) you wanted were added.[student@localhost 桌面]$ ssh root@xx.xx.xx.xxLast login: Sun Jul 30 21:44:34 2017 from 223.11.69.68Welcome to Alibaba Cloud Elastic Compute Service !

自定义SSH服务配置

配置文件在 /etc/ssh/sshd_config中,可以修改OpenSSH服务的各方面

禁止root用户使用SSH登陆

将默认的注释的许可root登陆去掉注释,然后在参数上改为no

[root@localhost ~]# vim /etc/ssh/sshd_config #Permitrootlogin yes //将这条语句改成下一条语句Permitrootlogin no[root@localhost ~]# systemctl restart sshd.service

另一选项是仅允许root身份进行基于密钥的ssh登陆

[root@localhost ~]# vim /etc/ssh/sshd_config #Permitrootlogin yes //将这条语句改成下一条语句Permitrootlogin without-password[root@localhost ~]# systemctl restart sshd.service

禁止使用SSH进行密码身份验证

[root@localhost ~]# vim /etc/ssh/sshd_config PasswordAuthentication yes //将这条语句改成下一条语句PasswordAuthentication no[root@localhost ~]# systemctl restart sshd.service

sshd_config参数解释

更改登陆端口

参数 解释 Port 22 SSH 预设使用 22 这个 port,您也可以使用多的 port ! Protocol 2,1 选择的 SSH 协议版本,可以是 1 也可以是 2 ,如果要同时支持两者,就必须要使用 2,1 这个分隔了! LoginGraceTime 600 当使用者连上 SSH server 之后,会出现输入密码的画面,在该画面中,在多久时间内没有成功连上 SSH server ,就断线!时间为秒! Compression yes 是否可以使用压缩指令

说明主机的 Private Key 放置的档案

参数 解释 HostKey /etc/ssh/ssh_host_key SSH version 1 使用的私钥 HostKey /etc/ssh/ssh_host_rsa_key SSH version 2 使用的 RSA 私钥 HostKey /etc/ssh/ssh_host_dsa_key SSH version 2 使用的 DSA 私钥

安全设定项目

登入设定部分

参数 解释 PermitRootLogin no 是否允许 root 登入!预设是允许的,但是建议设定成 no! UserLogin no 在 SSH 底下本来就不接受 login 这个程序的登入! StrictModes yes 当使用者的 host key 改变之后,Server 就不接受联机, 可以抵挡部分的木马程序! PubkeyAuthentication yes 是否允许 Public Key

认证部分

参数 解释 RhostsAuthentication no 本机系统不止使用 .rhosts ,因为仅使用 .rhosts 太不安全了,所以这里一定要设定为 no ! PasswordAuthentication yes 密码验证当然是需要的!所以这里写 yes PermitEmptyPasswords no 这个项目在是否允许以空的密码登入

sshd_config具体配置

原创粉丝点击