一、实验的目的
了解密钥对的创建和使用,掌握免密码远程登录和远程操作
二、实验环境
本地主机 rh1: 192.168.233.3/24
远程主机 rh2: 192.168.233.4/24
在putty上,以用户yanji登录rh1。
三、实验过程(写出屏幕上的输出,必要时作出评述或解释)
1、创建密钥对
主机rh1,用户yanji。
$zh
$cd
$mkdir -pm 700 .ssh
$ssh-keygen -f .ssh/id_dsa -t dsa
[[email protected] yanji]$ zh
[[email protected] yanji]$ cd
[[email protected] yanji]$ mkdir -pm 700 .ssh
[[email protected] yanji]$ ssh-keygen -f .ssh/id_dsa -t dsa
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_dsa.
Your public key has been saved in .ssh/id_dsa.pub.
The key fingerprint is:
19:0a:c1:03:f5:5a:d6:1f:fe:ba:2b:bb:8a:98:a9:2c [email protected]
口令串passphrase为空。查看产生的密钥对:
私钥:
$cd
$cat .ssh/id_dsa
[[email protected] yanji]$ cd
[[email protected] yanji]$ cat .ssh/id_dsa
-----BEGIN DSA PRIVATE KEY-----
MIIBuwIBAAKBgQCg2+/fn+RwjHYUDYA0hczgX/dRf0ljszAUeopo4QpF4K+WhmRM
fNsfdigzCA9Y7/aMeZ5olT69KfaKmEGJs2apvpcdtPdazHQgU3CyGneQly5USP0K
xwtrsF6SyllrDzS42JJrEgnTAvn3ix1KSy8mbZjfaCzFD3lDuRrk9kxWTQIVALcY
qQDmMAmq4OgxURdDvIuO0GPzAoGAA0ANpEGkECDi25QENHUSXwLyKEdcCwppE7VT
XgLJ+qZNnLND1xSPuJi4oD1H0XpuG+5tyoTfrizoEOBZfKfSSHwVPnSQKzB11o4r
Nn2H1pj6hX1nvCeNYHVZ/C5Y8XhfLK+CzWZy+avG17PrNHWSPoItMhP0/Oq7HW2T
LyFGWBsCgYBLGHGc7AOIUeuiBNMiQxLX8itbhxGtK2dNWJYb6CL8D2t3j7loEDGY
6y5EXhNcajIEDB7IBf0d7qYiqx8sBcf1+cgNOvJhySSe33XQNeDteJuQoNPOMcYu
uivB5IezWcO08+lfov4RqMdJQpJ+dcRE1i1s5sbxepQGU0jtroDKIAIVAIWu/kwt
gTQnfm3OPTAErqESbbb7
-----END DSA PRIVATE KEY-----
公钥:
$cat .ssh/id_dsa.pub
[[email protected] yanji]$ cat .ssh/id_dsa.pub
ssh-dss AAAAB3NzaC1kc3MAAACBAKDb79+f5HCMdhQNgDSFzOBf91F/SWOzMBR6imjhCkXgr5aGZEx82x92KDMID1jv9ox5nmiVPr0p9oqYQYmzZqm+lx2091rMdCBTcLIad5CXLlRI/QrHC2uwXpLKWWsPNLjYkmsSCdMC+feLHUpLLyZtmN9oLMUPeUO5GuT2TFZNAAAAFQC3GKkA5jAJquDoMVEXQ7yLjtBj8wAAAIADQA2kQaQQIOLblAQ0dRJfAvIoR1wLCmkTtVNeAsn6pk2cs0PXFI+4mLigPUfRem4b7m3KhN+uLOgQ4Fl8p9JIfBU+dJArMHXWjis2fYfWmPqFfWe8J41gdVn8LljxeF8sr4LNZnL5q8bXs+s0dZI+gi0yE/T86rsdbZMvIUZYGwAAAIBLGHGc7AOIUeuiBNMiQxLX8itbhxGtK2dNWJYb6CL8D2t3j7loEDGY6y5EXhNcajIEDB7IBf0d7qYiqx8sBcf1+cgNOvJhySSe33XQNeDteJuQoNPOMcYuuivB5IezWcO08+lfov4RqMdJQpJ+dcRE1i1s5sbxepQGU0jtroDKIA==
[email protected]
[[email protected] yanji]$
2、上传公钥到rh2
主机rh1,用户yanji。
$cd
$scp .ssh/id_dsa.pub [email protected]:.
[[email protected] yanji]$ scp .ssh/id_dsa.pub [email protected]:.
[email protected]‘s password:
id_dsa.pub 100% |*****************************| 617 00:00
3、远程登录rh2
$主机rh1,用户yanji。
$ssh [email protected]
...
#
[[email protected] yanji]$ ssh [email protected]
[email protected]‘s password:
Last login: Fri Jan 30 15:08:33 2015 from 192.168.233.1
现在到了rh2。
4、添加yanji的公钥到授权密钥文件
#mkdir -pm 600 .ssh
#cat id_dsa.pub >> .ssh/authorized_keys
#chmod 600 .ssh/authorized_keys
#exit
$
[[email protected] root]# mkdir -pm 600 .ssh
[[email protected] root]# cat id_dsa.pub >> .ssh/authorized_keys [[email protected] root]# chmod 600 .ssh/authorized_keys
[[email protected] root]# exit
[[email protected] yanji]$
现在回到了rh1。
5、免密码远程登录
主机rh1,用户yanji。
远程登录rh2,创建文件poem1后退出。
$ssh [email protected]
#zh
#cat > poem1
唧唧复唧唧
木兰当户织
不闻机杼声
惟闻女叹息
#exit
$
[[email protected] yanji]$ ssh [email protected]
Last login: Fri Jan 30 15:13:39 2015 from 192.168.233.3
[[email protected] root]# zh
[[email protected] root]# cat > poem1
唧唧复唧唧
木兰当户织
不闻机杼声
惟闻女叹息
[[email protected] root]# exit
logout
Connection to 192.168.233.4 closed.
[[email protected] yanji]$
现在又回到了rh1
6、远程复制
主机rh1,用户yanji
$ls
$scp [email protected]:poem1 .
$ls
$cat poem1
[[email protected] yanji]$ ls
01
[[email protected] yanji]$ scp [email protected]:poem1 .
poem1 100% |********************************| 64 00:00
[[email protected] yanji]$ ls
01 poem1
[[email protected] yanji]$ cat poem1
唧唧复唧唧
木兰当户织
不闻机杼声
惟闻女叹息
6、远程操作(远程执行命令)
主机rh1,用户yanji。
$ssh [email protected] ls -l
$ssh [email protected] "echo ‘(木兰诗片段)‘ >> poem1"
$ssh [email protected] "cat poem1"
[[email protected] yanji]$ ssh [email protected] ls -l
total 160
-rw-r--r-- 1 root root 1837 Nov 9 03:51 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 21 13:41 backup
drwxr-xr-x 2 root root 4096 Nov 16 01:44 bin
drwxr-xr-x 2 root root 4096 Nov 21 07:48 c
-rw-r--r-- 1 root root 49 Nov 21 13:52 cron.txt
-rw-r--r-- 1 root root 182 Nov 21 05:53 dd
-rw-r--r-- 1 root root 12 Nov 21 08:01 ee
-rw-r--r-- 1 root root 50 Nov 16 01:41 file
-rw-r--r-- 1 root root 0 Nov 21 01:53 file1
-rw-r--r-- 1 root root 0 Nov 21 01:53 file4
-rw-r--r-- 1 root root 0 Nov 21 01:53 file44
drwxr-xr-x 2 root root 4096 Nov 21 02:51 file55
drwxr-xr-x 2 root root 4096 Nov 21 01:57 file554
-rwxr-xr-x 1 root root 11816 Nov 16 02:09 hexagon
-rw-r--r-- 1 root root 339 Nov 16 02:08 hexagon.c
-rw-r--r-- 1 root root 460 Nov 16 02:10 hexagon.dat
-rw-r--r-- 1 root root 1696 Nov 16 01:52 hexagon.o
-rw-r--r-- 1 root root 151 Nov 16 02:12 hexagon.p
-rw-r--r-- 1 root root 617 Jan 30 15:10 id_dsa.pub
-rw-r--r-- 1 root root 12 Nov 21 08:03 info
-rw-r--r-- 1 root root 19564 Nov 9 03:50 install.log
-rw-r--r-- 1 root root 4268 Nov 9 03:50 install.log.syslog
-rwxr-xr-x 1 root root 103 Nov 21 15:22 l1
-rwxr-xr-x 1 root root 137 Nov 21 08:08 let
-rw-r--r-- 1 root root 3 Nov 14 16:15 list.o
-rwxr-xr-x 1 root root 104 Nov 14 12:10 mean
drwxr-xr-x 2 root root 4096 Nov 12 08:12 mmmmm
-rw-r--r-- 1 root root 762 Nov 21 07:44 named.conf
-rw-r--r-- 1 root root 104 Nov 11 07:04 nn
drwxr-xr-x 2 root root 4096 Nov 21 04:01 p
-rw-r--r-- 1 root root 64 Jan 30 15:20 poem1
drwxr-xr-x 2 root root 4096 Nov 21 04:01 pp
-rwxr-xr-x 1 root root 133 Nov 21 05:24 shell
-rw-r--r-- 1 root root 29 Nov 21 14:00 time
-rw-r--r-- 1 root root 9767 Nov 21 15:54 xx.tar.gz
[[email protected] yanji]$ ssh [email protected] "echo ‘(木兰诗片段)‘ >> poem1"
[[email protected] yanji]$ ssh [email protected] "cat poem1"
唧唧复唧唧
木兰当户织
不闻机杼声
惟闻女叹息
(木兰诗片段)
四、实验的体会
知道部分远程操控的操作和免密码登录,自己还是那么粗心,一开始就打错一个字母,搞了很久才发现。
版权声明:本文为博主原创文章,未经博主允许不得转载。