Logging in and Transferring Files via SSH

TODO

  1. Read the whole text carefully.
  2. Try the commands of Linux mentioned.
  3. Download and install some necessary tools.
  4. Try operations of logging and file transferring mentioned by yourself.
  5. Answer all the questions given in the quoting part.

Since there is one terminal with small size in the VM, it is not very convenient to use. Therefore, we recommend you to log in the VM via SSH.

For Linux Users

You can do experiments directly under the environment of physically installed Linux, not need to use SSH to connect the virtual machine.

For Mac Users

You will use the build-in ssh tool, and do not need to install an extra one. Open a terminal, run

ssh -pXXXX username@127.0.0.1

where username is your VM user name, XXXX is the Host Port number you have set in the Port Forwarding . For example:

ssh -p22 jack@127.0.0.1

If you are prompted with

Are you sure you want to continue connecting (yes/no)?

enter "yes". Then enter your VM user password. If everything is fine, you will access the VM via SSH successfully. To exit SSH, just type

exit

in terminal.

For Windows Users

Windows has no build-in ssh tool, and you have to download one manually. Download the latest release version of putty.exe hereopen in new window. Run putty.exe, and you will see a dialog is invoked. In the input box labeled with Host Name (or IP address), enter the IP address mentioned above. Leave other settings default, then click Open button. Enter your VM user name and password. If everything is fine, you will access the VM via SSH successfully. To exit SSH, just type

exit

in terminal.

Transferring Files between Hosts

Although VM is running on the host, they are isolated logically. This means the host can not directly access files in the VM, and vice versa. Therefore, a way is needed to transfer files from/to the VM. In most circumstances, you need to export the codes you wrote in the virtual machine or transfer some necessary files into the VM. This section will tell you how to do that.

For Linux Users

You can do experiments directly under the environment of physically installed Linux, not need to use SSH to transfer files to/from the virtual machine.

For Mac Users

Thanks to Unix Philosophy, you can use one command to achieve the transferring.

To transfer files from host to VM, issue the following command in the host:

tar cj file | ssh username@127.0.0.1 'tar xvjf -'

where file is the file to transfer in the host, username is the same as mentioned in the Logging in via SSH section. You will be asked to enter your VM user password. If everything is fine, the file will be transferred to the home directory of the VM.

To transfer files from VM to host, issue the following command in the host:

ssh username@127.0.0.1 'tar cj file' | tar xvjf -

where file is the file to transfer in the VM, username is the same as mentioned in the Host Configuration section above. You will be asked to enter your VM user password. If everything is fine, the file will be transferred to the home directory of the host.

For Windows Users

You can either use the latest release version of psftp.exe hereopen in new window which is just a command line program in Windows. Or we recommend you to use other tools like FileZillaopen in new window to establish the SSH file transfer protocol.

PSFTP

Run psftp.exe, and connect to the VM by the following command:

open ip_addr

where ip_addr is the same as mentioned in the Host Configuration section. Then enter your VM user name as well as the password, as if logging in via ssh. We list some useful commands in psftp.exe:

cd     change your remote working directory
exit   finish your SFTP session
get    download a file from the server to your local machine
help   give help
lcd    change local working directory
lpwd   print local working directory
ls     list remote files
put    upload a file from your local machine to the server
pwd    print your remote working directory

Under our situation, "local machine" stands for the host, "server" and "remote" stand for the VM. For more details about the command, refer to help COMMAND.

FileZilla
  1. Run FileZilla Client.
  2. Type the IP address at the Host entry like: sftp://ip_addr, note that sftp:// is needed.
  3. Type your username and password in the following textboxes and click Quickconnect.
  4. A box of Unknown host key will appear and you should check Always trust this host, add this key to the cache to prevent it from appearing again next time.
  5. If everything goes correctly, the connection will be established and you can see all the files in the VM.
  6. Enjoy transferring files between VM and host!

{% panel style="success", title="Have a try!" %}

  1. New a text file with casual contents in the host.
  2. Transfer the text file to the VM.
  3. Modify the content of the text file in the VM.
  4. Transfer the modified file back to the host.

Check whether the content of the modified file you get after the last step is expected. If it is the case, you are done!

{% endpanel %}

{% panel style="danger", title="不要把整个项目在虚拟机和宿主机之间来回传" %}

下一阶段我们将会获得整个项目的代码。请注意,在以后的任务中,不要在宿主机修改好了代码之后,把整个项目传到虚拟机中覆盖原项目,这会造成许多不可挽回的问题。请大家最好使用vim直接在虚拟机中编辑,答辩时vim使用的熟悉程度也占一定的分数。不用担心直接在虚拟机改会改错,因为改错了你可以通过git还原(之后会讲)。如果你实在不想用vim,你可以在本地编辑好之后,将修改的单个文件传到虚拟机中进行覆盖。

{% endpanel %}

Configuring X Server

We need graphical display after the middle stage of our course so you are supposed to configure it properly.

配置

你需要根据主机操作系统的类型, 你需要下载不同的 X Server:

  • Windows 用户. 点击这里open in new window下载, 安装并打开 Xming.
  • Mac 用户. 点击这里open in new window进入 XQuartz 工程网站, 下载, 安装并打开 XQuartz.
  • GNU/Linux 用户. 系统中已经自带 X Server, 你不需要额外下载.

在虚拟机中安装 xclock 这个 X 协议测试软件图形时钟:

sudo apt install x11-apps

然后根据主机操作系统的类型, 为 SSH 打开 X11 转发功能:

  • Mac用户:在运行 ssh 命令时加入 -X 选项即可:

    ssh -X -p 20022 username@ip_addr
    
  • Windows用户:在使用 PuTTY 登陆时, 在 PuTTY Configuration 窗口左侧的目录中选择 Connection -> SSH -> X11, 在右侧勾选 Enable X11 forwarding, 然后登陆即可.

  • GNU/Linux用户:安装有图形界面的 Linux 机器应该可以直接打开图形显示

通过带有X11转发功能的SSH登陆后,执行命令

xclock

打开图形时钟,测试是否能够正常打开显示。我们在PA进行到中期时也需要进行图像的输出, 因此你务必完成X Server的配置.

Exporting Your VM as Backup

You are supposed to make backups of your VM regularly to prevent potential crashes. It is easy to use the function provided by VirtualBox. What you should do is:

  1. Run Oracle VM VirtualBox Manager.
  2. Click File in menu bar and click Export Appliance....
  3. Virtual machines to export. Select the VM you want to export in the list and click Next.
  4. Storage settings. Select a save path to save the archive of VM, leaving other settings default and click Next.
  5. Appliance Settings. Fill in the blanks if you want, or just leaving it default is okay.
  6. Wait until the operation finished.

Operations above will pack up everything of your VM as a single file which includes settings and compressed virtual hard disks. When your VM is encountered with any fatal error that makes your VM unable to use, you can import these backups at any time.

养成经常备份虚拟机的好习惯

注意经常使用上述导出虚拟机功能对虚拟机进行导出操作,以防因一时不慎导致虚拟机崩溃无法正常开机或者系统出现问题丢失大量宝贵的代码和难得配置好的实验环境。导出的虚拟机存放在硬盘上会消耗一些硬盘空间,所以经常保存一个最新的虚拟机副本即可。一旦出现问题,可以及时删除出问题的虚拟机并导入副本。导出操作是直接把虚拟机设置和硬盘打包存储,重新导入后几乎所有配置和虚拟硬盘上的文件都和导出时完全相同,你只需要重写上次导出以来部分的代码即可(疯狂暗示要经常导出最新版本)


That's everything in PA0.6.

Last Updated 2023-04-02 14:55:00