这段时间在学习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 -y install openssh openssh-clients openssh-server
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
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 222
[root@witness14 ssh]
[root@witness14 ssh]
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
[root@witness14 ssh]
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
可见sshd服务的日志位置在`/var/log/secure`
- 参数
LogLevel INFO
登陆记录的等级为 INFO 级别及以上
4.2 安全调优重要参数
4.3 sshd防暴力破解
- 密码足够复杂
- 修改默认端口号
- 不允许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
+--[ 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
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
root@192.168.0.64
Now try logging into the machine, with "ssh '192.168.3.13'", and check in:
.ssh/authorized_keys
to make sure we haven
# 此时已经能够直接登陆13服务器了
通过第三方软件开源
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
bantime = 600
findtime = 600
maxretry = 3
backend = auto
[ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
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
bantime = 3600
findtime = 300
maxretry = 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
[ssh-iptables]
...
action = iptables[name=SSH, port=222, protocol=tcp]
...
port = 222
4.4 自定义脚本防止暴力破解
- centos6网络yum源安装方法:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
↩
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)