今天進度 : How to automate SSH login with password? - Server Fault
最常見方式
PS C:\> ssh test@172.31.120.220
The authenticity of host '172.31.120.220 (172.31.120.220)' can't be established.
ECDSA key fingerprint is SHA256:xCmbKR2fNHQraYe3U9I3fZyWGsvKcd/HBJgyYC3m/Rk.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.31.120.220' (ECDSA) to the list of known hosts.
test@172.31.120.220's password:
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-48-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Mon 05 Oct 2020 09:04:10 AM UTC
System load: 0.0
Usage of /: 12.2% of 61.76GB
Memory usage: 22%
Swap usage: 0%
Processes: 139
Users logged in: 1
IPv4 address for br-7053d83b1f4c: 172.18.0.1
IPv4 address for docker0: 172.17.0.1
IPv4 address for eth0: 172.31.120.220
* Kubernetes 1.19 is out! Get it in one command with:
sudo snap install microk8s --channel=1.19 --classic
https://microk8s.io/ has docs and details.
51 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Last login: Sun Oct 4 18:04:23 2020 from 172.31.112.1
還可以明碼密碼的方式 (不推薦喔,但挺方便的XD)
test@test:~$ sudo apt-get install sshpass
[sudo] password for test:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
sshpass
0 upgraded, 1 newly installed, 0 to remove and 52 not upgraded.
Need to get 10.5 kB of archives.
After this operation, 30.7 kB of additional disk space will be used.
Get:1 http://hk.archive.ubuntu.com/ubuntu focal/universe amd64 sshpass amd64 1.06-1 [10.5 kB]
Fetched 10.5 kB in 4s (2,500 B/s)
Selecting previously unselected package sshpass.
(Reading database ... 133029 files and directories currently installed.)
Preparing to unpack .../sshpass_1.06-1_amd64.deb ...
Unpacking sshpass (1.06-1) ...
Setting up sshpass (1.06-1) ...
Processing triggers for man-db (2.9.1-1) ...
test@test:~$ sshpass -p test ssh test@127.0.0.1
上面方式不建議,很容易洩漏密碼
使用最正規方式,生成 SSH key 放進 VM 方式
之後就可以擁有授權登入
test@test:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/test/.ssh/id_rsa
Your public key has been saved in /home/test/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:cr4BRARGB2HD7s5TeERswlk7J0pHQGnaTdnquCnofow test@test
The key's randomart image is:
+---[RSA 3072]----+
| =@@*o |
| oB=*.. |
| =.**.. |
| ..o+++ |
| ..=o S |
| + += |
| .oo = o |
|.E.o* o |
|oo.. . . |
+----[SHA256]-----+
test@test:~$ ssh-copy-id test@172.31.120.220
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/test/.ssh/id_rsa.pub"
The authenticity of host '172.31.120.220 (172.31.120.220)' can't be established.
ECDSA key fingerprint is SHA256:xCmbKR2fNHQraYe3U9I3fZyWGsvKcd/HBJgyYC3m/Rk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
test@172.31.120.220's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'test@172.31.120.220'"
and check to make sure that only the key(s) you wanted were added.
test@test:~$ ssh test@172.31.120.220
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-48-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Mon 05 Oct 2020 09:18:00 AM UTC
System load: 0.07
Usage of /: 12.2% of 61.76GB
Memory usage: 23%
Swap usage: 0%
Processes: 145
Users logged in: 1
IPv4 address for br-7053d83b1f4c: 172.18.0.1
IPv4 address for docker0: 172.17.0.1
IPv4 address for eth0: 172.31.120.220
* Kubernetes 1.19 is out! Get it in one command with:
sudo snap install microk8s --channel=1.19 --classic
https://microk8s.io/ has docs and details.
51 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Last login: Mon Oct 5 09:04:11 2020 from 172.31.112.1
test@test:~$