Ubuntu下使用SSH 命令用于登录远程桌面
问题描述
工作经常需要在一台电脑(本地主机)上写代码,另一台电脑(服务器,计算力强)上进行训练,两台电脑上都安装的是Ubuntu18.04,为了在local主机下可以随时跑程序,调代码,同时省流量而且迅速(不考虑这些的话用teamviewer也行),所以在两台电脑主机上进行SSH配置。
基础:ssh命令连接
step1:SSH程序的安装
确保在服务器上安装好了openssh-server程序,在本地主机上安装好了openssh-client程序。
sudo apt install openssh-client #本地主机运行此条,实际上通常是默认安装client端程序的
sudo apt install openssh-server #服务器运行此条命令安装
当然有兴趣也可以尝试两台电脑服务器客户端都安装,互相访问。
step2:服务器启动ssh服务
以下命令都只针对服务器端(server only)。
一般服务器上安装ssh完成后,会自动启动ssh服务,并且默认随系统启动,如果没有,请手动启动:
sudo /etc/init.d/ssh start #服务器启动ssh-server服务,
其他命令:
sudo /etc/init.d/ssh stop #server停止ssh服务
sudo /etc/init.d/ssh restart #server重启ssh服务
step3:查询服务器的ip地址
在服务器终端运行以下命令:
ifconfig #查询ip地址,在返回信息中找到自己的ip地址
比如我的服务器IP: 192.168.0.101
step4:在本地主机端ssh远程登录服务器
这一步需要知道服务器的用户名(我的服务器名字是yj)及IP地址。
在本地主机上运行以下命令:
用户端连接服务器用于登录远程桌面(以下user时远程主机的用户名)
uu@uu-HP-EliteBook-6930P-Notebook-PC:~$ ssh yj@192.168.0.101
uu@uu-HP-EliteBook-6930P-Notebook-PC:~$ ssh -l yj 192.168.0.101
如果需要调用图形界面的话,用-X:
uu@uu-HP-EliteBook-6930P-Notebook-PC:~$ ssh -X yj@192.168.0.101
初次登录时会出现以下信息,请记住要输入的密码是服务器主机本身的登陆密码:
uu@uu-HP-EliteBook-6930P-Notebook-PC:~$ ssh -X yj@192.168.0.101
yj@192.168.0.101's password:
Welcome to Ubuntu 18.04 LTS (GNU/Linux 4.15.0-29-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
* Canonical Livepatch is available for installation.
- Reduce system reboots and improve kernel security. Activate at:
https://ubuntu.com/livepatch
183 packages can be updated.
10 updates are security updates.
Last login: Tue Aug 7 15:26:51 2018 from 192.168.0.103
/usr/bin/xauth: file /home/yj/.Xauthority does not exist
yj@yj-Vostro-20-