配置ssh密钥对认证及scp、ftp等文件传输 目录ssh密钥配对认证文件传输linux主机之间telnet 192.168.1.120 ssh #测试对方的ssh 服务是否开启#按Ctrl ] 会呼出telnet的命令行ssh密钥配对认证openssh 连接[rootlocalhost ~]# ping ton.tom -c2 //没ping通主机名在/etc/hosts下添加 或者直接ping对方ip PING ton.tom (192.168.1.111) 56(84) bytes of data. 64 bytes from ton.tom (192.168.1.111): icmp_seq1 ttl64 time1.03 ms 64 bytes from ton.tom (192.168.1.111): icmp_seq2 ttl64 time0.883 ms --- ton.tom ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 3ms rtt min/avg/max/mdev 0.883/0.954/1.025/0.071 ms [rootlocalhost ~]# [rootlocalhost .ssh]# ssh ton.tom The authenticity of host ton.tom (192.168.1.111) cant be established. RSA key fingerprint is SHA256:7oVlQO5rj8RWD0uXREAYX1Z/YSgSpKg0YMUywjTnHrA. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ton.tom (RSA) to the list of known hosts. rootton.toms password: Last login: Sun Jul 26 04:23:39 2020 from 192.168.1.120 [rootton ~]# [rootton ~]# ls anaconda-ks.cfg file_creat.c linux-2.6.28.10.tar.gz [rootton ~]# exit logout Connection to ton.tom closed. [rootlocalhost ~]#添加key认证免密码登陆[serverlocalhost ~]$ ssh-keygen #生成密钥对 Generating public/private rsa key pair. Enter file in which to save the key (/home/server/.ssh/id_rsa): Created directory /home/server/.ssh. Enter passphrase (empty for no passphrase): #直接回车不添加 Enter same passphrase again: Your identification has been saved in /home/server/.ssh/id_rsa. Your public key has been saved in /home/server/.ssh/id_rsa.pub. The key fingerprint is: SHA256:CwLPbgDUDAliuAiFe69bEWJN2raeyw5Qc1GaUrOcj0 serverlocalhost The keys randomart image is: ---[RSA 2048]---- |** .. | |B. O | |oO O . | |.X o . | |....* o S | | . .B . . | | o.O E . | | .B . . | | .o | ----[SHA256]----- [serverlocalhost ~]$ ssh-copy-id yonghu192.168.1.111 #把公钥文件上传到对应用户 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: /home/server/.ssh/id_rsa.pub The authenticity of host 192.168.1.111 (192.168.1.111) cant be established. RSA key fingerprint is SHA256:7oVlQO5rj8RWD0uXREAYX1Z/YSgSpKg0YMUywjTnHrA. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /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 yonghu192.168.1.111s password: Number of key(s) added: 1 Now try logging into the machine, with: ssh yonghu192.168.1.111 and check to make sure that only the key(s) you wanted were added. [serverlocalhost ~]$ ssh yonghu192.168.1.111 #这时就可以不用输入密码直接登入了 [yonghuton ~]$ ls涉及到的命令ssh-keygen -t rsa #生成密钥对 ssh-keygen -t dsa # -t type #密钥类型 ssh-copy-id -i ~/.ssh/id_rsa.pub rootton.tom #-i identity_file 将本机的公钥复制到远程用户地址上/etc/ssh/sshd_config需对应配置文件Port 22 Protocol 2 PermitRootLogin yes #允许root可以ssh登陆 PubkeyAuthentication yes #设置密钥对的访问方式 PasswordAuthentication yes AuthorizedKeysFile .ssh/authorized_keystips:使用 ssh 加命令 的方式比较安全同时为了连接方便可以使用aliasalias tonssh rootton.tom hostname后面加多一个空格以免后面命令连接上来同时可将alias写入~/.bashrc中执行时直接ton mkdir /tmp/test[rootlocalhost ~]# ssh root192.168.1.111 ls /home root192.168.1.111s password: bincopy.sh initrd.img iso pub tom vmlinuz yonghu [rootlocalhost ~]#SecureCRT公钥连接主机即crt与主机间的免密认证登陆tools - create public key - 选择加密算法rsa 并且加密长度设置最长保存成id_rsa生成全局id_rsa.pub选择生成OpenSSH 密钥格式拷贝其中公钥id_rsa.pub的内容到服务器上的.ssh文件夹下的authorized_keys文件中authorized_keys 可直接手动创建secureCRT生成公钥#手动创建需如下 -rw------- 1 tom tom 381 Jul 26 07:28 authorized_keys ----注意属组与权限600 drwx------ 2 tom tom 4096 Jul 26 07:28 .ssh ----目录属组与权限700文件传输使用scp命令直接上传文件到ssh用户端[serverlocalhost ~]$ scp tom.txt yonghu192.168.1.111:/home/yonghu #上传[serverlocalhost ~]$ scp yonghu192.168.1.111:/tmp/3.txt ./ #下载到本地当前目录下-p 保留文件的时间和权限 preserve-r 递归 recursive-C 压缩传输时的数据流scp -r root192.168.31.125:/root/ldd3/ .这样会将远程的ldd3目录整个复制到当前目录下保持目录结构同步rsyncrsync -av root192.168.1.111:/var/log /server/ 将服务器上的整个目录同步到当前 ./server目录下scp拷贝比较生硬没有分析文件不同rsync则不同rsync会比scp更好传输文件他会发送文件的时间大小和md5来判断文件是否需要重传sftp[serverlocalhost ~]$ sftp yonghu192.168.1.111 Connected to yonghu192.168.1.111. sftp put tom.txt #上传 Uploading tom.txt to /home/yonghu/tom.txt tom.txt 100% 0 0.0KB/s 00:00 sftp get /tmp/3.txt #从服务器端下载 Fetching /tmp/3.txt to 3.txt sftp exit如果遇到ftp connectionrefused可以 service vsftpd restartftp遇到的错误ftp: root192.168.3.139: Temporary failure in name resolution #网关配置问题 ftp: root192.168.3.139: Name or service not known sudo iptables -F #可关闭iptableslftp - Sophisticated file transfer programmget *.iso 下载多个文件lcd 切换本地下载目录get [OPTS] rfile [-o lfile][rootlocalhost ~]# lftp -u root,123456 192.168.1.11 / user:passwdlftp root192.168.1.111:~ pwd 打印出一个ftp链接ftp://root:123456192.168.1.111/%2Froot可使用root去ftpvsftpd/ user_listvsftpd/ ftpusersnfsNFS /etc/export配置/var/ftp/pub 192.168.1.0/255.255.255.0(ro,async)Centos8 重启网络service 命令restart NetworkManager.serviceUbuntu16.04重启网络service 命令service networking restartopensuse高版本连接老系统问题Openssh版本 7.0已弃用DSA 密钥并且默认不使用它们而是使用RSA 密钥作为默认算法。1、 ssh -v -oHostKeyAlgorithmsssh-rsa usernameipaddress 2、 .ssh文件夹中创建一个名为config的文件然后在配置文件中添加此密钥 Host [ipaddress] HostKeyAlgorithmsssh-rsa 3、 SFTPsudo sftp -oHostKeyAlgorithmsssh-rsa usernameipaddress...sftp连接报错Ensure the remote shell produces no output for non-interactive sessions.