一起来学linux:SSH远程登陆
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远程登陆的更多相关文章
- Linux服务器开启ssh服务,实现ssh远程登陆!
最近在学linux,使用ssh远程登陆linux,记录下来! 首先进入/etc目录下,/etc目录存放的是一些配置文件,比如passwd等配置文件,要想使用ssh远程登陆,需要配置/etc/ssh/s ...
- linux系统新建用户ssh远程登陆显示-bash-4.1$解决方法
linux系统新建的用户用ssh远程登陆显示-bash-4.1$,不显示用户名路径 网络上好多解决办法,大多是新建.bash_profile文件然后输入XXXXX....然而并没有什么用没有用.... ...
- linux系统新建用户ssh远程登陆显示-bash-4.1$解决方法,ssh-bash-4.1
linux系统新建的用户用ssh远程登陆显示-bash-4.1$,不显示用户名路径 网络上好多解决办法,大多是新建.bash_profile文件然后输入XXXXX....然而并没有什么用没有用.... ...
- CentOS6无法本地登陆,ssh远程登陆没问题
CentOS6无法本地登陆,ssh远程登陆没问题---使用CentOS自带的rsyslog分析调试 Apr 21 14:15:27 raccontroller init: tty (/dev/tty1 ...
- SSH 远程登陆
2019-03-10 20:41:39 一.什么是SSH 简单说,SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登 ...
- 很好用的取代 PuTTY 的SSH远程登陆软件 Termius
一直以来, 我都是用 PuTTY 一个窗口一个窗口来监视我所有的远程服务器. putty-connections-on-a-screen 总感觉非常的不方便, 特别是当远程链接断开需要再重新打开PUT ...
- 在linux终端远程登陆linux服务器
在linux终端远程登陆linux服务器 原来在Linux终端远程登陆linux服务器是那么的容易,如果的服务器用户名是abc(也可以是root),只需要在终端输入: 然后电脑会提示输入密码就登录 ...
- 如何通过linux ssh远程linux不用输入密码登入
如何通过一台linux ssh远程其他linux服务器时,不要输入密码,可以自动登入.提高远程效率,不用记忆各台服务器的密码. 工具/原料 ssh,ssh-keygen,scp 方法/步骤 首 ...
- 树莓派3b+ Ubuntu 16.04 MATA系统 ssh远程登陆后修改主机名、用户密码和用户名
写在前面: 刚刚开始写博客,记录下自己的学习过程,备忘. 最近在使用树莓派做智能小车的开发,使用的是树莓派3b+,安装的是Ubuntu 16.04 MATA 系统,安装系统后需要修改主机名,登陆密码以 ...
- Linux SSH远程文件/目录 传输
Linux SSH远程文件/目录传输命令scp 2010年08月6日 上午 | 作者:VPS侦探 相信各位VPSer在使用VPS时会经常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已 ...
随机推荐
- JavaSE(十)之Map总结
上一篇是总结了Collection接口的各种实现类,这一篇我将分享的是Map的总结,希望大家点评! 一.Map接口 1.1.为什么Collection不能满足集合的所有功能? Collection接口 ...
- linux加载与使用ko驱动
linux驱动和有两种形式: 1:编译到内核 2:编译为ko模块 这里记录下ko模块使用方法. 首先cd到/var/lib/(内核版本)/drivers/ 在这里面找到要装载的模块ko文件 modpr ...
- Linux Ubuntu从零开始部署web环境及项目-----搭建ssh环境(一)
linux搭建ssh环境 1,用户登录 成功输入用户名和密码后 进入Ubuntu界面 2,配置网络 参考:http://blog.csdn.net/liu782726344/article/deta ...
- OpenStack Ocata 超详细搭建文档
前言 搭建前必须看我本文档搭建的是分布式O版openstack(controller+ N compute + 1 cinder)的文档.openstack版本为Ocata.搭建的时候,请严格按照文档 ...
- Angular2 Service实践
引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...
- 深入理解计算机系统_3e 第二章家庭作业答案
初始完成者:哈尔滨工业大学 李秋豪 许可:除2.55对应代码外(如需使用请联系randy.bryant@cs.cmu.edu),任何人可以自由的使用,修改,分发本文档的代码. 本机环境: (有一些需要 ...
- 浅析前端开发中的 MVC/MVP/MVVM 模式
MVC,MVP和MVVM都是常见的软件架构设计模式(Architectural Pattern),它通过分离关注点来改进代码的组织方式.不同于设计模式(Design Pattern),只是为了解决一类 ...
- 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏
1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...
- 我的第一个python web开发框架(4)——数据库结构设计与创建
小白做好前端html设计后,马上开始进入数据库结构设计步骤. 在开始之前,小白回忆了一下老大在公司里培训时讲过的数据库设计解说: 对于初学者来说,很多拿到原型时不知道怎么设计数据表结构,这是很正常的事 ...
- python Synchronization between processes
进程间同步,可以使用lock进行控制. 官方文档的例子如下: from multiprocessing import Process, Lock def f(l, i): l.acquire() pr ...