p { margin-bottom: 0.25cm; line-height: 120% }
a:link { }

在最早的远程连接技术,主要是telnet和RSH为主。缺点也很明显,就是明文传输。在网络上传输的数据会被截获。因此发展出了文字接口加密。以SSH为主。这种连接加密技术的机制简单来说就是采用非对称密钥系统,也就是公钥和私钥。在网络中传输的数据通过公钥来加密,在本端收到后用私钥解密。公钥是大家都能获取的。而私钥是存储在本地的不能外流。

首先我们来看下如何搭建一台可以远程访问的SSH服务器。SSH分为客户端和服务器端。如果你只是想登陆别的SSH只需要安装openssh-client. ubuntun默认是有安装的。如果没有则用apt-get install openssh-client。如果要使本机开放SSH服务器就需要安装openssh-server

apt-get
install openssh-server 安装好后会在/etc/ssh下面会出现生成的秘钥

root@zhf-linux:/etc/ssh#
ls -al

total
356

drwxr-xr-x
2 root root 4096 Oct 27 22:35 .

drwxr-xr-x
148 root root 12288 Oct 27 22:35 ..

-rw-r--r--
1 root root 300261 Mar 16 2017 moduli

-rw-r--r--
1 root root 1756 Mar 16 2017 ssh_config

-rw-r--r--
1 root root 2542 Oct 27 22:35 sshd_config

-rw-------
1 root root 672 Oct 27 22:35 ssh_host_dsa_key

-rw-r--r--
1 root root 604 Oct 27 22:35 ssh_host_dsa_key.pub

-rw-------
1 root root 227 Oct 27 22:35 ssh_host_ecdsa_key

-rw-r--r--
1 root root 176 Oct 27 22:35 ssh_host_ecdsa_key.pub

-rw-------
1 root root 411 Oct 27 22:35 ssh_host_ed25519_key

-rw-r--r--
1 root root 96 Oct 27 22:35 ssh_host_ed25519_key.pub

-rw-------
1 root root 1675 Oct 27 22:35 ssh_host_rsa_key

-rw-r--r--
1 root root 396 Oct 27 22:35 ssh_host_rsa_key.pub

-rw-r--r--
1 root root 338 Oct 27 22:35 ssh_import_id

然后通过/etc/init.d/ssh
start启动。访问方式如下:ssh
用户名@服务器地址

root@zhf-linux:/etc/ssh#
ssh zhf@192.168.0.9

zhf@192.168.0.9's
password:

Welcome
to Ubuntu 15.04 (GNU/Linux 3.19.0-84-generic i686)

*
Documentation: https://help.ubuntu.com/

Last
login: Tue Jul 25 11:05:08 2017

To
run a command as administrator (user "root"), use "sudo
<command>".

See
"man sudo_root" for details.

zhf@zhf-virtual-machine:~$
ls -al

前面访问的不是root用户,因为ubuntun是默认不启用root用户也不允许root远程登陆的。如果真的要使用root用户登陆,就要修改ssh_config的设置。找到PermitRootLogin
no 这一行修改为PermitRootLogin
yes 设置好后就可以访问了,否则访问的时候会提示Permission
Denied.

root@zhf-linux:/etc/ssh#
ssh root@192.168.0.9

root@192.168.0.9's
password:

Welcome
to Ubuntu 15.04 (GNU/Linux 3.19.0-84-generic i686)

*
Documentation: https://help.ubuntu.com/

Your
Ubuntu release is not supported anymore.

For
upgrade information, please visit:

http://www.ubuntu.com/releaseendoflife

New
release '16.04.3 LTS' available.

Run
'do-release-upgrade' to upgrade to it.

Last
login: Wed Jul 26 10:19:13 2017

root@zhf-virtual-machine:~#

退出的时候输入exit就可以了。

root@zhf-virtual-machine:~#
exit

logout

Connection
to 192.168.0.9 closed.

这种密码登陆的方式每次都要输入密码,还是比较麻烦,我们可以用免密码登陆的方式,也就是通过证书认证的方式来进行登陆,那么首先就要在客户端先生成证书。通过ssh-keygen的方式生成证书。

