linux进阶-你不知道的sshd服务器的功能和配置

shd服务的功能和配置

1.ssh服务器端

查询openssh-server

[root@centos7 ~]# rpm -qa openssh-server
openssh-server-7.4p1-21.el7.x86_64
要想通过ssh链接远程服务器,对方服务器需要安装ssh服务器端程序,并开启对应服务。
CentOS中默认使用的是openssh-server,进程名称为sshd,默认监听的网络端口是22。

sshd服务有对应的配置⽂件,位置是/etc/ssh/sshd_config

[root@centos7 ~]# rpm -ql openssh-server
/etc/pam.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd
/usr/lib/systemd/system/sshd-keygen.service
/usr/lib/systemd/system/sshd.service
/usr/lib/systemd/system/sshd.socket
/usr/lib/systemd/system/sshd@.service
/usr/lib64/fipscheck/sshd.hmac
/usr/libexec/openssh/sftp-server
/usr/sbin/sshd
/usr/sbin/sshd-keygen
/usr/share/man/man5/moduli.5.gz
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sftp-server.8.gz
/usr/share/man/man8/sshd.8.gz
/var/empty/sshd

2.修改sshd默认端⼝号

改sshd服务默认端⼝,为9527然后重启查看状态

修改配置⽂件中的Port 配置参数

#配置文件路径
vim /etc/ssh/sshd_config

修改为:
······
#Port 9527
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
······

#重启sshd服务
[root@centos7 ~]# systemctl restart sshd

修改监听的ip地址,默认监听本机的所有ip地址,通过修改配置⽂件指定ssh监听在指定的ip地址

给node1主机在添加⼀个ip地址172.20.1.111

[root@centos6 data]# ip a a 172.20.3.111/16 dev eth0
[root@centos6 data]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:1c:c0:2e brd ff:ff:ff:ff:ff:ff
    inet 172.20.3.6/16 brd 172.20.255.255 scope global eth0
    inet 172.20.3.111/16 scope global secondary eth0
    inet6 fe80::20c:29ff:fe1c:c02e/64 scope link 
       valid_lft forever preferred_lft forever

修改ssh的配置⽂件,指定监听的ip地址为172.20.1.111(此处配置的是node1)

[root@centos6 ~]# vim /etc/ssh/sshd_config 

修改为:
······
#Port 22
#AddressFamily any
ListenAddress 172.20.3.111
#ListenAddress ::
······

#重启sshd服务
[root@centos6 ~]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

#查看端口
[root@centos6 ~]# ss -tnl
State       Recv-Q Send-Q   Local Address:Port     Peer Address:Port 
LISTEN      0      128                 :::41310              :::*     
LISTEN      0      128                  *:53324               *:*     
LISTEN      0      128                 :::111                :::*     
LISTEN      0      128                  *:111                 *:*     
LISTEN      0      128       172.20.3.111:22                  *:*     
LISTEN      0      128          127.0.0.1:631                 *:*     
LISTEN      0      128                ::1:631                :::*     
LISTEN      0      100                ::1:25                 :::*     
LISTEN      0      100          127.0.0.1:25                  *:*     

然后使⽤node2链接172.20.3.6 链接失败,链接172.20.3.111成功。

[root@centos7 ~]# ssh 172.20.3.6
ssh: connect to host 172.20.3.6 port 22: Connection refused
[root@centos7 ~]# ssh 172.20.3.111
root@172.20.3.111's password: 
Last login: Thu Nov  7 17:51:31 2019 from 172.20.1.11
[root@centos6 ~]# 

修改ssh服务监听的⽹络地址类型,默认设any,inet 为ipv4,inet6为ipv6

[root@centos6 ~]# vim /etc/ssh/sshd_config

