当前位置:   article > 正文

配置ssh密钥对认证及scp、ftp等文件传输_密钥ftp

密钥ftp

目录

ssh密钥配对认证

文件传输


 

linux主机之间 

 telnet 192.168.1.120 ssh          #测试对方的ssh 服务是否开启 
 #按Ctrl + ] 会呼出telnet的命令行

ssh密钥配对认证

  • openssh 连接 
  1. [root@localhost ~]# ping ton.tom -c2 //没ping通主机名在/etc/hosts下添加 或者直接ping对方ip
  2. PING ton.tom (192.168.1.111) 56(84) bytes of data.
  3. 64 bytes from ton.tom (192.168.1.111): icmp_seq=1 ttl=64 time=1.03 ms
  4. 64 bytes from ton.tom (192.168.1.111): icmp_seq=2 ttl=64 time=0.883 ms
  5. --- ton.tom ping statistics ---
  6. 2 packets transmitted, 2 received, 0% packet loss, time 3ms
  7. rtt min/avg/max/mdev = 0.883/0.954/1.025/0.071 ms
  8. [root@localhost ~]#
  9. [root@localhost .ssh]# ssh ton.tom
  10. The authenticity of host 'ton.tom (192.168.1.111)' can't be established.
  11. RSA key fingerprint is SHA256:7oVlQO5rj8RWD0uXREAYX1Z/YSgSpKg0YMUywjTnHrA.
  12. Are you sure you want to continue connecting (yes/no)? yes
  13. Warning: Permanently added 'ton.tom' (RSA) to the list of known hosts.
  14. root@ton.tom's password:
  15. Last login: Sun Jul 26 04:23:39 2020 from 192.168.1.120
  16. [root@ton ~]#
  17. [root@ton ~]# ls
  18. anaconda-ks.cfg file_creat.c linux-2.6.28.10.tar.gz
  19. [root@ton ~]# exit
  20. logout
  21. Connection to ton.tom closed.
  22. [root@localhost ~]#
  • 添加key认证,免密码登陆 
  1. [server@localhost ~]$ ssh-keygen #生成密钥对
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/home/server/.ssh/id_rsa):
  4. Created directory '/home/server/.ssh'.
  5. Enter passphrase (empty for no passphrase): #直接回车不添加
  6. Enter same passphrase again:
  7. Your identification has been saved in /home/server/.ssh/id_rsa.
  8. Your public key has been saved in /home/server/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. SHA256:CwLPbgDUDAliuAiFe69bEWJN2raeyw5Qc1GaUr+Ocj0 server@localhost
  11. The key's randomart image is:
  12. +---[RSA 2048]----+
  13. |=** +.. |
  14. |B. O = |
  15. |+oO O . |
  16. |++.X o . |
  17. |....* o S |
  18. | . +.B . . |
  19. | o.O E . |
  20. | .B . . |
  21. | .o+ |
  22. +----[SHA256]-----+
  23. [server@localhost ~]$ ssh-copy-id yonghu@192.168.1.111 #把公钥文件上传到对应用户
  24. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/server/.ssh/id_rsa.pub"
  25. The authenticity of host '192.168.1.111 (192.168.1.111)' can't be established.
  26. RSA key fingerprint is SHA256:7oVlQO5rj8RWD0uXREAYX1Z/YSgSpKg0YMUywjTnHrA.
  27. Are you sure you want to continue connecting (yes/no)? yes
  28. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  29. /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
  30. yonghu@192.168.1.111's password:
  31. Number of key(s) added: 1
  32. Now try logging into the machine, with: "ssh 'yonghu@192.168.1.111'"
  33. and check to make sure that only the key(s) you wanted were added.
  34. [server@localhost ~]$ ssh yonghu@192.168.1.111 #这时就可以不用输入密码直接登入了
  35. [yonghu@ton ~]$ ls
  • 涉及到的命令 
  1. ssh-keygen -t rsa #生成密钥对
  2. ssh-keygen -t dsa # -t type #密钥类型
  3. ssh-copy-id -i ~/.ssh/id_rsa.pub root@ton.tom #-i identity_file 将本机的公钥复制到远程用户地址上

 

 /etc/ssh/sshd_config需对应配置文件

  1. Port 22
  2. Protocol 2
  3. PermitRootLogin yes #允许root可以ssh登陆
  4. PubkeyAuthentication yes #设置密钥对的访问方式
  5. PasswordAuthentication yes
  6. AuthorizedKeysFile .ssh/authorized_keys

