1. 远程登录命令 ssh
- ssh user@remote_host (-p port): 以user用户登录到远程主机remote_host上,加-p表示指定端口登录
➜ linux_commands ssh ***@10.20.34.27
=============================警告===============================
| |
| 这是跳板机,请确认你已知晓已下信息: |
| |
- ssh -i [key] user@remote_host:以指定私钥登录到远程主机
- ssh user@remote_host [command]: 登录到远程主机执行命令[command]
➜ linux_commands ssh ***@10.20.34.27 ls
a.out
adjust_user_quota
auto_renew.csv
auto_renew_clean
2. 安全文件传输 sftp
- sftp (-P port) user@remote_host: 连接到远程服务器并进入交互模式, 加-P表示可选端口
➜ ~ sftp ***@10.20.34.27
Connected to ***@10.20.34.27.
sftp>
- ls: 列出远程服务器上到文件列表
- lls: 列出本地系统的文件列表
- get [remote_file]: 在交互模式下传输远程文件到本地系统
sftp> ls template*
template.json
sftp> lls template*
zsh:1: no matches found: template*
Shell exited with status 1
sftp> get template.json
Fetching /home/qiuxiayu/template.json to template.json
/home/qiuxiayu/template.json 100% 27KB 174.7KB/s 00:00
sftp> lls template*
template.json
- put [remote_file]: 在交互模式下传输本地文件到远程系统
sftp> lls channel*
channel.jpg
sftp> ls channnel*
Can't ls: "/home/qiuxiayu/channnel*" not found
sftp> put channel.jpg
Uploading channel.jpg to /home/qiuxiayu/channel.jpg
channel.jpg 100% 208KB 550.2KB/s 00:00
sftp> ls channel*
channel.jpg
- get -R [dir]: 传输远程目录到本地系统,-R参数对put命令也适用, 但要提前创建接收系统的目录
sftp> put -R tt/
Uploading tt/ to /home/qiuxiayu/tt
Couldn't canonicalize: No such file or directory
sftp> mkdir tt
sftp> put -R tt
Uploading tt/ to /home/qiuxiayu/tt
Entering tt/
tt/test1 100% 2 0.0KB/s 00:00
rf: not a regular file
Entering tt/ss
Entering tt/ss/tt
Entering tt/ss/tt/ss
Entering tt/ss/tt/ss/tt
test3: not a regular file
tt/test2 100% 2 0.0KB/s 00:00
3. 远程拷贝命令scp
- scp [local_file] remote_host:[remote_file]: 将本地文件[local_file]拷贝成远程主机的[remote_file], 如果远程文件[remote_file]已存在,则会直接替换。也可以从远端拷贝文件到本地,即scp remote_host:[remote_file] [local_file]
➜ ~ scp bill.txt qiuxiayu@10.20.34.27:~/bill.txt
bill.txt 100% 14MB 11.2MB/s 00:01
qiuxiayu@new-jumpbox:~$ ls -l bill.txt
-rw-r--r-- 1 qiuxiayu qiuxiayu 14192123 Aug 7 12:20 bill.txt
- scp [local_file] remote_host:[remote_dir]: 将本地文件[local_file]拷贝到远程主机的[remote_dir]目录下
- scp -r [local_dir] remote_host:[remote_dir]: 将本地目录拷贝到远程主机的[remote_dir]下
➜ ~ ls -l tt
total 16
lrw-r--r-- 1 qiushye staff 25 Mar 22 20:41 rf -> /Users/qiushye/refund.csv
drwxr-xr-x 3 qiushye staff 96 Mar 22 20:41 ss
-rw-r--r-- 1 qiushye staff 2 Mar 22 20:41 test1
-rw-r--r-- 1 qiushye staff 2 Mar 22 20:41 test2
lrw-r--r-- 1 qiushye staff 5 Mar 22 20:41 test3 -> test1
➜ ~ scp -r tt qiuxiayu@10.20.34.27:~/
test1 100% 2 0.0KB/s 00:00
tt/rf: No such file or directory (查找不到原文件refund.csv导致rf链接拷贝失败)
test3 100% 2 0.0KB/s 00:00
test2 100% 2 0.0KB/s 00:00
qiuxiayu@new-jumpbox:~$ ls -l tt
total 16
drwxr-xr-x 3 qiuxiayu qiuxiayu 4096 Aug 6 12:28 ss
-rw-r--r-- 1 qiuxiayu qiuxiayu 2 Aug 7 12:24 test1
-rw-r--r-- 1 qiuxiayu qiuxiayu 2 Aug 7 12:24 test2
-rw-r--r-- 1 qiuxiayu qiuxiayu 2 Aug 7 12:24 test3
目录拷贝无法覆盖,只能在目标目录下创建同名目录,具体如下:
➜ ~ scp -r tt qiuxiayu@10.20.34.27:~/tt
test1 100% 2 0.0KB/s 00:00
tt/rf: No such file or directory
test3 100% 2 0.0KB/s 00:00
test2 100% 2 0.0KB/s 00:00
qiuxiayu@new-jumpbox:~$ ls tt/
ss test1 test2 test3 tt
- scp -3 remote_host1:[remote_file1] remote_host2:[remote_file2]: 将远程主机remote_host1的[remote_file1]文件经过本地传输到远程主机remote_host2的[remote_file2]
qiuxiayu@new-jumpbox:~$ ls bill*
bill.txt
➜ ~ scp -3 qiuxiayu@10.20.34.27:~/bill.txt qiuxiayu@10.20.34.27:~/bill1.txt
qiuxiayu@new-jumpbox:~$ ls bill*
bill.txt bill1.txt