Unix How To:- How to connect to remote without providing PASSPHRASE through SSH/SFTP?
1/Say you want to connect to REMOTE from LOCAL through SSH.
2/Create pub-priv key pair in ~/.ssh directory in LOCAL.
**** DONT GIVE ANY PASSPHRASE ****
* DONT CHANGE THE DEFAULT FILE NAME WHERE PUB/PRIV KEY WILL BE SAVED *
LOCAL> ssh-keygen
Generating public/private rsa key pair.
Please be patient.... Key generation may take a few minutes
Enter file in which to save the key (/mnt/abstract/home/abstractinfo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /mnt/abstract/home/abstractinfo/.ssh/id_rsa.
Your public key has been saved in /mnt/abstract/home/abstractinfo/.ssh/id_rsa.pub.
The key fingerprint is:
db:b3:12:a1:39:e6:4c:f5:96:1b:44:fa:e1:52:3d:9a abstractinfo@LOCAL
LOCAL> l
total 18
-rw------- 1 abstractinfo abstractinfo 1024 Oct 1 2004 prng_seed
-rw-r--r-- 1 abstractinfo abstractinfo 4656 Oct 3 14:56 known_hosts
-rw------- 1 abstractinfo abstractinfo 1751 Oct 3 15:02 id_rsa
-rw-r--r-- 1 abstractinfo abstractinfo 396 Oct 3 15:02 id_rsa.pub
LOCAL> cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA7ZwfwrbB+Q1DVjvQI5qk5UL2arq1HYbUoFeJGAl70p+pHjRqqAl9lZkvUv8Fet0fy8S70ukzE7eCqsGA0Mfh5LJBDgcH41cfTPo5nacihjSwzbWfo9zKl+rGYTBdZ2SOxvyeGxrwhJH5kLkclG6obmIy1Z2Mhlm4y1gYt576fgk598976VWIajFZT4igTAKqGmkFM7TlnZGLPktfw1YfOPqVS3fR4s5+WKeUJ19qsUqdCJBhvWjiAKFuWQft9DTW0k5w4QtxuJIMvslPUNKZz2EEwsWvpk9tVB7dORuUtBIvvGUrNY0DiXytNQjz5ar94P2qtDdDQyv8PPUWw== abstractinfo@LOCAL
3/Append that public key in REMOTE MACHINE`s ~/.ssh/authorized_keys file.
** If that ~/.ssh/authorized_keys file is not there create it with 640 permission **
REMOTE> cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtww1sd36jrpwPfJ7ILka5iGFvVSUL4jYd3fpLm0cHLRw08oKkmCvki9w02S97MSqPADKHA6Dax5kYrv0wJQmTrAo6niPmUjhDrXYJomQLbIIdiUDEeFP7kPtar9UY5ybU56bzaj/lvCNTgdNjXxiUJG7ZdkfBLiLiHRC4nTRLGciAbD2W2I4zff9E3+xv8PB20JYgq+07gjqjHtFXCkoGlmzz+vfzUWmDtvuHF+xr7TGd6VRom/9bH91xFPAdOKQ4DRJpf/9W1NGe6Jp/ToCQsRjf8kxQra250FoJW1tyx6vx3uHrnN08QUo37KMPe3ekjYHOUHLutNYj8ffiYZA+w== abstractinfo@LOCAL
REMOTE> l authorized_keys
-rw-r----- 1 abulhasi abstractinfo 396 Oct 3 15:05 authorized_keys
4/Test it from LOCAL.
LOCAL> ssh -v abulhasi@REMOTE
OpenSSH_4.4p1-hpn12v11, OpenSSL 0.9.7l 28 Sep 2006
HP-UX Secure Shell-A.04.40.006, HP-UX Secure Shell version
debug1: Reading configuration data /opt/ssh/etc/ssh_config
debug1: Connecting to REMOTE [##.##.###.###] port 22.
debug1: Connection established.
debug1: identity file /mnt/abstract/home/abstractinfo/.ssh/identity type -1
debug1: identity file /mnt/abstract/home/abstractinfo/.ssh/id_rsa type 1
debug1: identity file /mnt/abstract/home/abstractinfo/.ssh/id_dsa type -1
debug1: Remote protocol version 1.99, remote software version OpenSSH_4.4p1-hpn12v11
debug1: match: OpenSSH_4.4p1-hpn12v11 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.4p1-hpn12v11
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'REMOTE' is known and matches the RSA host key.
debug1: Found key in /mnt/abstract/home/abstractinfo/.ssh/known_hosts:18
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive,hostbased
debug1: Next authentication method: publickey
debug1: Trying private key: /mnt/abstract/home/abstractinfo/.ssh/identity
debug1: Offering public key: /mnt/abstract/home/abstractinfo/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 247
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
debug1: Final hpn_buffer_size = 131072
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
Warning: No xauth data; using fake authentication data for X11 forwarding.
debug1: Requesting X11 forwarding with authentication spoofing.
Last login: Wed Oct 3 14:59:05 2007 from LOCAL
Last login: 3 Oct 14:59
REMOTE>
5/Once that succeeds you may have your automated SFTP script as follows:-
LOCAL> cat hasimsftp.sh
# -v is for verbose mode and -b is for all the commands in a batchfile.
sftp -v -b sftpcommandbatch.dat abulhasi@REMOTE <
LOCAL> cat sftpcommandbatch.dat
lcd /mnt/abstract/home/abstractinfo
cd /home/abulhasi
mput a.sql
ls -lrt
6/Test that:-
LOCAL> hasimsftp.sh
OpenSSH_4.4p1-hpn12v11, OpenSSL 0.9.7l 28 Sep 2006
HP-UX Secure Shell-A.04.40.006, HP-UX Secure Shell version
debug1: Reading configuration data /opt/ssh/etc/ssh_config
debug1: Connecting to REMOTE [##.##.###.###] port 22.
debug1: Connection established.
debug1: identity file /mnt/abstract/home/abstractinfo/.ssh/id_rsa type 1
debug1: identity file /mnt/abstract/home/abstractinfo/.ssh/id_dsa type -1
debug1: Remote protocol version 1.99, remote software version OpenSSH_4.4p1-hpn12v11
debug1: match: OpenSSH_4.4p1-hpn12v11 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.4p1-hpn12v11
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'REMOTE' is known and matches the RSA host key.
debug1: Found key in /mnt/abstract/home/abstractinfo/.ssh/known_hosts:18
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive,hostbased
debug1: Next authentication method: publickey
debug1: Offering public key: /mnt/abstract/home/abstractinfo/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 247
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
debug1: Final hpn_buffer_size = 131072
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending subsystem: sftp
sftp> lcd /mnt/abstract/home/abstractinfo
sftp> cd /home/abulhasi
sftp> mput a.sql
Uploading a.sql to /home/abulhasi/a.sql
sftp> ls -lrt
drwxr-xr-x 4 abulhasi abstractinfo 96 Oct 2 17:53 test.txt
-rw-r--r-- 1 abulhasi abstractinfo 11500 Oct 3 15:29 a.sql
sftp>
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 0.5 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 0
LOCAL>
---: I am not responsible for any damages happened from the suggestion of my blog :---
Reach me at : m.a.hasim@inbox.com
Labels: Technical_HowTo_Unix
<< Home