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 [email protected]:/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.
[email protected]'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 [email protected]
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 [email protected]
[root@localhost dg]# 
2.将生成的公钥(默认为~/.ssh/id_rsa.pub)添加到远程服务器上:

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

复制代码
[root@localhost dg]# ssh-copy-id [email protected]
/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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
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 [email protected]
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 [email protected]:/usr/my_server/
gameSrv.jar                                                                                                                                                                                         100% 5959KB  95.2MB/s   00:00    
[root@localhost dg]# 
[root@localhost dg]# 
相关推荐
Jooolin4 小时前
【Linux】虚拟机、服务器、双系统,谁才是 Ubuntu 的最佳方案?
linux·ubuntu·ai编程
K·Herbert9 小时前
最新CentOS 7 yum源失效的解决方案(2025年6月)
linux·运维·centos
别骂我h10 小时前
部署KVM虚拟化平台
linux·运维·服务器
繢鴻10 小时前
紧急救援!Ubuntu崩溃修复大赛
linux·服务器·ubuntu
showmethetime10 小时前
优化nginx参数(基本通用参数)
运维·nginx
老六ip加速器11 小时前
获取ip地址安全吗?如何获取静态ip地址隔离ip
运维·网络·智能路由器
净心净意19 小时前
浅谈DaemonSet
运维·jenkins
Apex Predator1 天前
jenkins流水线打包vue无权限
运维·jenkins
hujun86101 天前
Ubuntu安装无线网卡
linux
Johny_Zhao1 天前
CentOS Stream 8 高可用 Kuboard 部署方案
linux·网络·python·网络安全·docker·信息安全·kubernetes·云计算·shell·yum源·系统运维·kuboard