root@zhf-linux:~/.ssh#
ssh-keygen -t rsa

Generating
public/private rsa key pair.

Enter
file in which to save the key (/root/.ssh/id_rsa):

/root/.ssh/id_rsa
already exists.

Overwrite
(y/n)? y

Enter
passphrase (empty for no passphrase):

Enter
same passphrase again:

Your
identification has been saved in /root/.ssh/id_rsa.

Your
public key has been saved in /root/.ssh/id_rsa.pub.

The
key fingerprint is:

SHA256:bC9RjgrGUAL4+74XEkEeK09Mpvv7pANkvdi/kNDTRdM
root@zhf-linux

The
key's randomart image is:

+---[RSA
2048]----+

|o...*
o. |

|.
O.o . .E |

|
.+.=. . . |

|
+Oo. o + |

|
oo+Bo. S . |

|
++++.o o |

|
o+oo.. . |

|
o=o . |

|
.==o. |

+----[SHA256]-----+

在/etc/ssh目录下能找到生成的证书

root@zhf-linux:/home/zhf#
cd /etc/ssh

root@zhf-linux:/etc/ssh#
ls -al

total
356

drwxr-xr-x
2 root root 4096 Oct 27 22:35 .

drwxr-xr-x
148 root root 12288 Oct 27 22:35 ..

-rw-r--r--
1 root root 300261 Mar 16 2017 moduli

-rw-r--r--
1 root root 1756 Mar 16 2017 ssh_config

-rw-r--r--
1 root root 2542 Oct 27 22:35 sshd_config

-rw-------
1 root root 672 Oct 27 22:35 ssh_host_dsa_key

-rw-r--r--
1 root root 604 Oct 27 22:35 ssh_host_dsa_key.pub

-rw-------
1 root root 227 Oct 27 22:35 ssh_host_ecdsa_key

-rw-r--r--
1 root root 176 Oct 27 22:35 ssh_host_ecdsa_key.pub

-rw-------
1 root root 411 Oct 27 22:35 ssh_host_ed25519_key

-rw-r--r--
1 root root 96 Oct 27 22:35 ssh_host_ed25519_key.pub

-rw-------
1 root root 1675 Oct 27 22:35 ssh_host_rsa_key

-rw-r--r--
1 root root 396 Oct 27 22:35 ssh_host_rsa_key.pub

-rw-r--r--
1 root root 338 Oct 27 22:35 ssh_import_id

然后我们需要将我们生成的公钥证书上传给服务器便于认证。
通过ssh-copy-id的方式进行证书上传。

root@zhf-linux:~/.ssh#
ssh-copy-id root@192.168.0.9

/usr/bin/ssh-copy-id:
INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

/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

root@192.168.0.9's
password:

Number
of key(s) added: 1

Now
try logging into the machine, with: "ssh 'root@192.168.0.9'"

and
check to make sure that only the key(s) you wanted were added.

在服务器端会发现生成了authorized_keys文件

root@zhf-virtual-machine:~#
ls -al /root/.ssh

total
24

drwx------
2 root root 4096 Oct 27 23:11 .

drwx------
10 root root 4096 Oct 27 22:11 ..

-rw-------
1 root root 396 Oct 27 23:11 authorized_keys

-rw-------
1 root root 1675 Oct 27 23:10 id_rsa

-rw-r--r--
1 root root 406 Oct 27 23:10 id_rsa.pub

-rw-r--r--
1 root root 222 Aug 31 22:17 known_hosts

查看文件可以看到是生成了客户端的公钥。

root@zhf-virtual-machine:~/.ssh#
cat ./authorized_keys

ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDE9qGkCG+g6UUEHLKvr4lWSrwwyvVDLcWDhfNBiifkegSuDeOW258SOkDmti4nnMvF1OOazYbWKjnRkk1XEa3GK70PsP7I/wu0w+POnP+NBsIFdMhyJsETfT1WzC5Gnk6eAtbwQuAw50s25qoXmutW97Nq0mGr2Dk03ysoUyMjM1mWCAkiC2l50K4EDW4S28SC3e5hdhuQ285s62fCvaCEKcoj16Ewth4H2x+MKCi2zgL/m4yjXhAJIggDofXl3CugqDCmZY3aEVByF9q7HTUZbKd1tk9QMjGGqg0e/B55GL2F1POUVEVTxwGQ/W1z/DiEZCzpKzzmtNH8+h3KfWeP
root@zhf-linux

