linux基本服务之sshd

这段时间在学习linux常用服务,这里将学习内容以及自己的实验心得记录下来,在自己忘记的时候也好复习


实验环境: centos 6.7 64bit


1. 简介

SSHD服务

  • 介绍:SSH协议:安全外壳协议。为 Secure Shell 的缩写。SSH 为建立在应用层和传输层基础上的安全协议。
  • 作用:sshd服务使用SSH协议可以用来进行远程控制, 或在计算机之间传送文件
    相比较之前用telnet方式来传输文件要安全很多,因为telnet使用明文传输,是加密传输

2.安装服务

安装服务需要安装 Openssh四个安装包,分别为

  • openssh-5.3p1-114.el6_7.x86_64:包含OpenSSH服务器及客户端需要的核心文件
  • openssh-clients-5.3p1-114.el6_7.x86_64:OpenSSH客户端软件包
  • openssh-server-5.3p1-114.el6_7.x86_64:OpenSSH服务器软件包
  • openssh-askpass-5.3p1-114.el6_7.x86_64:支持对话框窗口的显示,是一个基于X 系统的密码诊断工具
2.1 安装方法
  • 如果配置好了yum源1,直接使用命令
 yum -y install openssh openssh-clients openssh-server
  • 直接从光盘文件安装rpm包文件
rpm -ivh /media/cdrom/Packages/openssh*.rpm

注意:这种方法比较不推荐,因为检查依赖过程中可能需要安装其他的包,比较麻烦,基本就是缺啥装啥。

2.2 检查
rpm -qa |grep openssh

确认上面提到的四个包已经安装

openssh-askpass-5.3p1-114.el6_7.x86_64
openssh-clients-5.3p1-114.el6_7.x86_64
openssh-5.3p1-114.el6_7.x86_64
openssh-server-5.3p1-114.el6_7.x86_64

sshd服务的主要配置文件为/etc/ssh/ssh_config(客户端配置)和/etc/ssh/sshd_config(服务端配置)

2.3 设置开机启动
chkconfig sshd on
chkconfig --list sshd

检查结果:

ssh 0:off 1:off 2:on 3:on 4:on 5:on 6:off

3. ssh使用

  • format
    ssh [username]@[remote_server_name or IP_address]
  • examples
ssh 192.168.3.14
ssh root@192.168.3.14
# 创建witness用户并添加密码123456
useradd witness&&echo 123456 | passwd --stdin witness
ssh witness@192.168.3.14
# 一种不常用的方法
ssh -l wintess 192.168.3.14

在首次登陆某个主机时系统没有保存远程主机的信息,为了确认该主机身份会提示用户是否继续连接,输入YES后密码登陆。
此时$HOME/.ssh/known_hosts文件中会存入该远程主机的信息,下次登陆就不会有提示。
root用户可直接cat /root/.ssh/known_hosts查看记录的信息。

RSA算法基于一个十分强大的数论事实:将两个大素数相乘十分容易,但是想要对其乘积进行因式分解却是极其困难,因此可以将乘积公开作为加密密钥。

4. sshd服务配置与管理

对应配置文件为/etc/ssh/sshd_config,
参数前面有#代表默认值和注释


4.1 基本参数
  • 参数Port 22
#默认22端口
#Port 22 
#要修改端口为222 加一行
Port 222
#然后重启sshd服务
# 查看监听端口
[root@witness14 ssh]# netstat -tlunp | grep sshd
# 结果
[root@witness14 ssh]# netstat -tlunp | grep sshd
tcp        0      0 0.0.0.0:222                  0.0.0.0:*                   LISTEN      3250/sshd           
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      3124/sshd           
tcp        0      0 127.0.0.1:6011              0.0.0.0:*                   LISTEN      3255/sshd           
tcp        0      0 :::222                       :::*                        LISTEN      3250/sshd           
tcp        0      0 ::1:6010                    :::*                        LISTEN      3124/sshd           
tcp        0      0 ::1:6011                    :::*                        LISTEN      3255/sshd

修改端口后登陆方法:ssh -p 222 witness@192.168.3.14


  • 参数Protocol 2

    选择的 SSH 协议版本,可以是 1 也可以是 2 ,CentOS 5.x 预设是仅支持 V2。
    安全考虑,设置为最新的协议版本。

  • 参数HostKey /etc/ssh/ssh_host_key

    设置包含计算机私人密匙的文件,删除后ssh无法登陆,重启服务会自动生成。

  • 参数SyslogFacility AUTHPRIV
    当用户使用ssh登入系统的时候,ssh会记录这个信息,类型为AUTHPRIV
[root@witness14 ssh]# cat /etc/rsyslog.conf |grep authpriv --color
*.info;mail.none;authpriv.none;cron.none                /var/log/messages
# The authpriv file has restricted access.
authpriv.*                                          /var/log/secure
可见sshd服务的日志位置在`/var/log/secure`
  • 参数LogLevel INFO
    登陆记录的等级为 INFO 级别及以上
4.2 安全调优重要参数
  • 参数LoginGraceTime 2m

    从连接上ssh server 到输入密码进入的时间,如果这段时间没成功登陆,就会断开,无单位默认为秒

  • 参数PermitRootLogin yes

    是否允许root登陆,默认为yes,但实际生产中是禁止root登陆的。

  • 参数PasswordAuthentication yes

    是否需要密码验证,实际生产中有时候设置为no,通过认证密钥登陆

  • 参数PermitEmptyPasswords no

    是否允许以空密码登陆,默认不行

  • 参数PrintMotd yes

    登入后是否显示一些信息,手动编辑/etc/motd实现,打印该文件内容

  • 参数PrintLastLog yes

    显示上次登陆的信息,默认也是 yes

  • 参数UseDNS yes

    通常来说,为了判断客户端来源是否合法,会通过DNS反查客户端主机名
    但如果在内网互连,这个选项设置为no能加快登陆速度

