[root ~]# ssh-keygen # 命令 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): # 输入钥匙对位置和名称,默认位置~/.ssh/.id_rsa,没有特殊要求回车即可 Enter passphrase (empty for no passphrase): # 输入密码,默认为空,回车即可 Enter same passphrase again: # 再次输入刚刚密码 Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:WXeXTXqe31ROBDvhyvQg3GmG6LhvPlj3fN4lPsjhqzU root@instance-92souhhl The key's randomart image is: +---[RSA 2048]----+ | o.o| | o o o *o| | . = O *.*| | o o B = Bo| | . S o ..+| | .. . . .o| | .o . =Eo. +| | ..o .*ooo.| | oo....+o..| +----[SHA256]-----+
# 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 服务器端默认端口为22,如果需要可以改为其他端口,更加安全 #AddressFamily any #ListenAddress 0.0.0.0 服务器端档绑定的IP地址,如果有多个网卡或出现不同的IP地址,可以设置为其中的一块网卡,0.0.0.0表示全部 #ListenAddress ::
# 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 AuthorizedKeysFile用来设置公钥验证文件的路径,与PubkeyAuthentication配合使用,默认值是".ssh/authorized_keys"
下面这7行是关于首次连接添加到known_hosts文件的配置 # 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 是否允许密码登陆,建议配置好ssh后关闭此选项 #PermitEmptyPasswords no 是否允许空密码登陆
# Change to no to disable s/key passwords #ChallengeResponseAuthentication yes 设置禁用s/key 密码
下面6行关于Kerberos有关的设定 # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no #KerberosUseKuserok yes
下面6行关于GSSAPI有关的设定 # 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
# 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
# This is the ssh client system-wide configuration file. See # ssh_config(5) for more information. This file provides defaults for # users, and the values can be changed in per-user configuration files # or on the command line.
# Configuration data is parsed as follows: # 1. command line options # 2. user-specific file # 3. system-wide file # Any configuration value is only changed the first time it is set. # Thus, host-specific definitions should be at the beginning of the # configuration file, and defaults at the end.
# Site-wide defaults for some commonly used options. For a comprehensive # list of available options, their meanings and defaults, please see the # ssh_confg(5) man page.
# Host * # ForwardAgent no 连接是否经过验证代理(如果存在)转发给远程计算机 # ForwardX11 no X11连接是否被自动重定向到安全的通道和显示集(DISPLAY set) # RhostsRSAAuthentication no 是否使用基于rhosts的安全验证 # RSAAuthentication yes 是否使用RSA算法进行安全验证 # PasswordAuthentication yes 是否使用口令验证 # HostbasedAuthentication no 是否使用基于Host的安全验证 # GSSAPIAuthentication no # GSSAPIDelegateCredentials no # GSSAPIKeyExchange no # GSSAPITrustDNS no # BatchMode no 批处理模式,一般设为"no";如果设为"yes",交互式输入口令的提示将被禁止,这个选项对脚本文件和批处理任务十分有用 # CheckHostIP yes 设置ssh是否查看连接到服务器的主机的IP地址以防止DNS欺骗。建议设置为"yes"。 # AddressFamily any # ConnectTimeout 0 # StrictHostKeyChecking ask 如果设为"yes",ssh将不会自动把计算机的密匙加入"$HOME/.ssh/known_hosts"文件,且一旦计算机的密匙发生了变化,就拒绝连接 # IdentityFile ~/.ssh/identity 设置读取用户的RSA安全验证标识 # IdentityFile ~/.ssh/id_rsa # IdentityFile ~/.ssh/id_dsa # IdentityFile ~/.ssh/id_ecdsa # IdentityFile ~/.ssh/id_ed25519 # Port 22 # Protocol 2 # Cipher 3des # 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 # VisualHostKey no # 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 # 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 XMODIFIERSi
客户端配置文件通常只改动:RSA安全验证文件位置等
客户端用户配置文件 ~/.ssh/config
这个文件是客户端用户配置文件,需要自己生成,可以对不同域名指定密钥文件,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13
HOST * ServerAliveInterval 20
Host github.com 指定GitHub的密钥文件 User MinuteSheep PreferredAuthentications publickey IdentityFile ~/.ssh/github
Host 111.22.33.44 指定百度云服务器的密钥文件 User root PreferredAuthentications publickey IdentityFile ~/.ssh/baiduyun