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. JSP第六篇【自定义标签之传统标签】

    为什么要使用自定义标签? JSTL标签库只提供了简单的输出等功能,没有实现任何的HTML代码封装,并且某些复杂类型转换,或者逻辑处理的时候,JSTL标签库完成不了,需要自定义标签! 编写自定义标签的步 ...

  2. Linux硬链接软连接

    转载原文出处:http://www.cnblogs.com/itech/archive/2009/04/10/1433052.html 1.Linux链接概念 Linux链接分两种,一种被称为硬链接( ...

  3. 对比requirejs更好的理解seajs

    seajs遵循CMD规范,requirejs遵循AMD规范.AMD规范是预加载,CMD规范是赖加载. 下文举例假设有文件 b.js, c.js如下 //b.js define(function(req ...

  4. Java中的流程控制

    1.Java中有几种流程控制?分别是什么? 答:有三种流程控制,分别是顺序流程,分支流程和循环流程 2.分支语句if/else有哪三种形式?分别如何使用? 答:if/if-else-/if-else ...

  5. ambari

    http://blog.csdn.net/ggz631047367/article/details/50491616 https://cwiki.apache.org/confluence/displ ...

  6. SSM框架—详细整合教程(Spring+SpringMVC+MyBatis)

    很久没有新搭建过框架了,今天搭建一遍.以往都是在eclipse中搭建,今天换Idea吧,目前来说Idea用的还是很多的,但是用习惯了eclipse的朋友,可能会不太习惯 ok.....开始: 注意区分 ...

  7. 模型组合(Model Combining)之Boosting与Gradient Boosting

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  8. ARCGIS切图:TPK文件的空间参考为地理坐标系

    先来吐槽一下,之前习惯了百度地图API,所以一直习惯直接将经纬度点添加到地图上进行显示,目前使用ARCGIS RUNTIME FOR ANDROID进行开发,在地图上加点需要原始点的坐标为投影坐标系, ...

  9. Redis info 参数详解

    Redis Info 命令以一种易于理解和阅读的格式,返回关于 Redis 服务器的各种信息和统计数值. 通过给定可选的参数 section ,可以让命令只返回某一部分的信息: server : 一般 ...

  10. 上传文件没有写权限Access to the path is denied

    Access to the path is denied. asp.net程序目录放在系统盘,ntfs格式. 程序中对cfg.xml有写入操作. 运行的时候出现了这个问题. 在我自己的机器上没有问题 ...