tips: 

使用 ssh 加命令 的方式比较安全

同时为了连接方便可以使用alias

alias ton="ssh root@ton.tom "       (hostname后面加多一个空格以免后面命令连接上来,同时可将alias写入~/.bashrc中)

执行时直接 

ton "mkdir /tmp/test"

  1. [root@localhost ~]# ssh root@192.168.1.111 "ls /home"
  2. root@192.168.1.111's password:
  3. bincopy.sh
  4. initrd.img
  5. iso
  6. pub
  7. tom
  8. vmlinuz
  9. yonghu
  10. [root@localhost ~]#
  •  SecureCRT公钥连接主机(即crt与主机间的免密认证登陆)

 
 tools -> create public key -> 选择加密算法 
 rsa    并且加密长度设置最长
 
 保存成id_rsa
 生成全局id_rsa.pub   

选择生成OpenSSH  密钥格式

 拷贝其中公钥id_rsa.pub的内容到服务器上的.ssh文件夹下的authorized_keys文件中
 

authorized_keys 可直接手动创建

secureCRT生成公钥
  1. #手动创建需如下
  2. -rw-------  1 tom tom  381 Jul 26 07:28 authorized_keys      ----注意属组与权限600
  3. drwx------  2 tom  tom  4096 Jul 26 07:28 .ssh               ----目录属组与权限700

文件传输

  • 使用scp命令直接上传文件到ssh用户端

[server@localhost ~]$ scp tom.txt yonghu@192.168.1.111:/home/yonghu        #上传

[server@localhost ~]$ scp yonghu@192.168.1.111:/tmp/3.txt ./           #下载到本地当前目录下

-p       保留文件的时间和权限     preserve

-r       递归        recursive

-C      压缩传输时的数据流

  • 同步rsync

rsync -av root@192.168.1.111:/var/log /server/     将服务器上的整个目录同步到当前   ./server目录下

 

scp拷贝比较生硬,没有分析文件不同,rsync则不同

rsync会比scp更好传输文件他会发送文件的时间,大小,和md5来判断文件是否需要重传

  • sftp 
  1. [server@localhost ~]$ sftp yonghu@192.168.1.111
  2. Connected to yonghu@192.168.1.111.
  3. sftp> put tom.txt #上传
  4. Uploading tom.txt to /home/yonghu/tom.txt
  5. tom.txt 100% 0 0.0KB/s 00:00
  6. sftp> get /tmp/3.txt #从服务器端下载
  7. Fetching /tmp/3.txt to 3.txt
  8. sftp> exit

如果遇到ftp connection refused,可以 service vsftpd restart

ftp遇到的错误 

  1. ftp: root@192.168.3.139: Temporary failure in name resolution
  2. #网关配置问题
  3. ftp: root@192.168.3.139: Name or service not known
  4. sudo iptables -F #可关闭iptables

 

 

  • lftp - Sophisticated file transfer program

mget *.iso       下载多个文件
lcd  切换本地下载目录 
get [OPTS] <rfile> [-o <lfile>]      

[root@localhost ~]# lftp -u root,123456  192.168.1.11        / user:passwd
lftp root@192.168.1.111:~> pwd                       打印出一个ftp链接
ftp://root:123456@192.168.1.111/%2Froot

可使用root去ftp
vsftpd/ user_list
vsftpd/ ftpusers

  •  nfs

NFS /etc/export配置
/var/ftp/pub   192.168.1.0/255.255.255.0(ro,async)  

 

Centos8 重启网络service 命令

restart NetworkManager.service

Ubuntu16.04 重启网络service 命令 

service networking restart
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/187755
推荐阅读
相关标签
  

闽ICP备14008679号