LoginSignup
0
0

More than 3 years have passed since last update.

ssh 实现服务器端文件同步

Last updated at Posted at 2020-02-18

主题

  • ubuntu服务器端添加其他成员的ssh访问
  • 简单实现本地目录文件和服务器目录同步

SSH用公开键和私密键生成

$ ssh-keygen -t rsa -b 4096 -C "your comments"

命令行执行后会提示,输入要保存的文件名
输入文件名回车后,会要求输入密码串,密码串可以为空(直接打回车)

密码串关联内容参照以下连接
https://qiita.com/shukan0728/items/e654953c89aab6f847a4

服务器的publickeys登陆

本地的公开键上传到服务器(需要已存在的管理员用户来操作)

$ scp your-private-key.pub user@host:~/.ssh/
$ cat your-private-key.pub >> authorized_keys
$ chmod 600 authorized_keys

本地ssh的config设定

默认路径一般在
/home/username/.ssh/config
config文件不存在的话可以新建一个
touch config

#对外公开直连方式
Host aws_proxy_server
  HostName xx.xx.global.ip
  User ubuntu
  Port 22
  IdentityFile /home/username/your-private-key

#通过对外公开ip连接内部其他服务器
Host proxy_aa
  HostName xx.xx.global.innerip
  User ubuntu
  Port 22
  IdentityFile /home/username/your-private-key
  ProxyCommand ssh aws_proxy_server -W %h:%p

改变本地私密键的权限许可

$ chmod 600 your-private-key

测试一下可否能连接上

ssh aws_proxy_server

连接成功后直接会显示服务器控制台

把本地文件同步更新到服务器

本地目录结构

hello_world/
-Makefile
-src/
-conf/
-test/
-tmp/
-.gitignore
-README.md
-requirements.txt

all:
    @echo "deploy"

#ssh config中定以的服务器名
HOST := aws_proxy_server


deploy:
    @read -p "HOST: $(HOST)? [y,N]:" ans; \
    if [ "$$ans" = y ]; then  \
    rsync -C --filter=":- .gitignore" -av --exclude "tmp/*" --delete -e ssh ./ $(HOST):/home/username/dev/hello_world; \
    fi

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0