SSH学习笔记

SSH的架构

SSH的架构为客户端-服务器架构,向服务器发出请求的部分,称为客户端(client),OpenSSH 的实现为 ssh;接收客户端发出的请求的部分,称为服务器(server),OpenSSH 的实现为 sshd。

ssh的登陆流程:

  • 客户端使用ssh-keygen命令生成公钥和私钥
  • 手动将公钥复制到服务器上
  • 客户端向服务器端发送请求
  • 服务器端收到客户端发送的请求之后,发送一些数据给客户端,要求客户端证明身份
  • 客户端收到数据之后使用私钥进行签名,并发送给服务器
  • 服务器端收到客户端发送的加密签名数据之后,使用公钥进行解密,然后对比原始数据,一致的话就允许客户端进行登录

生成SSH key

1、创建公钥,打开终端输入命令行

ssh-keygen -t rsa -C "xxx@xxx.com"
-b:指定秘钥的二进制位数。参数越大,秘钥越难破解,当然解密开销也会更大

-t:参数用于指定生成秘钥的算法

-C:指定密匙文件的注释

其中:“xxx@xxx.com”为你的邮箱。

在这里插入图片描述

打开公钥
cat ~/.ssh/id_rsa.pub

得到一段编码:

手动上传公钥到服务器

将公钥复制到服务器端的~/.ssh/authorized_keys文件中

自动上传公钥

 ssh-copy-id -i id_rsa user@host

配置之后如果在登录过程中显示失败,记得配置ssh_config。

sshd_config详解

#	$OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 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/bin:/bin:/usr/sbin:/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.

#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

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile	.ssh/authorized_keys .ssh/authorized_keys2

#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!

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# If you just want the PAM account and session checks to run without

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem sftp	/usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#	X11Forwarding no
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server

遇到的问题

连接服务器时,提示如下错误信息:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:1vXv9I7BzalAj6JfMfkIQZ0fLD+0gw7x1UoTVQ9D7gE.
Please contact your system administrator.
Add correct host key in /Users/jiaqi.zhang/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/jiaqi.zhang/.ssh/known_hosts:9
ECDSA host key for xxx.xxx.xxx.xx has changed and you have requested strict checking.

解决办法:

将本地客户端的known_hosts中连接目标服务器相关信息都删掉即可。vi ~/.ssh/known_hosts

ssh-keygen -R hostname
参考文章:

SSH教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值