SSH时不需输入密码

时间:2022-07-01 21:59:19
 
我这里有2台机器,一台装了Teradata数据库,ip是192.168.184.128,称它为teradata-pc;另一台装了Oracle数据库,ip地址是192.168.184.129,称它为oracle-pc。有时需要从oracle-pc访问teradata-pc,每次都需要输入密码,比较麻烦。通过设置ssh免密码登陆可以解决这个问题。(更多的时候是——某个server上跑的程序需要ssh到另一个server,而又不能把密码hardcode在程序里。所以采用ssh的方式就比较安全和方便。)
 
实验目的:从oracle-pc连接teradata-pc时不需要输入密码(用root连接)。
 
步骤:
1、若之前没有设置过ssh的话,需要在主目录下建立.ssh文件夹;
2、在oracle-pc主目录下的.ssh文件夹上生成密钥对,并copy公钥为authorized_keys;
3、把生成的authorized_keys文件copy到teradata-pc的.ssh目录下;
4、ssh到teradata-pc时已经不需要输入密码了;
 
 
1、登陆到teradata-pc,在用户主目录(我们这里用root测试)下创建.ssh目录,用于存放authorized_keys文件。
 
TDExpress14.0.3_Sles10:~ # cd ~
 
TDExpress14.0.3_Sles10:~ # mkdir .ssh
 
 
 
2、登陆到oracle-pc,创建.ssh目录,生成密钥对文件,并copy公钥为authorized_keys
 
 
[root@ora11g ~]# cd ~
 
[root@ora11g ~]# mkdir .ssh
[root@ora11g ~]# cd .ssh
 
 
//生成密钥对文件
 
[root@ora11g .ssh]# ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.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:
5e:e1:73:ea:c8:c7:bd:57:02:68:9b:ac:20:73:ff:55 root@ora11g.oracle.com

[root@ora11g .ssh]# ll
total 8
-rw------- 1 root root 1679 May 14 17:45 id_rsa
-rw-r--r-- 1 root root  404 May 14 17:45 id_rsa.pub

 
 
//copy公钥文件id_rsa.pub为authorized_keys
 
[root@ora11g .ssh]# cp id_rsa.pub authorized_keys
 
 
 
3、把生成的authorized_keys文件copy到teradata-pc的.ssh目录下
 
[root@ora11g .ssh]# scp ~/.ssh/authorized_keys root@192.168.184.128:/root/.ssh/

The authenticity of host '192.168.184.128 (192.168.184.128)' can't be established.
RSA key fingerprint is fc:a7:63:38:85:4f:f9:bb:1c:81:5e:53:fc:ec:4f:28.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.184.128' (RSA) to the list of known hosts.
Password:
authorized_keys                    100%  404     0.4KB/s   00:00 
  
 

4、从oracle-pc通过ssh连接到teradata-pc时已经不需要输入密码了
 
[root@ora11g .ssh]# ssh 192.168.184.128
Last login: Wed May 14 05:39:08 2014 from 192.168.184.1
Your use is subject to the terms and conditions of
            the click through agreement that brought you to this
            screen ("TERADATA EXPRESS") EVALUATION AND DEVELOPMENT
            LICENSE AGREEMENT), including the restriction that this
            evaluation copy is not for production use.
 
 
//成功连接到teradata-pc
 
TDExpress14.0.3_Sles10:~ # hostname
TDExpress1403_Sles10