CentOS7下SSH服务学习笔记
测试环境:
[root@nmserver-7 ~]# uname -a
Linux nmserver-7.test.com 3.10.0-514.el7.centos.plus.i686 #1 SMP Wed Jan 25 12:55:04 UTC 2017 i686 i686 i386 GNU/Linux
[root@nmserver-7 ~]# cat /etc/redhat-release
CentOS release 7.3.1611 (AltArch)
[root@nmserver-7 ~]#
1、SSH服务安装
linux系统安装后默认已经开启ssh服务,并且包含ssh所有需要的软件了,但是还是有必要了解下已经安装的ssh是那个包提供的,
[root@nmserver-7 ~]# whereis sshd
sshd: /usr/sbin/sshd /usr/share/man/man8/sshd.8.gz
[root@nmserver-7 ~]# rpm -qf /usr/sbin/sshd
openssh-server-6.6.1p1-31.el7.i686
[root@nmserver-7 ~]#
也就是说假如没有ssh服务的话我们可以通过yum安装openssh-server来重新启动ssh服务
那么openssh-server都在系统中安装了什么文件?
[root@nmserver-7 ~]# rpm -ql openssh-server
/etc/pam.d/sshd
/etc/ssh/sshd_config #配置文件位置
/etc/sysconfig/sshd
/usr/lib/fipscheck/sshd.hmac
/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/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、SSH服务启动
CentOS7.3下SSH服务的启动
[root@nmserver-7 ~]# systemctl stop sshd #停止
[root@nmserver-7 ~]# systemctl start sshd #开始
[root@nmserver-7 ~]# systemctl restart sshd #重启
[root@nmserver-7 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2017-08-24 16:32:52 CST; 1s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 2863 ExecStart=/usr/sbin/sshd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2864 (sshd)
CGroup: /system.slice/sshd.service
└─2864 /usr/sbin/sshd
8月 24 16:32:51 nmserver-7.test.com systemd[1]: Starting OpenSSH server d...
8月 24 16:32:52 nmserver-7.test.com systemd[1]: PID file /var/run/sshd.pi...
8月 24 16:32:52 nmserver-7.test.com sshd[2864]: Server listening on 0.0.0...
8月 24 16:32:52 nmserver-7.test.com sshd[2864]: Server listening on :: po...
8月 24 16:32:52 nmserver-7.test.com systemd[1]: Started OpenSSH server da...
Hint: Some lines were ellipsized, use -l to show in full.
查看ssd服务端口是否正常,
[root@nms ~]# netstat -tlnp |grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1207/sshd
tcp 0 0 :::22 :::* LISTEN 1207/sshd
3、SSH原理(SSH服务器端与客户端的连接步骤)

step1:服务器建立公钥文件
每一次启动sshd服务时,该服务会主动去找/etc/ssh/ssh_host*文件,若系统刚安装完成,会没有公钥文件,
sshd会主动计算出这些需要的公钥文件,同时也会计算出服务器自己需要的私钥文件。
step2:客户端主动连接要求
若客户端想要连接到ssh服务器,则需要使用适当的客户端程序来连接,linux系统默认已经安装openssh-clients,包含SSH客户端;
windows下常用的客户端连接工具有xShell、SecureCart、Putty等
step3:服务器传送公钥文件给客户端
接收到客户端的要求后,服务器便将第一个步骤取的公钥文件以明码的形式传送给客户端;
step4:客户端记录/比对服务器的公钥数据及随机计算自己的公私钥;
客户端将接收到的公钥数据记录到客户端的用户主目录内的 ~/.ssh/known_hosts文件里,之后则开始计算客户端自己的公私钥数据;
step5:返回客户端的公私钥数据到服务器端
用户将自己的公钥传送给服务器,此时服务器具有服务器的私钥和客户端的公钥,而客户端具有服务器的公钥及客户端自己的私钥,你会看到,
在此次连接的服务器与客户端的密钥系统(公钥+私钥)并不一样,因此,才称为非对称密钥系统;
step6:服务器接收客户端的公钥开始双向加解密
(1)服务器到客户端:服务器传送数据时,使用客户端的公钥加密后进行发送,客户端接收后,用自己的私钥解密;
(2)客户端到服务器:客户端传送数据时,使用服务器的公钥进行加密数据然后发送给服务器,服务器接收后用服务器的私钥解密数据;
4、SSHD服务器配置
SSHD服务器的详细设置都放在/etc/ssh/sshd_config配置文件中,在配置文件中,只要是未被注释的设置值即为“默认值”
[root@nmserver-7 ssh]# cat /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm 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
# 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
#ssh默认使用22端口,也可以使用多个Port,例如想要开放sshd在22和2200,则多加一行内容为:Port 2200 然后重启SSHD服务即可;
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#监听的主机网卡,默认值是监听所有接口的SSH要求
# The default requires explicit activation of protocol 1
#Protocol 2
#选择SSH协议版本,默认支持V2 如要支持旧版本V1,则改为“Protocol 2,1”
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key #ssh 版本使用的私钥
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key #ssh 版本2使用的RSA私钥
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key #ssh 版本2使用的ECDSA私钥
HostKey /etc/ssh/ssh_host_ed25519_key
#说明主机Private Key 放置的位置,默认使用上面文件
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024 #设置生产密钥的位数
# Ciphers and keying
#RekeyLimit default none
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH #当有人使用SSH登录系统的时候,SSH会记录信息;
SyslogFacility AUTHPRIV
#LogLevel INFO #设置日志等级
# Authentication: # 以下是安全设置项目
#LoginGraceTime 2m
#PermitRootLogin yes #是否允许root登录。默认是允许的,建议设置成no
#StrictModes yes #是否让SSDH去检查用户主目录或相关文件的权限数据;
#MaxAuthTries 6
#MaxSessions 10
#RSAAuthentication yes
#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 #自定义的公钥数据放置于用户主目录下 .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
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and 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 Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation sandbox # Default for new installations.
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#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
4、SSH简易安全设置
4.1 服务器软件本身的设置强化
SSHD服务器配置文件/etc/ssh/sshd_config默认项目已经很完备了,出于安全考虑需要设置:禁止root这个账号使用sshd服务;
[root@db2017 ~]# vi /etc/ssh/sshd_config
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin yes #去掉注释且修改成PermitRootLogin no
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
4.2、TCP Wrapper使用:/etc/hosts.allow /etc/hosts.deny
假如只想让本机以及局域网192.168.1.0/24、192.168.0.0/24及192.168.100.0/24网段内的主机能够登录的话,可以这样设置:
[root@db2017 ~]# vi /etc/hosts.allow
#
# hosts.allow This file contains access rules which are used to
# allow or deny connections to network services that
# either use the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
sshd:127.0.0.1 192.168.0.0/255.255.255.0 192.168.1.0/255.255.255.0 192.168.100.0/255.255.255.0 #新添加的SSHD规则!
[root@db2017 ~]# vi /etc/hosts.deny
# the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# The rules in this file can also be set up in
# /etc/hosts.allow with a 'deny' option instead.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
sshd:ALL
4.3、iptables数据包过滤防火墙
[root@db2017 sysconfig]# vi /etc/sysconfig/iptables
增加规则:
iptables -A INPUT -i EXTIF -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -i EXTIF -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
CentOS7下SSH服务学习笔记的更多相关文章
- CentOS7环境下SSH端口修改笔记
CentOS7环境下SSH端口修改笔记 说明: CentOS7服务器环境,默认SSH端口为22,考虑到安全方面问题,欲修改端口为62231(机器内网IP为192.168.1.31) ssh配置文件和i ...
- SpringCloud微服务学习笔记
SpringCloud微服务学习笔记 项目地址: https://github.com/taoweidong/Micro-service-learning 单体架构(Monolithic架构) Mon ...
- 微服务学习笔记(1)——使用MagicOnion实现gRPC
原文:微服务学习笔记(1)--使用MagicOnion实现gRPC 1.什么是gRPC 官方文档:https://grpc.io/docs/guides/index.html 2.什么是MagicOn ...
- 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现
原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...
- Spring Cloud微服务学习笔记
Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...
- CentOS7下RabbitMQ服务安装配置
参考文档: CentOS7下RabbitMQ服务安装配置 http://www.linuxidc.com/Linux/2016-03/129557.htm 在linux下安装配置rabbitMQ详细教 ...
- CentOS7下NFS服务安装及配置固定端口
CentOS7下NFS服务安装及配置 系统环境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64 软件版本:nfs-utils-1. ...
- CENTOS7开启SSH服务
CENTOS7开启SSH服务 CENTOS7开启SSH服务以后,XSHELL,WINSCP才能成功连接CENTOS7. 开启ssh服务需要root权限,先用root账户登陆. 安装SSH: yum i ...
- Ubuntu环境下SSH服务安装、SSH远程登录以及SSH数据传输
https://www.cnblogs.com/asyang1/p/9467646.html SSH 为 Secure Shell 的缩写,为建立在应用层基础上的安全通信协议. 一.检查SSH服务是否 ...
随机推荐
- VisualSVN的安装使用
1.什么是VisualSVN VisualSVN Server是集成了Subversion和Apache的一种版本管理工具,它简化了手工配置Subversion的繁琐步骤,安装的时候SVN Serve ...
- AngularJs ng-repeat解决循环对象出现重复项报错的问题
问题:ng-repeat 的循环对象是不能出现重复项的,所以如果有重复的就会报错,应该是 key value的问题吧,不是很了解内部运行机制:经过查询发现 在 循环后面加上 track by $i ...
- C语言实践
初学者往往有这样的困惑: 教程也阅读了,知识点也理解了,但是真正编写代码起来无从下手. 连一些基本的小程序都不能完成. 究其原因,就是缺少实践,没有培养起编程思维. 没有处理相关问题的经验. 编程能力 ...
- PreparedStatement是如何防止SQL注入的?
为什么在Java中PreparedStatement能够有效防止SQL注入?这可能是每个Java程序员思考过的问题. 首先我们来看下直观的现象(注:需要提前打开mysql的SQL文日志) 1. 不使用 ...
- Html5的datetime-local控件
参考http://stackoverflow.com/questions/7815232/html5-input-type-datetime-vs-datetime-local-which-shoul ...
- TCP通讯模型简单示例
1. TCP通讯模型 2. 服务器端 ① 创建socket,用函数socket() ② 绑定IP地址.端口号等信息到socket上,用函数bind() ③ 设置允许的最大连接数,用函数listen() ...
- Python 初识爬虫-**机场出港业务
# -*- coding:utf-8 -*- from lxml import etree import requests ##先进单页测试,然后在进行多页循环 没有解决的问题,动态解决最大页数,目前 ...
- grep常用选项记录
grep: 一.常用选项: -i 不区分大小写针对单个字符 -v 显示不包括查找字符的所有行 -o 只打印出匹配到的字符 -c 显示有多少行被匹配到 -e 可以使用多个表 ...
- 21-----BBS论坛
BBS论坛(二十一) 21.1.编辑轮播图功能完成 (1)cms_banners.html 把属性绑定到<tr>上面,方便找到各属性的值 <tbody> {% for bann ...
- 关于WampServer一些配置修改
1.解决WAMP mysql中文乱码问题(在mysql的my.ini文件中) 1).找到client字段并添加:default-character-set=utf8 2).找到mysql字段并添加: ...