每次ssh登录服务器都要输入一串字符,还要输入密码,很是麻烦。
如比:
ssh [email protected] -p 222
常用的登录命令形式,之后还需要输入密码验证。麻烦。如何才能简化呢。方法如下:
第一步:简化登录命令行
既要达到这么一个效果,即:
ssh 100 <=等效于=> ssh [email protected]
ssh one <=等效于=> ssh username@domain_one.com
ssh two <=等效于=> ssh [email protected]_two -p 222
如何设置呢?很简单,方法如下:修改~/.ssh/config (如果没有.ssh或者config,就新建一个)
test@ubuntu:~$ cd .ssh/
test@ubuntu:~/.ssh$ cat config
Host 100
HostName 192.168.1.100
Port 22
User username
Host one
HostName domain_one.com
Port 22
User username
Host two
HostName domain_two.com
Port 222
User username
保存后,输入:ssh 100 就可以等了服务器了,但是还是需要输入密码。
第二步:实现免密码登录
ssh常用公钥和私钥的方式实现免密码登录,在你安装ssh后,自带了一个ssh-genkey的工具生成公钥和私钥。
设置方法如下:
test@ubuntu:~$
test@ubuntu:~$ cd .ssh/
test@ubuntu:~/.ssh$
test@ubuntu:~/.ssh$ ls
config
test@ubuntu:~/.ssh$
test@ubuntu:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yaolan/.ssh/id_rsa): id_rsa (输入保存的文件名称)
Enter passphrase (empty for no passphrase): (输入Enter键)
Enter same passphrase again: (输入Enter键)
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
14:b5:e4:73:1a:c7:95:d1:f4:86:3e:0c:6d:6e:cc:ef yaolan@VirtualBox
The key's randomart image is:
+--[ RSA 2048]----+
| ..o o=.|
| + o o..o|
| . = = + o|
| . * O . |
| S . O |
| . o |
| .|
| . |
| E|
+-----------------+
test@ubuntu:~/.ssh$ ls
config id_rsa id_rsa.pub
id_rsa私钥,id_rsa.pub公钥,采用RSA加密形式。我们只要把 id_rsa.pub改为 authorized_keys并上传到服务器你的主目录下的.ssh下即可,
方法如下:
test@ubuntu:~/.ssh$ cp id_rsa.pub authorized_keys
test@ubuntu:~/.ssh$
test@ubuntu:~/.ssh$ ls
authorized_keys config id_rsa id_rsa.pub
test@ubuntu:~/.ssh$
test@ubuntu:~/.ssh$ scp authorized_keys [email protected]:~/.ssh
chmod
600
.ssh/authorized_keys
完成这步,我们就可以免密码等了
总结:做完第一步和第二步后,输入 ssh 100就可以直接登录服务器了。如下:
test@ubuntu:~$ ssh 100
Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 4.2.0-35-generic x86_64)
* Documentation: https://help.ubuntu.com/
191 packages can be updated.
108 updates are security updates.
New release '16.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Tue Nov 1 14:44:36 2016 from 192.168.1.120
username@Server:~$
username@Server:~$