跳转到内容

ssh密钥认证

Terminal window
$ ssh-keygen -t rsa
$ cd .ssh/
Terminal window
$ cat id_rsa.pub >> authorized_keys
PermitRootLogin no
PasswordAuthentication no
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
ChallengeResponseAuthentication no
Port 22
Protocol 2,1
ListenAddress 192.168.0.100
PidFile /var/run/sshd.pid
LoginGraceTime 600
Compression yes
HostKey /etc/ssh/ssh_host_key
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
KeyRegenerationInterval 3600
ServerKeyBits 768
SyslogFacility AUTH
LogLevel INFO
PermitRootLogin no
UserLogin no
StrictModes yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
RhostsAuthentication no
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
IgnoreUserKnownHosts no
PasswordAuthentication yes
PermitEmptyPasswords no
ChallengeResponseAuthentication yes
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosTgtPassing no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
PrintMotd no
PrintLastLog yes
KeepAlive yes
UsePrivilegeSeparation yes
MaxStartups 10
DenyUsers test
DenyGroups test
Subsystem sftp /usr/lib/ssh/sftp-server
  1. 将私钥拷贝到用户.ssh目录
  2. 执行ssh-agent bash
  3. 执行ssh-add 私钥文件

SSH 登录太慢可能是 DNS 解析的问题。可以在 sshd 的配置文件(sshd_config)里取消 sshd 的反向 DNS 解析。

vi /etc/ssh/sshd_config
找到UseDNS,修改为no
UseDNS no
重启SSH服务
/etc/init.d/sshd restart
  1. eval "$(ssh-agent -s)" [bash环境]
  2. ssh-add -K ~/.ssh/id_rsa
  3. ~/.ssh/config
Host 111.111.111.111
HostName 111.111.111.111
#Port 22
User xxxxxx
IdentityFile ~/.ssh/id_rsa_xxxx
IdentitiesOnly yes

sshpass命令 – 用于非交互的ssh 密码验证

Section titled “sshpass命令 – 用于非交互的ssh 密码验证”

sshpass命令用于非交互的 ssh 密码验证。

Terminal window
sshpass -p "password" ssh username@ip
sshpass -p "password" ssh -p 8443 username@ip
sshpass -f xxx.txt ssh [email protected]
sshpass -p '123456' scp root@host_ip:/home/test/t ./tmp/