服务器在尝试 scp 时给我一个权限错误,但在 ssh 时却没有

Server gives me a permission error when trying to scp, but not when ssh

当你想scp到服务器时,是否需要设置服务器配置?如果我 ssh 进入机器,我使用:

ssh -i file.pem user@server

但是当我尝试 scp 命令时,它会给我一个权限错误。

scp path/to/file.pdf -i file.pem user@server:/home/user/.

我做错了什么,我该如何解决?我似乎无法弄清楚它是怎么回事。错误显示为:
Permission denied (publickey). lost connection

你需要重新考虑你的选择和论点。应该是:

scp -i file.pem path/to/file.pdf user@server:/home/user

注意目标路径末尾省略了 /.。没有必要,因为如果目标的最后一段是目录,它会将文件复制到目录中,就像 cpmv.

Is there a way to assign the file.pem such that it just picks it up when i use: user

您可以使用 ~/.ssh/config 配置文件来做到这一点:

Host            serveralias
HostName        server.com
User            user
IdentityFile    /path/to/your/key.pem

创建后您可以执行以下操作:

# will effectively be like doing 
# ssh -i /path/to/your/key.pem user@server.com
ssh serveralias

# should pick up on the same settings and effectively like 
# ssh -i /path/to/your/key.pem user@server.com
ssh user@server.com

而且他们都应该使用您指定的 IdentityFile。