Scp 使用密码拷贝文件

在Linux系统中,scp是一个非常好用的命令,可以用于在本地和远程计算机之间复制文件和目录

使用scp命令复制文件时,你可以使用两种方法来提供密码:

 1.直接拷贝(手动输入密码)

下面是使用scp命令复制文件并手动输入密码的方法:

scp username@remote_host:/path/to/remote/file /path/to/local/directory

在上面的命令中,username是远程服务器的用户名,remote_host是远程服务器的IP地址或域名,/path/to/remote/file是远程服务器上的文件路径,/path/to/local/directory是本地服务器上的目标目录。

当你运行这个命令时,系统会提示你输入密码。输入密码后,文件就会被复制到本地服务器上。

[root@localhost dg]# scp gameSrv.jar root@192.168.2.67:/usr/my_server/
The authenticity of host '192.168.2.67 (192.168.2.67)' can't be established.
ECDSA key fingerprint is SHA256:8KXTdTgAUO6ESWUkYXLMfEVZQH8uSn3jb4DnuH651bY.
ECDSA key fingerprint is MD5:7f:1f:0e:9e:c3:ed:8d:cd:1b:a0:e9:f7:89:1d:e1:4b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.67' (ECDSA) to the list of known hosts.
root@192.168.2.67's password:

2.使用密钥拷贝(ssh密钥对)

使用ssh-keygen生成ssh密钥对,然后将公钥添加到远程服务器的~/.ssh/authorized_keys文件中,这样在复制文件时就不需要输入密码了。

下面是使用ssh-keygen生成ssh密钥对并将公钥添加到远程服务器的方法:

1.在本地服务器上生成ssh密钥对:

这台机器的ip是:192.168.2.66(机器1)  (需要在你自己的机器1上执行)

直接下一步 下一步就好。

[root@localhost dg]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:nXsv3egTQHkKpzbBiQ99nkegxmO38WKNyozj48TsptQ root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|         + ..o   |
|        o.*.= o  |
|         o*Oo=   |
|         +=+=*.  |
|        S.o.=oo  |
|       + + + ..  |
|      . E = .. + |
|     . +o. ...+ .|
|      .++.   oo. |
+----[SHA256]-----+
[root@localhost dg]# 
[root@localhost dg]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2gLD9dsTTaRImIC4mhWxYy2ESe3f/kj+xpBA5ixOJDgN3lUIFbHBZKY0f2ESHV1cZj/DXG2qkJD5dd4dPPJEPsycFmtLI1Y1OKvEIlMK3pSpTNkUZ5aGPxRJi1vwEzqcglZXPmzT5w6s42laSY4o49dSRYBDCxt7Aq6t3IQU+MvxmEpZ+IThsYngBY6vXnhkjNAZ5JGdax1fgpYpvTsFRbFPKEqTvqSu2lpKGCXRclPyF6TkXqAFj1PxgnGZ8gFJ/1z5dk38K0KtQzCdRo8Fx+i1OQ6IDuBMMDGg/TIaNL7Zsqw/KJFs6rrrWVy0QUKE5zpb/HeKxlBorbQOdHXFh root@localhost.localdomain
[root@localhost dg]# 
2.将生成的公钥(默认为~/.ssh/id_rsa.pub)添加到远程服务器上:

需要换成自己的用户名和ip(机器2),会提示输入192.168.2.67(机器2)的登录密码

[root@localhost dg]# ssh-copy-id root@192.168.2.67
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
root@192.168.2.67's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.2.67'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost dg]# 

  3.测试一下连接看是否需要输入密码
[root@localhost dg]# 
[root@localhost dg]# ssh root@192.168.2.67
Last login: Tue Aug 13 16:59:28 2024 from 192.168.2.35
[root@localhost ~]# 
[root@localhost ~]#
4.拷贝一个文件试试
[root@localhost dg]# 
[root@localhost dg]# scp gameSrv.jar root@192.168.2.67:/usr/my_server/
gameSrv.jar                                                                                                                                                                                         100% 5959KB  95.2MB/s   00:00    
[root@localhost dg]# 
[root@localhost dg]# 

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值