使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机。
ssh-keygen 创建公钥和密钥。
ssh-copy-id 把本地主机的公钥复制到远程主机的authorized_keys文件上。
ssh-copy-id 也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh/authorized_keys设置合适的权限 。
步骤1: 用 ssh-key-gen 在本地主机上创建公钥和密钥
root@zc-Lenovo-B450:~$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/zc/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/zc/.ssh/id_rsa. Your public key has been saved in /home/zc/.ssh/id_rsa.pub. The key fingerprint is: db:fd:13:dc:f5:99:4b:0a:c1:53:bf:f3:5f:c3:5c:2e zc@zc-Lenovo-B450 The key's randomart image is: +--[ RSA 2048]----+ | | | . | | . . . | | + ..| | S o. .B| | o o =B+| | . . o E=*| | o.o+| | ..o| +-----------------+
步骤2: 用 ssh-copy-id 把公钥复制到远程主机上
root@zc-Lenovo-B450:~$ ssh-copy-id 192.168.0.191 root@192.168.0.191's password: Now try logging into the machine, with "ssh '192.168.0.191'", and check in:~/.ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.
步骤3: 直接登录远程主机
root@zc-Lenovo-B450:/home/zc# ssh 192.168.0.191 Welcome to Ubuntu 11.10 (GNU/Linux 3.0.0-17-generic-pae i686)* Documentation: https://help.ubuntu.com/364 packages can be updated. 299 updates are security updates.New release '12.04 LTS' available. Run 'do-release-upgrade' to upgrade to it.You have new mail. Last login: Mon Feb 4 15:02:51 2013 from zc-lenovo-b450.local