MacOS Pycharm ssh 免密码部署代码

1. 本地机器设置

在本地机器上升成公钥、私钥。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa):
/Users/username/.ssh/id_rsa already exists.
Overwrite (y/n) y?

2. 公钥拷贝到远程机器 

$ ssh-copy-id -i ~/.ssh/id_rsa.pub -p 2222 username@localhost
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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
vagrant@localhost's password: 

Number of key(s) added:        1

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

使用  ssh -p 2222 username@localhost 登录远程机器成功。

3. Pycharm 远程部署

登录成功后,创建项目目录,例如 ~/Project/ccp/ccp。 在 ~/Project/ccp 目录下执行命令创建虚拟环境。激活虚拟环境后,出现 (venv)的提示前缀。

% mkdir -p Project/cpp/ccp
% cd Project/ccp
% python3 -m venv venv
% source venv/bin/activate
(venv) %

从 Pycharm 的本机工程中增加一个 Interpreter

接下来选择虚拟环境中的解释器以及配置本地与远程文件同步目录。 

 

保存成功后。在 Pycharm 的右下角选用新的解释器。

4. 配置远程工程环境

和项目运行环境相关的配置最好放在项目目录的上级目录。这样同步代码时不会把本地代码同步到远程。

5. 安装项目依赖包

(venv)% cd ccp
(venv)% pip install -r requirements.txt

 6.在 Pycharm 中将本地的 project 配置项复制一个,并将解释器换为远程解释器。

保存成功后,选择 ccp_remote 工程运行,消除其中的错误即完成远程项目配置了。这时 Pycharm 可以正常运行、调试,但代码是远程运行的结果。一些第三方包也是使用远程服务器上的。 

猜你喜欢

转载自blog.csdn.net/whyliu_/article/details/125481937