SSH概述
SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录、远程 复制等功能。SSH 协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令,SSH 为建立在应用层和传输层基础上的安全协议。对数据进行压缩,加快传输速度。
SSH原理
公钥传输原理
- 客户端发起链接请求
- 服务端返回自己的公钥,以及一个会话ID(这一步客户端得到服务端公钥)
- 客户端生成密钥对
- 客户端用自己的公钥异或会话ID,计算出一个值Res,并用服务端的公钥加密
- 客户端发送加密值到服务端,服务端用私钥解密,得到Res
- 服务端用解密后的值Res异或会话ID,计算出客户端的公钥(这一步服务端得到客户端公钥)
- 最终:双方各自持有三个秘钥,分别为自己的一对公、私钥,以及对方的公钥,之后的所有通讯都会被加密
OpenSSH
- 服务名称: sshd
- 服务端主程序: /usr/sbin/sshd
- 服务端配置文件: /etc/ssh/sshd_config
- 客户端配置文件:/etc/ssh/ssh_config
SSH登录方式
ssh [远程主机用户名]@[远程服务器主机名或IP地址] -p port
ssh 默认端口号 22 port (端口号)
ssh [选项] [远程主机用户名] [远程服务器主机名或IP地址] -p port
选项:
-l :指定登录名称。
-p:指定登录端口(当服务端的端口非默认时,需要使用-p 指定端口进行登录)
-t :方便跳板链接 # ssh -t 172.16.26.20 ssh -t 172.16.26.30 ssh 172.16.26.40
-o: 将配置文件放后面 # ssh 172.16.26.30 -o StrictHostKeyChecking=no
SSH用法
加命令
服务端配置
常用配置项:
[root@localhost named]# vim /etc/ssh/sshd_config
# 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
# OpenSSH is to specify options with their default value where
# default value.
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22 #生产环境中改掉
#AddressFamily any
#ListenAddress 0.0.0.0 #监听地址设置SSHD服务器绑定的IP 地址,0.0.0.0 表示侦听所有地址安全建议:如果主机不需要从公网ssh访问,可以把监听地址改为内网地址 这个值可以写成本地IP地址,也可以写成所有地址,即0.0.0.0 表示所有IP
#ListenAddress ::
HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
# Authentication:
#LoginGraceTime 2m #如果用户登录失败,在切断连接前服务器需要等待的时间,单位为秒
#PermitRootLogin yes #默认 ubuntu不允许root远程ssh登录
#StrictModes yes #检查.ssh/文件的所有者,权限等
#MaxAuthTries 6 #最大失败尝试登陆次数为6
#MaxSessions 10 #同一个连接最大会话
#PubkeyAuthentication yes #基于key验证 不需要输密码 可以直接登录
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
#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
PasswordAuthentication yes ##基于用户名和密码连接
ChallengeResponseAuthentication no
#KerberosTicketCleanup yes
# GSSAPI options
GSSAPIAuthentication yes #提高速度可改为no
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# the setting of "PermitRootLogin without-password".
# and ChallengeResponseAuthentication to 'no'.
# problems.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0 #秒
#ClientAliveCountMax 3 #3次
#ShowPatchLevel no
#UseDNS no #禁用反向解析 提高速度可改为no 内网改为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
PermitEmptyPasswords no
#密码验证当然是需要的!所以这里写 yes,也可以设置为 no,在真实的生产服务器上,根据不同安全级别要求,有的是设置不需要密码登陆的,通过认证的秘钥来登陆。MaxStartups #未认证连接最大值,默认值10
Banner /path/file
#以下可以限制可登录用户的办法:白名单 黑名单
AllowUsers user1 user2 user3@ip(限制主机)
DenyUsers user1 user2 user3
AllowGroups g1 g2
DenyGroups g1 g2
修改默认端口
将默认端口22改为7025
禁止root登录
密码输错限制
白名单 黑名单列表
使用秘钥对及免交互验证登录
原理
- 客户端发起ssh请求,服务器会把自己的公钥发送给用户
- 用户会根据服务器发来的公钥对密码进行加密
- 加密后的信息回传给服务器,服务器用自己的私钥解密,如果密码正确,则用户登录成功