一、自带ssh通过密码连接主机
ssh root@192.168.17.131 -p52113
#通过52113端口,连接这个主机的root用户
在ssh中传文件
(需要在ubuntu的/etc/ssh/sshd_config
下配置:
Port 52113
PermitRootLogin yes
然后重启服务:
service sshd restart
)
scp -rp -P52113 hello.c dawson@192.168.17.131:/home/dawson/
注:
这里是把centos下的hello.c文件传到ubuntu下
二、SSH密钥连接(设置好密钥,连接时不用密码,这两个主机间就能够连接)
这里测试centos通过密钥连接ubuntu:
1.在centos下
在root用户下输入:ssh-keygen
,然后回车就会输出:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
则/root/.ssh/id_rsa
下就存放了私钥,/root/.ssh/id_rsa.pub
下存放公钥
2.在ubuntu下
把centos里的公钥存入你要连接的目录下,比如一个dawson用户(/home/dawson
),然后把id_rsa.pub
文件内容拷到/home/dawson/.ssh/authorized_keys
里(如果没有.ssh
文件就自己创建,权限最好是700,即自己什么都能干,其他人什么都干不了),要注意的是公钥是完整的一行,如果因为复制时多出换行符,记得删了
3.在centos下
前面是通过root生成的密钥,所以在root下输入:
ssh dawson@192.168.17.131 -p52113
就会发现连接上dawson了,并且过程当中不需要密码,所以以后如果centos的root用户想连接哪个主机只要把公钥放在该主机下就行了