linux基本服务之sshd
来源:互联网 发布:淘宝网登录首页官网 编辑:程序博客网 时间:2024/06/05 04:03
这段时间在学习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_64openssh-clients-5.3p1-114.el6_7.x86_64openssh-5.3p1-114.el6_7.x86_64openssh-server-5.3p1-114.el6_7.x86_64
sshd服务的主要配置文件为/etc/ssh/ssh_config
(客户端配置)和/etc/ssh/sshd_config
(服务端配置)
2.3 设置开机启动
chkconfig sshd onchkconfig --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.14ssh root@192.168.3.14# 创建witness用户并添加密码123456useradd witness&&echo 123456 | passwd --stdin witnessssh 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 sshdtcp 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-keygenGenerating 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@witness14The 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.13The 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)? yesWarning: 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_keysto 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/fail2banchkconfig --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)修改成 truefilter = sshd #过滤规则filter的名字,对应filter.d目录下的sshd.confaction = iptables[name=SSH, port=ssh, protocol=tcp] #动作的相关参数,对应action.d/iptables.conf文件sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, 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-iptablesStatus 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 自定义脚本防止暴力破解
- centos6网络yum源安装方法:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
↩
- linux基本服务之sshd
- Linux SSHD服务
- Linux服务-SSHD
- Linux下sshd服务
- Linux sshd 相关服务
- linux初学者-sshd服务
- Linux----sshd服务
- linux之vsftpd和sshd配置服务安装
- Linux sshd服务自动启动
- linux中 vsftpd,sshd 服务
- sshd服务基本配置和使用
- sshd服务基本配置和使用
- linux入门之sshd练习
- linux初学之sshd远程登陆
- linux如何开机启动SSHD服务
- Linux系统管理命令和sshd服务
- Linux下 sshd服务不能启动
- sshd服务
- Python3播放MP3(支持MAC,WIN,LINUX)
- C++:动态分配内存(malloc,free)
- 获取本机IP地址(Java版本)
- 双系统时ubuntu更改开机引导
- Java异常处理
- linux基本服务之sshd
- Python:把二叉树打印成多行
- Linux源码安装Gitblit
- win32控制台应用程序不显示窗口启动
- 51nod 1134 最长递增子序列
- stl-vector
- 总结Java基础第二章
- Linux源码安装JDK1.8
- struts2