4.3 sshd防暴力破解


  • method1

  • 密码足够复杂
  • 修改默认端口号
  • 不允许root账号直接登陆,添加普通账号赋予一些权限
  • 不允许密码登陆,只能通过认证密钥来登陆系统

# 通过密钥认证实现ssh登陆
# 1.生成密钥,过程一路回车默认即可
[root@witness14 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/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:
f3:fc:52:0e:4d:74:17:4b:2a:f8:60:94:0b:de:19:07 root@witness14
The key's randomart image is:
+--[ RSA 2048]----+
|         Eo    o.|
|       ..o... + o|
|      . o+=o o o |
|       ..+o o    |
|        S  +     |
|         +. o    |
|          o+     |
|          ...    |
|           ..    |
+-----------------+
# 2.将公钥拷贝到服务器上

[root@witness14 ~]# ssh-copy-id -i root@192.168.3.13
The authenticity of host '192.168.3.13(192.168.3.13)' can't be established.
RSA key fingerprint is d9:17:d7:db:38:7c:e8:56:9c:4b:7e:00:7f:9e:1c:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.64' (RSA) to the list of known hosts.
root@192.168.0.64's password: 
Now try logging into the machine, with "ssh '192.168.3.13'", and check in:

  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting
# 此时已经能够直接登陆13服务器了


  • method2

通过第三方软件开源 fail2ban—-> 官网
#安装
yum -y install fail2ban

主要配置文件说明

  • /etc/fail2ban/action.d #动作文件夹,内含默认文件。iptables以及mail等动作配置
  • /etc/fail2ban/fail2ban.conf #定义了fai2ban日志级别、日志位置及sock文件位置
  • /etc/fail2ban/filter.d #条件文件夹,内含默认文件。过滤日志关键内容设置
  • /etc/fail2ban/jail.conf #主要配置文件,模块化。主要设置启用ban动作的服务及动作阀值

如果是源码安装,需要让其开机自启动则需要在源码目录执行

cp files/redhat-initd /etc/init.d/fail2ban
chkconfig --add fail2ban  #开机自动启动

应用实例:
设置条件:ssh远程登录5分钟内3次密码验证失败,禁止用户IP访问主机1小时,1小时该限制自动解除,用户可重新登录。
因为动作文件(action.d/iptables.conf)以及日志匹配条件文件(filter.d/sshd.conf )安装后是默认存在的。基本不用做任何修改。所有主要需要设置的就只有jail.conf文件。启用sshd服务的日志分析,指定动作阀值即可。
实例文件/etc/fail2ban/jail.conf及说明如下:

[DEFAULT]               #全局设置
ignoreip = 127.0.0.1/8       #忽略的IP列表,不受设置限制
bantime  = 600             #屏蔽时间,单位:秒
findtime  = 600             #这个时间段内超过规定次数会被ban掉
maxretry = 3                #最大尝试次数
backend = auto            #日志修改检测机制(gamin、polling和auto这三种)

[ssh-iptables]               #单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置。
enabled  = true             #是否激活此项(true/false)修改成 true
filter   = sshd              #过滤规则filter的名字,对应filter.d目录下的sshd.conf
action   = iptables[name=SSH, port=ssh, protocol=tcp]             #动作的相关参数,对应action.d/iptables.conf文件
sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.c
om, sendername="Fail2Ban"]#触发报警的收件人
logpath  = /var/log/secure   #检测的系统的登陆日志文件。这里要写sshd服务日志文件。 默认为logpath  = /var/log/sshd.log
#5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下
bantime  = 3600   #禁止用户IP访问主机1小时
findtime  = 300    #在5分钟内内出现规定次数就开始工作
maxretry = 3    #3次密码验证失败

执行iptables -L -n会多出一条规则链,故意输错几次也能看到被禁用的提示。
执行fail2ban-client status检测fail2ban是否正常工作

Status
|- Number of jail:  1
`- Jail list:       ssh-iptables

执行fail2ban-client status ssh-iptables可以看到禁用IP

fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
|  |- File list:    /var/log/secure 
|  |- Currently failed: 0
|  `- Total failed: 3
`- action
   |- Currently banned: 1
   |  `- IP list:   192.168.3.13 
   `- Total banned: 1

日志消息存放在/var/log/fail2ban.log

  • fail2ban实际上通过检查指定日志/var/log/secure 来确定禁用名单的,如果想立即解禁,清空/var/log/secure并重启fail2ban即可
  • 后期如果清空或者重启iptables,也需要重启fail2ban
  • 修改ssh端口后需要修改 /etc/fail2ban/jail.conf/etc/fail2ban/action.d/iptables.conf
# 例如端口修改为222
# jail.conf
[ssh-iptables] 
...              
action   = iptables[name=SSH, port=222, protocol=tcp]
...

# iptables.conf

#port = ssh
#改为
port = 222  

#都修改完之后重启服务即可           
4.4 自定义脚本防止暴力破解

输错密码累计十次永久拉黑


  1. centos6网络yum源安装方法:wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值