当前位置: 代码网 > it编程>数据库>Mysql > Scp如何使用密码拷贝文件

Scp如何使用密码拷贝文件

2025年02月13日 Mysql 我要评论
scp使用密码拷贝文件在linux系统中,scp是一个非常好用的命令,可以用于在本地和远程计算机之间复制文件和目录。使用scp命令复制文件时,你可以使用两种方法来提供密码:直接拷贝(手动输入密码)下面

scp使用密码拷贝文件

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

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

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

下面是使用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:

使用密钥拷贝(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]# 

5.做一个脚本复制

ips=("192.168.99.3" "192.168.99.1" "192.168.99.4")
ports=("55898" "33488" "31880")

for i in "${!ips[@]}"; do
    scp -p ${ports[$i]} -r server root@${ips[$i]}:/usr/my_server/javaserver/dg
    if [ $? -eq 0 ]; then
        echo "server upload to ${ips[$i]} successful!"
    else
        echo "server upload to ${ips[$i]} failed!"
    fi

    scp -p ${ports[$i]} gamesrv.jar root@${ips[$i]}:/usr/my_server/javaserver/dg
    if [ $? -eq 0 ]; then
        echo "gamesrv upload to ${ips[$i]} successful!"
    else
        echo "gamesrv upload to ${ips[$i]} failed!"
    fi

done

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com