修改为:
······
#Port 22
#AddressFamily inet
ListenAddress 172.20.3.111
#ListenAddress ::
······
[root@centos6 ~]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[root@centos6 ~]# ss -tnl
State       Recv-Q Send-Q   Local Address:Port     Peer Address:Port 
LISTEN      0      128                 :::41310              :::*     
LISTEN      0      128                  *:53324               *:*     
LISTEN      0      128                 :::111                :::*     
LISTEN      0      128                  *:111                 *:*     
LISTEN      0      128       172.20.3.111:22                  *:*     
LISTEN      0      128          127.0.0.1:631                 *:*     
LISTEN      0      128                ::1:631                :::*     
LISTEN      0      100                ::1:25                 :::*     
LISTEN      0      100          127.0.0.1:25                  *:*  

sshd服务默认需要记录⽇志,默认的⽇志级别为INFO,⽇志存放于/var/log/secure中

[root@centos6 ~]# vim /etc/ssh/sshd_config 

修改为:
······
SyslogFacility AUTHPRIV
LogLevel INFO
······

[root@centos6 ~]# ll /var/log/secure
-rw------- 1 root root 18489 Nov  7 18:12 /var/log/secure

关于验证⽅⾯的配置

LoginGraceTime 指的是登录输入密码的最长时间,2m内容不输入密码,则结束本次链接,0代表不做限制。
PermitRootLogin 指的是是否允许root远程登录。yes代表允许, no代表不允许。
StrictModes 指的是在接受登录之前 是否检查文件权限和所有者信息和家目录信息,默认要检查。
MaxAuthTries 每次登录输入密码错误次数,默认是6次。
[root@centos6 ~]# vim /etc/ssh/sshd_config

修改为:
······
LoginGraceTime 2m
PermitRootLogin yes
StrictModes yes
MaxAuthTries 6
MaxSessions 10
······

登录验证⽅式

[root@centos6 ~]# vim /etc/ssh/sshd_config

修改为:
······
PubkeyAuthentication yes
PasswordAuthentication yes
······

是否⽀持x11选项,⾮活动时间限制选项

X11Forwarding 指定是否支持X11转发;
ClientAlivedInterval 非活动时间限制,0为不限制;
ClientAlivedCountMax 指定满足非活动时间次数,因此ClientAlivedInterval乘以ClientAlivedCountMax
的值为最长终非活动时间;
UseDNS 是否需要域名反解析,建议设置为no 这样可以提高链接速度。
[root@centos6 ~]# vim /etc/ssh/sshd_config

修改为:
······
X11Forwarding yes
ClientAlivedInterval 0
ClientAlivedCountMax 3
······

设置ssh登录成功后的提⽰信息

设置banner,后⾯跟上提⽰的⽂本信息

vim /etc/ssh/sshd_config

修改为:
······
Banner /root/sshbanner.txt
······

重启sshd服务,重新连接172.20.3.111可看到登录信息

[root@centos7 ~]# ssh 172.20.3.111
Hello Nanjing_Bokebi!!!
root@172.20.3.111's password: 
Last login: Thu Nov  7 19:30:33 2019 from 172.20.3.7
[root@centos6 ~]#

设置访问控制⽤户

AllowUsers 允许登录的用户;
DenyUsers 不允许登录的用户;
AllowGroups 允许登录的组;
DenyGroups 不允许登录的组

在node1上创建3个⽤户,并配置对应的密码:

[root@centos6 ~]# useradd gordon
[root@centos6 ~]# passwd gordon
Changing password for user gordon.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@centos6 ~]# useradd tom
[root@centos6 ~]# passwd tom
Changing password for user tom.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@centos6 ~]# useradd alice
[root@centos6 ~]# passwd alice
Changing password for user alice.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

sshd配置⽂件中添加相关的访问控制选项

[root@centos6 ~]# vim /etc/ssh/sshd_config

修改为
······
AllowUsers gordon tom
DenyUsers tom
AllowGroups alice gordon
DenyGroups alice
······
实际登录测试时,只有gordon可以登录成功,tom和alice再输⼊密码后被拒绝,要求再次输⼊密码;因此,有拒绝的⽤户
和组,肯定会被拒绝。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值