sshd服务安装与配置

sshd服务使用的安装包: 

OpenSSH  服务需要3  个软件包 

openssh-4.3p2-24.el5.i386.rpm:包含OpenSSH  服务器及客户端需要的核心文件 

openssh-clients-4.3p2-24.el5.i386.rpm:OpenSSH  客户端软件包 

openssh-server-4.3p2-24.el5.i386.rpm:OpenSSH  服务器软件包 

查看是否安装过ssh服务

 rpm -pqi openssh-server-5.3p1-70.el6.x86_64.rpm

或 

 rpm -qa | grep openssh 

OpenSSH  配置文件

OpenSSH  常用配置文件有两个 /etc/ssh/ssh_config/etc/ssh/sshd_configssh_config 为客户端配置文件 ;sshd_config  为服务器端配置文件 

服务管理命令: 

Centos 6: 

service sshd restart/stop/start/status  或  /etc/init.d/sshd restart/stop/start/status 

Centos 7、8:

systemctl  sshd restart/stop/start/status  sshd

开机自启服务: 

Centos 6: 

chkconfig sshd on   #设置开机自启

chkconfig sshd off    #关闭开机自启

chkconfig --list sshd  #查看服务是否开机自启

Centos 7、8:

systemctl enable sshd       #设置开机自启

systemctl disable sshd        #关闭开机自启

systemctl is-enabled sshd   #查看服务是否开机自启

服务端sshd配置文件说明:

Port 22             

#设置sshd监听端口号 ,可使用多个port,安全防护:建议修改port number为其它端口。防止别人暴力破解。

#如果启用了SELinux,需要添加对应端口例外,命令:semanage port -a -t ssh_port_t -p tcp 更改的端口号

ListenAddress 0.0.0.0 

#设置sshd服务器绑定的IP地址,0.0.0.0 表示侦听所有地址 ,如只在192.168.3.63网络接口上监听,就写成 ListenAddress 192.168.3.63

Protocol 2 

#  选择的  SSH  协议版本,可以是 1 也可以是 2 ,CentOS 5.x 预设是仅支持V2。   

#  如果想要支持旧版V1,就改为 Protocol 2,1  安全建议不建议使用1版本,存在漏洞

SyslogFacility AUTHPRIV 

#定义了ssh服务的日志类别为验证类别,ssh 默认日志存放在 /var/log/secure 

LogLevel INFO 

#登录记录的日志等级为INFO级别以上。

PermitRootLogin yes   

#是否允许  root  登录!预设是允许的,安全建议是设定成  no  ,通过其他普通账号远程上服务器后在切换成root用户使用   

PasswordAuthentication yes   

#密码验证当然是需要的!所以这里写  yes 。在设置秘钥认证登录后可以设置成no,禁止密码登录该操作需谨慎确保秘钥登录正常才可关闭!!!

PermitEmptyPasswords no   

#这项意思是是否允许以空的密码登录!默认不予许为no

LoginGraceTime 2m 

#当使用者连上SSH server之后,等待输入密码时间限制,若无单位则默认时间为秒!

PrintMotd yes   

#登入后是否显示一些提示信息,例如上次登入的时间、地点等等,默认是yes ,如要自定义内容请修改/etc/motd文件

PrintLastLog yes   

#显示上次登入的相关信息!默认是 yes 

UsePrivilegeSeparation yes   

#是否使用权限较低的程序来提供用户操作。由于sshd的服务程序是使用 root 的身份运行。

#开启后当普通用户mk登入后,会让sshd产生一个属于mk的sshd程序来使用,防止内存溢出导致系统安全问题

UseDNS yes   

#一般来说,为了要判断客户端来源是正常合法的,因此会使用DNS去反查客户端的主机名。如果是在内网互联,这项设置为no会让ssh登录速度快。

PidFile /var/run/sshd.pid   

#可以放置SSHD这个PID的文件位置,上述为默认值

常用案例:两台Linux使用秘钥互联

1、客户端生成密匙对

使用root帐号登录后执行ssh-keygen  生成密匙。

[root@loaclhost]# ssh-keygen 

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):     #秘钥生成存放位置,默认为 /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:

SHA256:QJLGnzlfGUrbw4g4Dj2/NEwv/pjpivyWUmkx7M3FILg root@10-255-1-38

The key's randomart image is:

+---[RSA 2048]----+

|   o...          |

|  . =o. . .      |

|   = +.B * o     |

|  E O B.* *      |

|   + % =S. .     |

|    * O o        |

|   o + +         |

| ...o o+         |

|  o+oo=..        |

+----[SHA256]-----+

2、发布公钥 

使用ssh-copy-id  命令将客户端生成的公钥发布到远程服务器192.168.1.63 ,幵使用 -i 参数指定本地公钥的存放位置。

ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

#命令的意思是将客户端生成的公钥 id_dsa.pub发送到远程服务器192.168.1.63的root用户,添加到root用户的 /root/.ssh/authorized_keys 文件中

TIM截图20200326023520.png

常用案例:Windows使用秘钥登录Linux  

相关文章链接:https://l-t.top/458.html

版权声明:
作者:WaterBear
链接:https://l-t.top/464.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>