用ssh登陆服务器帮忙跑程序,写给ERICUG(219A)的学习伙伴们。

Sometimes, 自己的笔记本在运行大程序时会很慢,这个时候就需要服务器帮忙运行,可以用ssh(ssh是什么?)远程登陆。基本操作是:在自己的笔记本上用ssh登陆服务器 -> 用scp拷贝数据到服务器 -> 服务器运行 -> scp/sftp 拷贝结果到自己的笔记本。

~~目前有一个前提是笔记本和服务器都要用同一个局域网,后续再说明不同局域网进行ssh穿透登陆的方法,到时在家也可以用办公室服务器啦!~~这儿分享的是在同一片局域网下ssh登陆的方法,不同局域网登陆请看NAT traverse by ssh。我的笔记本是ubuntu 16.04,以此为例;Mac、Windows、Xshell等其他系统请自行修改某些命令。

1. 安装ssh-server

ssh分客户端openssh-client和openssh-server; 推荐阅读 ubuntu开启ssh服务远程登陆

查看当前的ubuntu是否安装了ssh-server服务:

$ dpkg -l | grep ssh

ii  libssh-4:amd64        0.6.3-4.3          amd64 tiny C SSH     library (OpenSSL flavor)
ii  libssh-gcrypt-4:amd64 0.6.3-4.3          amd64 tiny C SSH library (gcrypt flavor)
ii  libssh2-1:amd64       1.5.0-2ubuntu0.1   amd64 SSH2 client-side library
ii  openssh-client        1:7.2p2-4ubuntu2.4 amd64 secure shell (SSH) client, for secure access to remote machines
ii  openssh-server        1:7.2p2-4ubuntu2.4 amd64 secure shell (SSH) server, for secure access from remote machines
ii  openssh-sftp-server   1:7.2p2-4ubuntu2.4 amd64 secure shell (SSH) sftp server module, for SFTP access from remote machines
ii  ssh-import-id         5.5-0ubuntu1       all   securely retrieve an SSH public key and install it locally

如果有openssh-clientopenssh-server两行,证明已经安装。没有安装则:

$ sudo apt-get install openssh-client

$ sudo apt-get install openssh-server

查看ssh-server是否启动:

$ ps -e | grep ssh

942 ?        00:00:00 sshd
7421 pts/1    00:00:00 ssh

证明已经启动。

2. 配置ssh

服务器端配置基本不用设置,已安装ssh-server。我们服务器的名称是u1, IP地址为192.168.5.10 192.168.5.2

对于自己笔记本,ssh-server配置文件位于/etc/ssh/sshd_config,在这里可以定义ssh的服务端口,默认端口(port)是22。

$ vi /etc/ssh/sshd_config

3. 登陆服务器

有两种登陆方式,一是用密码登陆,一是用密钥登陆。密钥登陆不需要每次用密码,方便一点。

3.1 密码登陆

常用格式:ssh [-l login_name] [-p port] [hostname@(ip address)]

$ ssh -l root -p 22 u1@192.168.5.2

输入服务器密码,这里 -l root-p 22 都是默认值,可以省略。

$ ssh u1@192.168.5.2

登陆到服务器之后,建议在服务器主目录下建立一个以自己名字命名的文件夹,以后所有的操作在自己的文件夹下运行; 或者在服务器建立属于自己的账户,比如我在服务器上建立了yq的非root 用户,登陆时将上述命令里的u1替换为yq即可,密码是yq用户的密码。

$ ssh yq@192.168.5.2

3.2 密钥登陆

使用下例中ssky-keygen和ssh-copy-id,通过3个步骤就能登陆远程Linux主机。

步骤1: 用 ssh-key-gen 在本地主机上创建公钥和密钥。

$ ssh-keygen -t rsa
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[不用理,敲回车键]
Enter passphrase (empty for no passphrase): [继续回车]
Enter same passphrase again: [继续回车]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is: xxx.xxx.xxx.xxx.xxx.xxx.xxx

步骤2: 用 ssh-copy-id 把公钥复制到远程主机上。

$ ssh-copy-id -i ~/.ssh/id_rsa.pub u1@192.168.5.2
u1@192.168.5.2's password:
Now try logging into the machine, with ―ssh ?remote-host‘‖, and check in:
.ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting.
[注: ssh-copy-id 把密钥追加到远程主机的 .ssh/authorized_key 上]

步骤3: 直接登陆远程主机。

$ ssh u1@192.168.5.2
Last login: Sun Oct 14 17:22:33 2018 from 192.168.5.2
[注: ssh 不会询问密码,你现在已经登陆到了远程主机上]

3.3 快捷登陆

上述两种方式登陆时都需要输入用户名和 IP,可以将 ssh u1@192.168.5.2 写入到系统命令中,实现快捷登陆:

$ echo "ssh u1192.168.5.2" > 907_server
$ sudo chmod +x 907_server
$ sudo mv 901_servr /usr/local/bin/
$ 907_server

4. scp 和 sftp 拷贝

scp是 secure copy 的简写。用于在 Linux 下进行远程拷贝文件的命令,和它类似的命令有 cp,不过 cp 只是在本机进行拷贝不能跨服务器,而且 scp 传输是加密的。当你服务器硬盘变为只读 read only system 时,用 scp 可以帮你把文件移出来。

$ scp --help 
unknown option -- h 
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] 
           [-l limit] [-o ssh_option] [-P port] [-S program] 
           [[user@]host1:]file1 ... [[user@]host2:]file2

lenon@yq:~$ scp 4.5_week.csv u1@192.168.5.2:/home/u1/YQ/ [注:在我的笔记本里的当前文件夹下,将文件拷贝到,服务器里的某文件夹中]

u1@cugeri:~$ scp lenovo@192.168.5.10:/home/lenovo/how* . [注:在服务器的当前文件夹下,将文件拷贝到,我的笔记本里的某文件夹中]

注意在使用 scp 时复制路径的规范。

另外 sftp 也可以拷贝,原理相同,优点在于可以通过 sftp 连接后,可同时访问本地和服务器:

# 连接远程服务器
$ sftp root@192.168.25.140

# 查看当前服务器路径
$ lpwd

# 查看远程服务器路径[默认用户家目录]
$ pwd

# 上传文件
$ put 当前路径  远程连接

# 下载文件
$ get 远程路径  当前路径

5. 实例

点击查看实图。[注:ip地址有变化,以下面的code为准]

lenovo@YQ:~$ ssh u1@192.168.5.2         [注:在我的系统lenovo@YQ下登陆服务器]
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-36-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

40 packages can be updated.
0 updates are security updates.

New release '18.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Mon Oct 15 09:49:37 2018 from 192.168.5.10 [注:我的ip:192.168.5.10]
u1@cugeri:~$ ls [注:已经在服务器了]
Desktop    Downloads         Music     Public     Videos
Documents  examples.desktop  Pictures  Templates  YQ
u1@cugeri:~$ cd YQ/
u1@cugeri:~/YQ$ ls
4.5_week.csv
u1@cugeri:~/YQ$ scp lenovo@192.168.5.10:/home/lenovo/how* . [注:从我的电脑里复制文件到服务器本地]
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.5.10' (ECDSA) to the list of known hosts.
lenovo@192.168.5.10's password: 
how_to_Zhuangbi                               100%   43     0.0KB/s   00:00
u1@cugeri:~/YQ$ ls
4.5_week.csv  how_to_Zhuangbi
u1@cugeri:~/YQ$ exit [注:退出登陆]
logout
Connection to 192.168.5.2 closed.
lenovo@YQ:~$ [注:已回到lenovo@YQ]

6. 后续问题

请查看下一博文

References: