📌

UbuntuTips7:Ununtu两台服务器传文件

1. 安装SSH

  • 互传文件的每台机器都需要安装

1.1 安装

# 更新 sudo apt-get update # 安装 sudo apt-get install openssh-client sudo apt-get install openssh-server # 修改文件 vi /etc/ssh/sshd_config

1.2. 添加信息

# 添加信息 PermitRootLogin yes
notion image

1.3 重启SSH服务

sudo service ssh restart

1.4 查看SSH

  • ssh-agent表示ssh-client启动
  • sshd表示ssh-server启动了
ps -ef |grep ssh

1.5 测试

  • docker01中连接docker02
root@docker01:~# ssh docker02 root@docker02's password: Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.11.0-40-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 197 updates can be installed immediately. 0 of these updates are security updates. To see these additional updates run: apt list --upgradable Your Hardware Enablement Stack (HWE) is supported until April 2025. *** System restart required *** The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. root@docker02:~#

2. SCP

  • rsync主要用于备份和镜像。具有速度快、避免复制相同内容和支持符号链接的优点。
  • rsync做文件的复制要比scp的速度快,rsync只对差异文件做更新。scp是把所有文件都复制过去。
  • 补充一点,需要自己创建对应文件夹
  1. 在/usr/local/bin 目录下创建 xsync 文件
root@docker:/home# cd /usr/local/bin/ root@docker:/usr/local/bin# touch xsync root@docker:/usr/local/bin# ll total 12448 drwxr-xr-x 2 root root 4096 11月 20 11:03 ./ drwxr-xr-x 10 root root 4096 2月 10 2021 ../ -rwxr-xr-x 1 root root 12737304 11月 18 20:40 docker-compose* -rw-r--r-- 1 root root 0 11月 20 11:03 xsync root@docker:/usr/local/bin#
 
  1. 在xsync文件下编写
  • Note:这里的slave对应自己主机名,需要做相应修改。
#!/bin/bash # $#:表示传递给脚本或函数的参数个数。 #1 获取输入参数个数,如果没有参数,直接退出 pcount=$# if((pcount==0)); then echo no args; exit; fi #2 获取文件名称 p1=$1 fname=`basename $p1` echo fname=$fname #3 获取上级目录到绝对路径 pdir=`cd -P $(dirname $p1); pwd` echo pdir=$pdir #4 获取当前用户名称 user=`whoami` #5 循环 for host in {docker01,docker02,docker03,docker04}; do #echo $pdir/$fname $user@$host:$pdir echo --------------- $host ---------------- rsync -rvl $pdir/$fname $user@$host:$pdir done
  1. 修改脚本xsync具有执行权限
root@docker01:/usr/local/bin# chmod 777 xsync
  1. 调用脚本
#xsync 文件名 root@docker01:/home# ls docker01 test.txt root@docker01:/home# xsync test.txt
  1. 效果如图
notion image