Linux基础服务sshd简介

2024-04-10 01:08
文章标签 基础 linux 服务 简介 sshd

本文主要是介绍Linux基础服务sshd简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

sshd由OpenSSH来提供

  • SSH 协议:Secure Shell,安全的shell协议。
  • SSH 为建立在应用层和传输层基础上的安全协议。
  • sshd服务使用SSH协议可以用来进行远程控制, 或在计算机之间传送文件。
  • sshd使用加密传输,较之使用明文传输的telnet传输文件要安全很多。

sshd配置文件

/etc/ssh/sshd_config

  • 如果井号开头的和后面参数没有空格的,表示默认值,是生效的
  • 如果井号开头和后面有空格的,表示纯注释

调优参数

端口

  • 默认端口22,外网生产环境需要修改
    Port 22
  • 使用参数指定端口连接非22默认端口[-p port]
    ssh -p 1234 root@10.0.0.88

监听地址

  • 默认缺省值为所有网卡的所有地址
  • 可以修改为指定IP
#ListenAddress 0.0.0.0
#ListenAddress ::

登陆等待时间

  • 从输入用户名到敲入密码的登陆等待时间
  • 默认是2分钟,可以调的小一些
    #LoginGraceTime 2m

允许root登录

  • 生产环境应该禁止root直接登录
  • Debian系列例如Ubuntu是默认禁止root登陆的
    PermitRootLogin yes

默认认证公钥文件

AuthorizedKeysFile .ssh/authorized_keys

是否允许使用密码认证登录

  • 做过密钥认证以后可关闭密码认证登录,防止暴力破解
    PasswordAuthentication yes

打印登陆提示信息和最后登录日志

  • 发现被黑线索
    PrintMotd no
    #PrintLastLog yes
[14:56:49 root@C8-88[ ~]#ssh 10.0.0.189
Last login: Sat Jul  3 22:00:12 2021 from 10.0.0.88

使用DNS反向解析

  • 如果敲完密码一直卡着,半天才进系统,可以将此项改为no
    #UseDNS no

修改登录提示

  • 创建或修改motd文件
    /etc/motd
  • 在文件中添加需要登录显示的内容
  • 修改配置文件打开PrintMotd
    sed -ri.bak 's/(PrintMotd )no/\1yes/' /etc/ssh/sshd_config

fail2ban防止暴力破解

  • 监控日志系统,匹配日志信息,将过分的ip加入到ipatble中
  • python写的用python装py包
  • 主配置文件jail.conf
  • 模板服务文件在源码包的files中,fail2ban.service,redhat-initd
  • 查找古时候的启动文件,文件内容带有chkconfig相关字样
    grep chkconfig ./* -R --color
  • 老版本使用chkconfig添加启动项
    chekconfig --add fail2ban
  • 登录日志

相关主要文件说明:

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

应用案例

  • 远程ssh用户5分钟三次失败则禁用1小时
  • 防止频繁试密码
  • 修改[ssh-iptables]
  • 先启用功能
    enabled = ture
  • 指定日志
    logpath = /var/log/secure
  • 发一批邮件改sentmail,系统中需要已启用sentmail
  • 如果ssh服务器不是22端口,则需要改配置文件,
  • iptables配置文件也要改

查看ban的状态

fail2ban-client status ssh-iptable

使用shell脚本实现fail2ban功能

  • 利用定时任务查看安全日志
  • 将超过阈值的IP加入系统黑名单hosts.deny中去
#!/bin/bash
cat var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2"="$1;}' > /root/satools/black.txt
DEFINE='10'
for i in `cat /root/satools/black.txt`;doIP=`echo $i | awk -F= '{print $1}'`NUM=`echo $i | awk -F= '{print $2}'`if [ $NUM -gt $DEFINE ];thengrep $IP /etc/hosts.deny > /dev/nullif [ $? -gt 0 ];thenecho "sshd:$IP" >> /etc/hosts/denyfifi
done

利用denyhosts实现

  • epel中的denyhosts简单实现

系统再带pam模块提供防护功能

  • 系统本身体统pam模块功能
    /etc/pam.d/sshd
  • 添加错误尝试次数,和锁定时间
  • 在第一行下面添加一条规则
    auth required pam_tally2.so deny=3 unlock_time=600 even_deny_root root_unlock_time=1200

pam模块命令

  • 查看用户登录次数
    手动解除锁定:
    查看某一用户错误登陆次数:
    pam_tally –-user
    例如,查看work用户的错误登陆次数:
    pam_tally –-user work
    清空某一用户错误登陆次数:
    pam_tally –-user –-reset
    例如,清空 work 用户的错误登陆次数,
    pam_tally –-user work –-reset

xshell等客户端免密登录服务器

  • 使用xshell等客户端本地生成公钥文件
  • 将生成的公钥文件放到服务器对应的用户家目录的.ssh目录中去

==========================================================
配置文件内容:

#	$OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key# Ciphers and keying
#RekeyLimit default none# System-wide Crypto policy:
# This system is following system-wide crypto policy. The changes to
# Ciphers, MACs, KexAlgoritms and GSSAPIKexAlgorithsm will not have any
# effect here. They will be overridden by command-line options passed on
# the server start up.
# To opt out, uncomment a line with redefinition of  CRYPTO_POLICY=
# variable in  /etc/sysconfig/sshd  to overwrite the policy.
# For more information, see manual page for update-crypto-policies(8).# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO# Authentication:#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10#PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile	.ssh/authorized_keys#AuthorizedPrincipalsFile none#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Fedora and may cause several
# problems.
UsePAM yes#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes# It is recommended to use pam_motd in /etc/pam.d/sshd instead of PrintMotd,
# as it is more configurable and versatile than the built-in version.
PrintMotd no#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none# no default banner path
#Banner none# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS# override default of no subsystems
Subsystem	sftp	/usr/libexec/openssh/sftp-server# Example of overriding settings on a per-user basis
#Match User anoncvs
#	X11Forwarding no
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server

这篇关于Linux基础服务sshd简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/889703

相关文章

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

关于DNS域名解析服务

《关于DNS域名解析服务》:本文主要介绍关于DNS域名解析服务,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录DNS系统的作用及类型DNS使用的协议及端口号DNS系统的分布式数据结构DNS的分布式互联网解析库域名体系结构两种查询方式DNS服务器类型统计构建DNS域