Another small trick, using the expect scripting language to implement SSH automatic connection to the server, you can automatically enter the password. Because every time using SSH root@xx.xx.xx.xx connection server, you have to manually enter the password, there is no way to automatically enter the password it?
Search on the Internet, there is a way, there is a script language expect, its role is to interact with the program to "programmable" session, such as automatic input password, hit the "password" character will automatically enter the password.
The first thing to do is install the expect script, but bring it in the Macbook. For example, a CentOS machine can be installed using Yum install expect.
This simple little script Ali.exp as follows (note that the script file format is exp):
#!/usr/bin/expect
Spawn ssh root@112.xx.xx.47
Expect "Password:"
Send "a0000223456700\r"
Interact
This way, after you set the Ali.exp file as executable, you can./ali.exp run the script and automatically connect to the server.
Spawn is followed by the shell command to execute, expect is to capture the characters to wait for input, send is automatically entered the content, note to "\ r" to indicate a newline to confirm the input.
So, this scripting language can be used in a lot of places, under Linux, often need to type the script of content, you can consider using expect to implement automated interactive scripts to replace human repetitive activities.
Supplemental method shell script SSH automatic login
To run a command on a:
# ssh-keygen-t RSA (three consecutive returns, that is, the public and private keys are generated locally, no password is set)
# SSH root@192.168.1.10 "mkdir. SSH" (create. SSH directory on 1.10, you need to enter a password)
# SCP ~/.ssh/id_rsa.pub root@192.168.1.10:.ssh/id_rsa.pub (need to enter password)
The Order on B:
# Touch/root/.ssh/authorized_keys (If this file is already present, skip this one)
# cat/root/.ssh/id_rsa.pub >>/root/.ssh/authorized_keys (append id_rsa.pub content to
In Authorized_keys)
Back to a machine:
# SSH root@192.168.1.10 (no password required, login successful)
Do rsync sync on the B machine without having to enter a password or use a password file.
Rsync-avzu--progress--delete root@192.168.1.9:/var/www//var/www (192.168.1.10 client execution)
Add two: Automatically login Linux server with SSH public key
The first step: generate an SSH public key pair
Today's Linux servers are installed with OpenSSH software by default.
The generated public key key pair is generated on the Management Server:
[Root@server ~]# ssh-keygen-b 1024-t RSA
Generating public/private RSA key pair. #提示正在生成rsa密钥对
Enter file in which to save the key (/HOME/USRNAME/.SSH/ID_DSA): #询问公钥和私钥存放的位置, enter with the default location
Enter passphrase (empty for no passphrase): #询问输入私钥密语, enter the password
Enter same passphrase again: #再次提示输入密语确认
Your identification has been saved IN/HOME/USRNAME/.SSH/ID_DSA. #提示公钥和私钥已经存放在 the/root/.ssh/directory
Your public key has been saved in/home/usrname/.ssh/id_dsa.pub.
The key fingerprint is:
x6:68:xx:93:98:8x:87:95:7x:2x:4x:x9:81:xx:56:94 Root@server #提示key的指纹
-B 1024 uses a 1024-byte public/private key pair, a maximum of 4096 bytes, generally 1024 or 2048 is enough to meet security needs, too long encryption and decryption need to increase the time.
-t RSA uses the RSA encryption method public/private key pair, in addition to RSA and the DSA Way, the RSA method shortest cannot be less than 768 byte length.
If you need to use more other parameters please refer to man Ssh-keygen.
In the process of generating a key pair, you are asked: Enter the password passphrase (empty for no passphrase), the password (passphrase) is you use a phrase or a sentence as a password input, Then by the system internal encryption or hashing algorithm to generate a virtual password, the next step of authentication. The advantage is that it enhances security and is not easily cracked. Read a lot of articles, inside all put this phrase empty, that is, the representative does not use the password. Here I strongly urge you to enter the password phrase. Some people will say that after the use of password, login and enter the password to use this is no more convenient than using user name and password login, I said please do not worry, then read my article.
Note: If you generate a key pair without setting a passphrase, if your private key is lost, your problem may be worse than losing your username password.
Step Two: Copy your public key to the managed server
On your Management Server, copy your public key to the user directory on the managed server to be automatically logged in.
[Root@server ~]# SCP. Ssh/id_dsa.pub remote_usrname@192.168.0.2: #比如你想使用用户peter登陆, then remote_usrname please use Peter instead
Renaming and making permission settings
Log on to the managed server, enter the user directory that needs to be logged in remotely, and place the public key in the user directory. SSH directory (if directory does not exist, need to create ~/.ssh directory, and set directory permissions to 700), rename the public key to Authorized_keys2, and set its user permissions to 600.
[Peter@client ~]$ ls
Id_rsa.pub
[Peter@client ~]$ mkdir ~/.ssh #如果当前用户目录下没有. SSH directory, please create the directory first
[Peter@client ~]$ chmod ~/.ssh
[Peter@client ~]$ MV Id_rsa.pub ~/.ssh
[Peter@client ~]$ CD ~/.ssh
[Peter@client ~]$ cat id_rsa.pub >> authorized_keys2
[Peter@client ~]$ rm-f id_rsa.pub
[Peter@client ~]$ chmod authorized_keys2
[Peter@client ~]$ Ls-l
Total 4
-RW-------1 Peter the Oct 11:28 authorized_keys2
Test uses key pair for remote login
[Root@server ~]# ssh peter@192.168.0.2
Enter passphrase for key '/root/.ssh/id_rsa ': #提示输入密码短语, enter the passphrase you just set
Last Login:sun Oct 11:32:14 from 192.168.0.1
[Peter@client ~]$
If you are not able to log in correctly, you should check your AUTHORIZED_KEYS2 permissions again. You may also want to check the permissions for the. SSH directory.
Use Ssh-agent (SSH proxy) to automatically enter a passphrase
Remember your "password phrase", now you can use your key instead of the password to log on to your server, but this still does not save anything, you still want to enter the key "passphrase." Is there a simpler way to do it? The answer is to use an SSH proxy (ssh-agent), a program that helps you remember the phrase "passphrase." Ssh-agent is the SSH agent that is included by default in OpenSSH.
Log on to the admin server
[Root@server ~]# Ssh-agent
ssh_auth_sock=/tmp/ssh-vegjcm2147/agent.2147; Export Ssh_auth_sock;
ssh_agent_pid=2148; Export ssh_agent_pid;
echo Agent pid 2148;
When you run ssh-agent, it will print out the SSH environment and variables it uses. There are two ways to use these variables, one is to declare the environment variables manually, and the other is to run the eval command to declare the environment variables automatically.
Method One: Manually declaring environment variables
[Root@server ~]# ssh_auth_sock=/tmp/ssh-vegjcm2147/agent.2147; Export Ssh_auth_sock;
[Root@server ~]# ssh_agent_pid=2148; Export ssh_agent_pid;
[Root@server ~]# printenv | grep ssh #检查 SSH environment variable has joined the current session's environment variable
ssh_agent_pid=2148
ssh_auth_sock=/tmp/ssh-vegjcm2147/agent.2147
Method Two: Run the eval command to automatically declare environment variables
[Root@server ~]# eval ' ssh-agent '
Agent PID 2157
[Root@server ~]# printenv | grep ssh #检查 SSH environment variable has joined the current session's environment variable
ssh_agent_pid=2148
ssh_auth_sock=/tmp/ssh-vegjcm2147/agent.2147
Now the ssh-agent is already running, but the ssh-agent inside is blank and there is no private key to decrypt. We're going to tell it where we have the private key and where the private key is. This requires the use of the Ssh-add command to add our private key to the ssh-agent cache.
[Root@server ~]# Ssh-add ~/.SSH/ID_DSA
Enter Passphrase FOR/HOME/USER/.SSH/ID_DSA: #输入你的密码短语
Identity added:/home/user/.ssh/id_dsa (/HOME/USER/.SSH/ID_DSA)
[Root@server ~]# ssh-add-l #查看 The cached content of the SSH agent
1024 72:78:5e:6b:16:fd:f2:8c:81:b1:18:e6:9f:77:6e:be/root/.ssh/id_rsa (RSA)
Enter the password phrase, now, you can log on to your remote server without typing your passphrase, and your private key is password protected. Try it!
[Root@server ~]# ssh peter@192.168.0.2
Last Login:sun Oct 11:32:45 from 192.168.0.1
[Peter@client ~]$
Log on to the server after the operation, remember to turn off the ssh-agent, or other people can be remote after landing.
[Root@server ~]# Ssh-agent-k
Unset Ssh_auth_sock;
Unset ssh_agent_pid;
Echo Agent pid 2148 killed;
[Root@server ~]# ssh-add-l #查看一下, there is no key in the cache.
The agent has no identities.
Enter the next time you log in
#ssh-add
Enter a password hint
#ssh-y Username@ip