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. 【翻译】基于web创建逼真的3D图形 | CSS技巧

    个人翻译小站:http://www.zcfy.cc/article/creating-photorealistic-3d-graphics-on-the-web-css-tricks-4039.htm ...

  2. 深入浅出数据结构C语言版(17)——希尔排序

    在上一篇博文中我们提到:要令排序算法的时间复杂度低于O(n2),必须令算法执行"远距离的元素交换",使得平均每次交换减少不止1逆序数. 而希尔排序就是"简单地" ...

  3. MySQL的数据备份以及pymysql的使用

    一.MySQL的数据备份 语法: # mysqldump -h 服务器 -u用户名 -p密码 数据库名 > 备份文件.sql #示例: #单库备份 mysqldump -uroot -p123 ...

  4. Vue.js项目模板搭建

    前言 从今年(2017年)年初起,我们团队开始引入「Vue.js」开发移动端的产品.作为团队的领头人,我的首要任务就是设计 整体的架构 .一个良好的架构必定是具备丰富的开发经验后才能搭建出来的.虽然我 ...

  5. eclipse Maven新建一个项目并使用

      安装参考这篇博文eclipse配置maven + 创建maven项目(三)  打开pom.xml 试着添加MySQL的JDBC驱动 添加如下配置, <dependency> <g ...

  6. [js高手之路] 设计模式系列课程 - 迭代器(1)

    迭代器是指通过一种形式依次遍历数组,对象,或者类数组结构中的每个元素. 常见的有jquery中的each方法, ES5自带的forEach方法. 下面我们就来自定义一个类似jquery或者ES5的迭代 ...

  7. NDK调试

    第一种(控制台输出): 1.配置好环境变量,这是为了方便起见.将你sdk和ndk的根目录放到环境变量path中.配置完成之后可以来个小检测: 在命令行分别输入adb和ndk-stack后点击回车,只要 ...

  8. 一张图告诉你移动Web前端所有技术(工程化、预编译、自动化)

    你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa 跨终端技术:HTML5.CSS 3.JavaScript ...

  9. 彻底弄懂AngularJS中的transclusion

    点击查看AngularJS系列目录 彻底弄懂AngularJS中的transclusion AngularJS中指令的重要性是不言而喻的,指令让我们可以创建自己的HTML标记,它将自定义元素变成了一个 ...

  10. Spring配置中<context:annotation-config> VS <context:component-scan>

    Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...