ssh、scp、ssh 免密码登陆

1,685 阅读3分钟
原文链接: github.com

ssh 远程登录

ssh 命令。如果端口号是 22,则不需要指定

ssh -p 端口号 用户名@远程主机 

ssh -p 51122 centosone@localhost

在 .zshrc 中添加别名来简化命令

alias ubuntuone="ssh ubuntuone@localhost -p 61122"
alias ubuntutwo="ssh ubuntutwo@localhost -p 62222"
alias ubuntuthree="ssh ubuntuthree@localhost -p 63322"
alias centosone="ssh centosone@localhost -p 51122"
alias centostwo="ssh centostwo@localhost -p 52222"
alias centosthree="ssh centosthree@localhost -p 53322"

scp 远程上传下载文件

1.从服务器上的下载 Hello.txt 文件到本地 test 目录,默认端口是 22,如果默认端口不是 22 的话需要用大写的 -P 来指定端口

➜  ~ pwd
/home/centosone
➜  ~ ls
Hello.txt
➜  ~ exit
Connection to localhost closed.
➜  Docker scp -P 51122 centosone@localhost:/home/centosone/Hello.txt /Users/bingoogol/Desktop/Docker/test/
centosone@localhost's password:
Hello.txt                                      100%    6     0.0KB/s   00:00
➜  Docker ls test
Hello.txt

2.上传本地的 Hello.txt 文件到服务器上,默认端口是 22,如果默认端口不是 22 的话需要用大写的 -P 来指定端口

➜  Docker scp -P 51122 ./test/Hello.txt centosone@localhost:/home/centosone/
centosone@localhost's password:
Hello.txt                                      100%   12     0.0KB/s   00:00
➜  Docker ssh -p 51122 centosone@localhost
centosone@localhost's password:
Last login: Tue Jan 17 11:32:03 2017 from 192.168.56.101
➜  ~ pwd
/home/centosone
➜  ~ ls
Hello.txt

3.从服务器下载整个 test(包含test目录本身) 目录到本地的 download 目录中,和下载单文件不同的是需要加上 -r 参数,默认端口是 22,如果默认端口不是 22 的话需要用大写的 -P 来指定端口

➜  ~ pwd
/home/centosone
➜  ~ ls
test
➜  ~ ls test
Hello.txt  test2  World.txt
➜  ~ ls test/test2
Hello.txt
➜  ~ exit
Connection to localhost closed.
➜  Docker scp -r -P 51122 centosone@localhost:/home/centosone/test /Users/bingoogol/Desktop/Docker/download
centosone@localhost's password:
Hello.txt                                      100%   12     0.0KB/s   00:00
World.txt                                      100%    6     0.0KB/s   00:00
Hello.txt                                      100%   12     0.0KB/s   00:00
➜  Docker ls download
test
➜  Docker ls download/test
Hello.txt World.txt test2
➜  Docker ls download/test/test2
Hello.txt

4.上传本地 test(包含test目录本身) 目录到服务器的 /home/centosone 目录中,和上传单文件不同的是需要加上 -r 参数,默认端口是 22,如果默认端口不是 22 的话需要用大写的 -P 来指定端口

➜  Docker ls
CentOS-7-x86_64-Minimal-1611.iso test                             ubuntu-16.04-server-amd64.iso
Docker镜像和容器.md              ubuntu-16.04-desktop-amd64.iso
➜  Docker scp -r -P 51122 /Users/bingoogol/Desktop/Docker/test centosone@localhost:/home/centosone
centosone@localhost's password:
Hello.txt                                                                                             100%   12     0.0KB/s   00:00
Hello.txt                                                                                             100%   12     0.0KB/s   00:00
World.txt                                                                                             100%    6     0.0KB/s   00:00
➜  Docker ssh -p 51122 centosone@localhost
centosone@localhost's password:
Last login: Tue Jan 17 12:58:32 2017 from gateway
➜  ~ ls
test
➜  ~ ls test
Hello.txt  test2  World.txt
➜  ~ ls test/test2
Hello.txt

上传 SSH 公钥上传到 Linux 服务器实现面密码登陆

1.生成 SSH 密钥和公钥

ssh-keygen -b 4096 -t rsa

2.将公钥内容添加到服务器的 ~/.ssh/authorized_keys 文件中。

a.我这里是直接通过 scp 拷贝的(确保服务器上已经有 .ssh 目录了,没有的话需要先登陆上去创建,CentOS 需将该目录权限设置为 700)

scp -P 63322 ~/.ssh/id_rsa.pub ubuntuthree@localhost:~/.ssh/authorized_keys

b.如果服务器上之前已经有 authorized_keys 文件了

开发机
scp -P 63322 ~/.ssh/id_rsa.pub ubuntuthree@localhost:~/.ssh/new_authorized_keys

登陆到服务器上
cat ~/.ssh/new_authorized_keys >> ~/.ssh/authorized_keys

c.如果开发机是 Linux,可以直接使用 ssh-copy-id 命令来完成公钥的上传

ssh-copy-id -i ~/.ssh/id_rsa.pub centosone@192.168.56.221

3.如果是 CentOS 的话,还需要修改 .ssh 目录权限为 700

chmod 700 ~/.ssh

4.如果是 CentOS 的话,可能需要重启 SSH 服务

systemctl restart sshd.service