继续访问就不需要密码了,直接就可以登陆上远端电脑了。

root@zhf-linux:/etc/ssh#
ssh root@192.168.0.9

Welcome
to Ubuntu 15.04 (GNU/Linux 3.19.0-84-generic i686)

*
Documentation: https://help.ubuntu.com/

0
packages can be updated.

0
updates are security updates.

Your
Ubuntu release is not supported anymore.

For
upgrade information, please visit:

http://www.ubuntu.com/releaseendoflife

New
release '16.04.3 LTS' available.

Run
'do-release-upgrade' to upgrade to it.

Last
login: Sun Oct 29 09:38:23 2017 from 192.168.0.11

root@zhf-virtual-machine:~#

ssh命令只是远程连接的命令而已,如果想从远程服务器上下载或者是上传文件,那就要用到SFTP或者FTP命令了,命令的使用方式和FTP一样的。

root@zhf-linux:/etc/ssh#
sftp root@192.168.0.9

Connected
to 192.168.0.9.

sftp>
cd /home/zhf

sftp>
dir

Desktop
Documents

Downloads
Music

Pictures
Public

Templates
VMwareTools-9.6.1-1378637.tar.gz

Videos
examples.desktop

vmware-tools-distrib
zhf

下载

sftp>
pwd

Remote
working directory: /home/zhf

sftp>
cd ./zhf

sftp>
dir

python_prj
python_src tcpdumpresult.txt

下载tcpdumpresult文件

sftp>
get tcpdumpresult.txt

Fetching
/home/zhf/zhf/tcpdumpresult.txt to tcpdumpresult.txt

/home/zhf/zhf/tcpdumpresult.txt
100% 30KB 30.0KB/s 00:00

将本机的test.txt文件上传到远端电脑,远端电脑的存放路径为/root/test.txt

sftp>
put /home/zhf/zhf/test.txt

Uploading
/home/zhf/zhf/test.txt to /root/test.txt

/home/zhf/zhf/test.txt
100% 0 0.0KB/s 00:00

另外还有一种简洁的方式就是通过SCP的方式,如果知道远程服务器的目录。那么可以直接通过

scp
源文件路径 用户名@IP:目的文件路径的方式进行上传数据。

root@zhf-linux:/home/zhf/zhf#
scp /home/zhf/zhf/test1.txt root@192.168.0.9:/home/root

test1.txt
100% 2633KB 2.6MB/s 00:01

那么如果在window界面通过ssh的方式登陆linux呢,只需要下载相应的软件就可以了,常用的就是Puttygen软件,导入先前生成的私钥id_rsa,转换成putty所识别的格式(*.ppk),得到文件id_rsa.ppk.



windows上启动putty,进行如下配置

Session-Logging-Hostname:填上你的linux的ip

Windows
-Translation
- 在下拉菜单里选上UTF-8,这里不设置,登录后将会出现中文乱码。

Connection-
Data - Auto login username:填上你登录ubuntu时用的用户名。

Connection-
SSH-Auth-Private key file for
authentication:选上id_rsa.ppk

保存Session配置。然后点击登陆就可以了

