ssh的加密方式
互联网数据传输,没有进过加密是很危险的,使用数据加密方式进行保护是非常有必要的。
目前加密的方式有两种:对称加密与非对称加密。
对称加密(秘钥加密):客户端与服务器使用同一套秘钥对数据进行加密解密。对称加密的加密强度高,不容易破解。
非对称加密(公钥加密):非对称加密有一个公钥和一个私钥,SSH就是使用非对称加密。可以通过公钥或者私钥来加密或者解密,因为在验证身份的时候,ssh只支持客户端保存私钥,服务器端保存公钥,所以一般情况是:客户端生成密钥对,将公钥分发给服务端;服务端生成密钥对,将私钥分发给客户端。
ssh相关文件
在服务端即主机B上:
/etc/ssh/sshd_config :ssh服务程序sshd的配置文件。
/etc/ssh/ssh_host_* :服务程序sshd启动时生成的服务端公钥和私钥文件。如ssh_host_rsa_key和ssh_host_rsa_key.pub。.pub结尾文件是主机验证时的host key,将写入到客户端的~/.ssh/known_hosts文件中。私钥文件严格要求权限为600,若不是则sshd服务可能会拒绝启动。

~/.ssh/authorized_keys:保存的是基于公钥认证机制时来自于客户端的公钥。在基于公钥认证机制认证时,服务端将读取该文件。
在客户端即主机A上:
/etc/ssh/ssh_config :客户端的全局配置文件。
~/.ssh/config :客户端的用户配置文件,生效优先级高于全局配置文件。一般该文件默认不存在。该文件对权限有严格要求只对所有者有读/写权限,对其他人完全拒绝写权限。
~/.ssh/known_hosts :保存主机验证时服务端主机host key的文件。文件内容来源于服务端的ssh_host_rsa_key.pub文件。
/etc/ssh/known_hosts:全局host key保存文件。作用等同于~/.ssh/known_hosts。
~/.ssh/id_rsa :客户端生成的私钥。由ssh-keygen生成。该文件严格要求权限,当其他用户对此文件有可读权限时,ssh将直接忽略该文件。
~/.ssh/id_rsa.pub :私钥id_rsa的配对公钥。对权限不敏感。当采用公钥认证机制时,该文件内容需要复制到服务端的~/.ssh/authorized_keys文件中。
~/.ssh/rc :保存的是命令列表,这些命令在ssh连接到远程主机成功时将第一时间执行,执行完这些命令之后才开始登陆或执行ssh命令行中的命令。
/etc/ssh/rc :作用等同于~/.ssh/rc。
/etc/ssh/sshd_config配置简介
#Port 22 #默认端口22,可以修改为其他没有使用的端口
#AddressFamily any #IPV4和IPV6协议家族用哪个,any表示二者均有,没有修改过这个参数
#ListenAddress 0.0.0.0 #监听的ip,0.0.0.0表示本机所有ip
#ListenAddress :: #指明监听的IPV6的所有地址格式 (还没有机会使用ipv6)
HostKey /etc/ssh/ssh_host_rsa_key #rsa私钥认证(默认)
#HostKey /etc/ssh/ssh_host_dsa_key #dsa私钥认证
HostKey /etc/ssh/ssh_host_ecdsa_key #ecdsa私钥认证
HostKey /etc/ssh/ssh_host_ed25519_key #ed25519私钥认证
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV #SSH登录日志,信息保存在/var/log/secure里面
#LogLevel INFO #日志等级
# Authentication:
#LoginGraceTime 2m #登录容忍时间,2分钟没有输入密码就自动断开连接
#PermitRootLogin yes #是否允许root登录
#StrictModes yes #是否让sshd去检查用户主目录或相关文件的权限数据
#MaxAuthTries 6 #最大认证尝试次数,最多可以尝试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 #服务器生成一对公私钥之后,会将公钥放到.ssh/authorizd_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,这里最好设置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 #是否启用基于GSSAPI的身份认证机制
GSSAPICleanupCredentials no #是否在用户退出登录后自动销毁用户凭证缓存。默认值是"yes".仅用于SSH-2.
#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 #使用PAM认证,ssh服务端会加载/etc/pam.d/sshd
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no #设置为yes表示sshd允许被远程主机所设置的本地转发端口绑定在非环回地址上
X11Forwarding yes #是否允许x11转发,可以让窗口的数据通过SSH连接来传递(请查看ssh -X 参数):#ssh -X user@IP
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no #默认值是"no",如果指定UseLogin为yes,我们将在登录的过程中使用ssh服务端的login程序进行登录验证,如果将login程序移走或删除,我们如果使用UseLogin yes,此时将无法登录ssh服务端.
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes #指定是否将客户端主机名解析为IP,以检查此主机名是否与其IP地址真实对应,默认yes,影响的是主机验证阶段。建议在未配置DNS解析时,将其设置为no,否则主机验证阶段会很慢
#PidFile /var/run/sshd.pid #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 #外部子系统,如关闭该选项,将无法使用sftp
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
/etc/ssh/ssh_config配置简介
# Host * # Host指令是ssh_config中最重要的指令,只有ssh连接的目标主机名能匹配此处给定模式时,下面一系列配置项直到出现下一个Host指令才对此次连接生效
# ForwardAgent no
# ForwardX11 no
# RhostsRSAAuthentication no
# RSAAuthentication yes
# PasswordAuthentication yes # 是否启用基于密码的身份认证机制
# HostbasedAuthentication no # 是否启用基于主机的身份认证机制
# GSSAPIAuthentication no # 是否启用基于GSSAPI的身份认证机制
# GSSAPIDelegateCredentials no
# GSSAPIKeyExchange no
# GSSAPITrustDNS no
# BatchMode no # 如果设置为"yes",将禁止passphrase/password询问。比较适用于在那些不需要询问提供密码的脚本或批处理任务任务中。默认为"no"。
# CheckHostIP yes
# AddressFamily any
# ConnectTimeout 0
# StrictHostKeyChecking ask # 设置为"yes",这样直接拒绝加入对方的公钥,需要手工来填加,ssh将不自动添加host key到~/.ssh/known_hosts文件,且拒绝连接那些未知的主机(即未保存host key的主机或host key已改变的主机)。它将强制用户手动添加host key到~/.ssh/known_hosts中。设置为ask将询问是否保存到~/.ssh/known_hosts文件。设置为no将自动添加到~/.ssh/known_hosts文件。
# IdentityFile ~/.ssh/identity # ssh v1版使用的私钥文件
# IdentityFile ~/.ssh/id_rsa # ssh v2使用的rsa算法的私钥文件
# IdentityFile ~/.ssh/id_dsa # ssh v2使用的dsa算法的私钥文件
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22 # 当命令行中不指定端口时,默认连接的远程主机上的端口
# Protocol 2 #使用SSH第二版本,centos7默认第一版本已拒绝
# Cipher 3des # 指定ssh v1版本中加密会话时使用的加密协议
# Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
# MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
# EscapeChar ~
# Tunnel no
# TunnelDevice any:any
# PermitLocalCommand no # 功能等价于~/.ssh/rc,表示是否允许ssh连接成功后在本地执行LocalCommand指令指定的命令。
# VisualHostKey no # 是否开启主机验证阶段时host key的图形化指纹
# ProxyCommand ssh -q -W %h:%p gateway.example.com
# RekeyLimit 1G 1h
#
# Uncomment this if you want to use .local domain
# Host *.local
# CheckHostIP no
Host *
GSSAPIAuthentication yes # 是否启用基于GSSAPI的身份认证机制
# If this option is set to yes then remote X11 clients will have full access
# to the original X11 display. As virtually no X11 client supports the untrusted
# mode correctly we set this to yes.
ForwardX11Trusted yes
# Send locale-related environment variables
SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
SendEnv XMODIFIERS
在配置大量的节点之间需要ssh连通的时候,如果自动复制很多节点,都需要输入yes,两两节点之间都要互通一次,很麻烦,这里就需要修改配置文件/etc/ssh/ssh_config中的StrictHostKeyChecking 为 no 或者使用命令时添加-o参数,例如:ssh root@10.0.0.45 -o "StrictHostKeyChecking no"
ssh命令参数
常用参数:
-1 强制使用ssh协议版本1
-2 强制使用ssh协议版本2
-4 强制使用IPv4地址
-6 强制使用IPv6地址
-A 开启认证代理连接转发功能
-a 关闭认证代理连接转发功能
-b<IP地址> 使用本机指定的地址作为对位连接的源IP地址
-C 请求压缩所有数据
-F<配置文件> 指定ssh指令的配置文件,默认的配置文件为“/etc/ssh/ssh_config”
-f 后台执行ssh指令
-g 允许远程主机连接本机的转发端口
-i<身份文件> 指定身份文件(即私钥文件)
-l<登录名> 指定连接远程服务器的登录用户名
-N 不执行远程指令
-v 详细模式,将输出debug消息,可用于调试。"-vvv"可更详细。
-V 显示版本号并退出。
-o<选项> 指定配置选项
-p<端口> 指定远程服务器上的端口
-q 静默模式,所有的警告和诊断信息被禁止输出
-X 开启X11转发功能
-x 关闭X11转发功能
-y 开启信任X11转发功能
网友评论