一起来学linux:SSH远程登陆的更多相关文章

  1. Linux服务器开启ssh服务,实现ssh远程登陆!

    最近在学linux,使用ssh远程登陆linux,记录下来! 首先进入/etc目录下,/etc目录存放的是一些配置文件,比如passwd等配置文件,要想使用ssh远程登陆,需要配置/etc/ssh/s ...

  2. linux系统新建用户ssh远程登陆显示-bash-4.1$解决方法

    linux系统新建的用户用ssh远程登陆显示-bash-4.1$,不显示用户名路径 网络上好多解决办法,大多是新建.bash_profile文件然后输入XXXXX....然而并没有什么用没有用.... ...

  3. linux系统新建用户ssh远程登陆显示-bash-4.1$解决方法,ssh-bash-4.1

    linux系统新建的用户用ssh远程登陆显示-bash-4.1$,不显示用户名路径 网络上好多解决办法,大多是新建.bash_profile文件然后输入XXXXX....然而并没有什么用没有用.... ...

  4. CentOS6无法本地登陆,ssh远程登陆没问题

    CentOS6无法本地登陆,ssh远程登陆没问题---使用CentOS自带的rsyslog分析调试 Apr 21 14:15:27 raccontroller init: tty (/dev/tty1 ...

  5. SSH 远程登陆

    2019-03-10 20:41:39 一.什么是SSH 简单说,SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登 ...

  6. 很好用的取代 PuTTY 的SSH远程登陆软件 Termius

    一直以来, 我都是用 PuTTY 一个窗口一个窗口来监视我所有的远程服务器. putty-connections-on-a-screen 总感觉非常的不方便, 特别是当远程链接断开需要再重新打开PUT ...

  7. 在linux终端远程登陆linux服务器

    在linux终端远程登陆linux服务器   原来在Linux终端远程登陆linux服务器是那么的容易,如果的服务器用户名是abc(也可以是root),只需要在终端输入: 然后电脑会提示输入密码就登录 ...

  8. 如何通过linux ssh远程linux不用输入密码登入

    如何通过一台linux ssh远程其他linux服务器时,不要输入密码,可以自动登入.提高远程效率,不用记忆各台服务器的密码. 工具/原料   ssh,ssh-keygen,scp 方法/步骤   首 ...

  9. 树莓派3b+ Ubuntu 16.04 MATA系统 ssh远程登陆后修改主机名、用户密码和用户名

    写在前面: 刚刚开始写博客,记录下自己的学习过程,备忘. 最近在使用树莓派做智能小车的开发,使用的是树莓派3b+,安装的是Ubuntu 16.04 MATA 系统,安装系统后需要修改主机名,登陆密码以 ...

  10. Linux SSH远程文件/目录 传输

    Linux SSH远程文件/目录传输命令scp 2010年08月6日 上午 | 作者:VPS侦探 相信各位VPSer在使用VPS时会经常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已 ...

随机推荐

  1. webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】

    实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...

  2. [UIKit学习]02.关于UIButton

    按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 按钮也是一种容器,在这一点上跟UIView类似 按钮的三种状态 normal(普通状态) 默认情况(Default) 对 ...

  3. PyTorch教程之Training a classifier

    我们已经了解了如何定义神经网络,计算损失并对网络的权重进行更新. 接下来的问题就是: 一.What about data? 通常处理图像.文本.音频或视频数据时,可以使用标准的python包将数据加载 ...

  4. hdu1760博弈SG

    A New Tetris Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. IE兼容

    这个基本知识http://www.cnblogs.com/yoosou/archive/2012/07/27/2612443.html 参考: http://www.cnblogs.com/cocow ...

  6. 20.Linux-USB鼠标驱动

    在上一章分析完USB总线驱动程序后, 接下来开始写一个USB驱动: 本节目的: 将USB鼠标的左键当作L按键,将USB鼠标的右键当作S按键,中键当作回车按键 参考/drivers/hid/usbhid ...

  7. ES6-模块化

    ES6-模块化 在es6标准中,js原生支持modulele. ES6模块需要使用babel转码,这里简单解释一下什么是babel转码. babel就是将‘ES6模块化语法’转化为‘CommonJS模 ...

  8. HDU2874 LCA Tarjan

    不知道为什么_add2不能只用单方向呢...........调试了好多次,待我解决这个狗血问题 #include <iostream> #include <vector> #i ...

  9. HDU1068 二分匹配 独立集

    前边和后边性别不同!!!不然NP了 Girls and Boys Problem Description the second year of the university somebody star ...

  10. Java面向对象 集合(上)

     Java面向对象  集合(上) 知识概要:             (1)体系概述 (2)共性方法 (3)迭代器 (4)list集合 (5)Set 集合 体系概述:              集 ...