返回总目录页

ssh软件及命令的使用

  

常用软件安装及使用目录

 

第1章 ssh常用用法小结

1.1 连接到远程主机:

命令格式

ssh name@remoteserver 或者

ssh remoteserver -l name

说明:以上两种方式都可以远程登录到远程主机,server代表远程主机,name为登录远程主机的用户名。

实例1-1 

[root@m01 ~]# ssh 172.16.1.31            用户名可以省略

Last login: Sun Oct 22 07:01:34 2017 from 10.0.0.1

wo shi ma chang wei

[root@nfs01 ~]# ifconfig eth0|sed -n "2p"

          inet addr:10.0.0.31  Bcast:10.0.0.255  Mask:255.255.255.0

实例1-2 

[root@nfs01 ~]# ssh oldboy@172.16.1.8

The authenticity of host '172.16.1.8 (172.16.1.8)' can't be established.

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

Are you sure you want to continue connecting (yes/no)? yes     

Warning: Permanently added '172.16.1.8' (RSA) to the list of known hosts.

oldboy@172.16.1.8's password:

wo shi ma chang wei

[oldboy@web01 ~]$ ifconfig eth0|sed -n "2p"

          inet addr:10.0.0.8  Bcast:10.0.0.255  Mask:255.255.255.0

实例1-3 

[oldboy@web01 ~]$ ssh 172.16.1.7 -l oldboy

The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.16.1.7' (RSA) to the list of known hosts.

oldboy@172.16.1.7's password:

wo shi ma chang wei

[oldboy@web02 ~]$ ifconfig eth0|sed -n "2p"

          inet addr:10.0.0.7  Bcast:10.0.0.255  Mask:255.255.255.0

 

1.2 连接到远程主机指定的端口:

命令格式:

ssh name@remoteserver -p 2222 或者

ssh remoteserver -l name -p 2222

说明:参数指定端口号,通常在路由里做端口映射时,我们不会把22端口直接映射出去,而是转换成其他端口号,这时就需要使用-p端口号命令格式。

实例1-1 

[root@m01 ~]# ssh 172.16.1.41 

ssh: connect to host 172.16.1.41 port 22: Connection refused

[root@m01 ~]# ssh -p52113 172.16.1.41

Last login: Sat Oct 21 23:18:08 2017 from 10.0.0.1

wo shi ma chang wei

[root@backup ~]# ip a|sed -n "8p"

    inet 10.0.0.41/24 brd 10.0.0.255 scope global eth0

1.3 通过远程主机1跳到远程主机2

命令格式:

ssh -t remoteserver1 ssh remoteserver2

说明:当远程主机remoteserver2无法直接到达时,可以使用-t参数,然后由remoteserver1跳转到remoteserver2。在此过程中要先输入remoteserver1的密码,然后再输入remoteserver2的密码,然后就可以操作remoteserver2了。

实例1-1 

[root@m01 ~]# ssh -t 172.16.1.31 ssh -p52113 oldboy@172.16.1.41

The authenticity of host '[172.16.1.41]:52113 ([172.16.1.41]:52113)' can't be established.

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '[172.16.1.41]:52113' (RSA) to the list of known hosts.

oldboy@172.16.1.41's password:

wo shi ma chang wei

[oldboy@backup ~]$ ip a|sed -n "2p"

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

[oldboy@backup ~]$ ip a|sed -n "8p"

    inet 10.0.0.41/24 brd 10.0.0.255 scope global eth0

1.4 通过SSH运行远程shell命令:

命令格式:

ssh -l name remoteserver ‘command’

说明:连接到远程主机,并执行远程主机的command命令。例如:查看远程主机的内存使用情况。

$ ssh -l root 192.168.1.100 svmon -G

实例1-1 

[root@m01 ~]# ssh -l oldboy 172.16.1.31 free -m

oldboy@172.16.1.31's password:

             total       used       free     shared    buffers     cached

Mem:           980        193        786          0         34         51

-/+ buffers/cache:        107        873

Swap:          767          0        767

 

1.5 修改SSH监听端口:

默认情况下,SSH监听连接端口22,攻击者使用端口扫描软件就可以看到主机是否运行有SSH服务,将SSH端口修改为大于1024的端口是一个明智的选择,因为大多数端口扫描软件(包括nmap)默认情况都不扫描高位端口。打开/etc/ssh/sshd_config文件并查找下面这样的行:

Port  22

去掉该行前面的号,然后修改端口号并重新启动SSH服务:

$ /etc/init.d/ssh restart

实例1-1 

[root@backup ~]# ss -lntup|grep sshd

tcp    LISTEN     0      128                   :::52113                :::*      users:(("sshd",1415,4))

tcp    LISTEN     0      128                    *:52113                 *:*      users:(("sshd",1415,3))

[root@backup ~]# vim /etc/ssh/sshd_config

#Port 52113

[root@backup ~]# /etc/init.d/sshd restart

Stopping sshd:                                             [  OK  ]

Starting sshd:                                             [  OK  ]

[root@backup ~]# ss -lntup|grep sshd

tcp    LISTEN     0      128                   :::22                   :::*      users:(("sshd",1543,4))

tcp    LISTEN     0      128                    *:22                    *:*      users:(("sshd",1543,3))

1.6 仅允许SSH协议版本2

有两个SSH协议版本,仅使用SSH协议版本2会更安全,SSH协议版本1有安全问题,包括中间人攻击(man-in-the-middle)和注入(insertion)攻击。编辑/etc/ssh/sshd_config文件并查找下面这样的行:

# Protocol 2,1

修改为

Protocol 2

1.7 禁止root用户登录:

通常情况下,不采用直接用root用户登录到远程主机,由于root用户拥有超级权限,这样会带来安全隐患,所以,一般我们用普通用户登录,当需要管理远程主机时,再切换到root用户下。打开/etc/ssh/sshd_config文件并查找下面这样的行:

#PermitRootLogin yes

#号去掉,然后将yes修改成no,重启ssh服务,这样就可以禁止root用户登录。

实例1-1 

[root@m01 ~]# ssh oldboy@172.16.1.41

oldboy@172.16.1.41's password:

Last login: Sat Oct 21 23:25:45 2017 from 172.16.1.31

wo shi ma chang wei

[oldboy@backup ~]$ logout

Connection to 172.16.1.41 closed.

[root@m01 ~]# ssh root@172.16.1.41

root@172.16.1.41's password:

Permission denied, please try again.

1.8 设置登录时提示信息

首先编辑一个文件,如bannertest.txt,文件内容自行定义。然后打开/etc/ssh/sshd_config文件并查找下面这样的行:

#Banner /some/path

#号去掉,然后将bannertest.txt文件的全路径替换/some/path,然后保存,重启ssh服务。当客户端登录时,就会看到bannertest.txt文件中的提示信息。

实例1-1 

# no default banner path

#Banner none

banner /machangwei/qianyan           

[root@backup ~]# /etc/init.d/sshd restart

Stopping sshd:                                             [  OK  ]

Starting sshd:                                             [  OK  ]

 

[root@m01 ~]# ssh 172.16.1.41

wo hen shan liang de

Last login: Sat Oct 21 23:50:38 2017 from 172.16.1.61

wo shi ma chang wei

1.9 进行端口映射:

假如公司内网有台web服务器,但是只对内不对外,这样,外网就无法访问,可以用ssh进行端口映射来实现外网访问内网的web服务器。假如web服务器名为webserverwebserver可以用ssh访问到远端主机remoteserver,登录到webserver,然后用下面命令进行映射

命令格式:

ssh -R 3000:localhost:80 remoteserver

执行完成后,在remoteserver机器上,执行netstat -an | grep 3000,查看有没有开通3000端口。并执行以下命令观察是否可以打开webserver上的网页

$ w3m http://127.0.0.1:3000

如果能打开界面,说明映射成功.但是,这只限于本机访问web服务器,即只能remoteserver机器访问webserver。因为3000端口绑定的是remoteserver机器的127.0.0.1端口。可以编辑remoteserver机器上的/etc/ssh/sshd_config文件并添加如下内容:

添加 GatewayPorts yes  内容,把监听端口3000绑定到 0.0.0.0 地址上,这样外部的所有机器都能访问到这个监听端口,然后保存退出。并重启ssh服务。完成后其它机器就可以在浏览器中输入 http://remoteserver:3000来访问webserver了。

 

第2章 几个ssh管道用法

2.1 查看SSH客户端版本

有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH

$ ssh -V 
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003

下面的例子表明该系统正在使用SSH2

$ ssh -V 
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu

 

1. remote file copy

[root@m01 ~]# cat 1.txt

  root:x:0:0:root:/root:/bin/bash

[root@m01 ~]# cat 1.txt|ssh 172.16.1.31 'cat - >~/aaa'

[root@m01 ~]# ssh 172.16.1.31 "cat aaa"

  root:x:0:0:root:/root:/bin/bash


拷贝文件时,如果文件很大,又不想影响网络IO可以用pv工具进行流量控制

pv -L10m test.pl | ssh 10.1.74.76 'cat - > /tmp/test.pl'

这里pv的行为跟cat比较类似,但是支持IO流量控制,这里设置10M/s.

2.2 local script remote execute

[root@m01 ~]# cat 1.txt

#!/bin/sh

hostname

[root@m01 ~]# cat 1.txt|ssh 172.16.1.31 "sh"

nfs01

[root@m01 ~]# ssh 172.16.1.31 'sh' <1.txt 

nfs01

[root@m01 ~]# ssh 172.16.1.31 'sh 1.txt'  

sh: 1.txt: No such file or directory


这样就不用把脚本拷贝到远端去执行了

参考:

http://linux.icydog.net/ssh/piping.php

http://www.ivarch.com/programs/quickref/pv.shtml

http://www.mysqlperformanceblog.com/2009/05/20/hint-throttling-xtrabackup/

 

2.3 故障1/var/empty/sshd

Starting sshd:/var/empty/sshd must be owned by root and not group or world-writable.                                                             [FAILED]

 

解决方案:

这个是权限的问题

可采取以下两步解决

chown   -R   root.root    /var/empty/sshd

chmod 744 /var/empty/sshd

service sshd restart

就可以解决上述的问题

[c:\~]$ ssh 10.0.0.61                连接外网IP

Connecting to 10.0.0.61:22...

Connection established.

To escape to local shell, press 'Ctrl+Alt+]'.

 

Last login: Sun Oct 22 18:06:38 2017 from 10.0.0.1

wo shi ma chang wei

[root@m01 ~]#

2.4 故障2

 

 

/home/admin 权限应该是 700,属性admin admin

/home/admin/.ssh 权限应该是 755 属性 admin admin

/home/admin/.ssh/authorized_keys 权限应该是644 属性admin admin

 

故障3

 

通过ssh连接不上了 

[root@localhost webapps]# ssh root@192.168.0.10 ssh_exchange_identification: Connection closed by remote host

 

联系机房使用KVM也输入用户名和密码后又自动退出跳转到登陆界面,通过KVM切换用户模式的时候提示INIT: cannot fork, retry..初步怀疑系统资源肯定消耗干了,要么等待系统释放部分资源以后再进入,但是现在已经有部分应用不能使用,只能重启系统. 重启系统以后查看/var/log/messages日志中发现 Jan 24 11:32:55 localhost automount[2831]: expire_proc: expire thread create for /misc failed 而且故障时间也基本吻合更进一步说明是线程数创建过多,并且有死锁导致

 

注释 通过cat /proc/sys/kernel/threads-max 能查看到系统的最大线程数

 

2.5 普通用户连接其他用户权限:

.

实例2-1 普通用户连接其他用户:

[p@web02 ~]$ ssh 172.16.1.31

The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.

RSA key fingerprint is 07:96:2a:15:c4:24:e5:b3:4e:fc:e8:a7:15:98:bb:f4.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.16.1.31' (RSA) to the list of known hosts.

p@172.16.1.31's password:

Permission denied, please try again.

 

2.6 问题一   No route to host 

ssh登录的时候链接端口失败  

提示(1):  # ssh 172.16.81.221  

ssh: connect to host 172.16.81.221 port 22: No route to host  

这由于server端没有开机或是网络不通(这个原因很多,最简单的是网线没有插。还有就是可能会是网卡down了等)  

提示(2):  

# ssh work@172.16.81.221  

ssh: connect to host 172.16.81.221 port 22: Connection refused  

这是由于对方serverssh服务没有开。这个server端开启服务即可。  

 

2.7 问题二 PermitRootLogin no  

sshserver上的时候密码是对的但是报如下息:

[root@nfs01 ~]# ssh 172.16.1.8

root@172.16.1.8's password:

Permission denied, please try again.

root@172.16.1.8's password:

Last login: Sun Oct 22 06:55:06 2017 from 10.0.0.1

wo shi ma chang wei

[root@web01 ~]#

这个是由于如果不输入用户名的时候默认的是root用户,但是安全期间ssh服务默认没有开root用户的ssh权限  

解决方法:  

要修改rootssh权限,即修改 /etc/ssh/sshd_config文件中  PermitRootLogin no 改为 PermitRootLogin yes  

实例2-1 

[root@m01 ~]# ssh 172.16.1.8

root@172.16.1.8's password:

Permission denied, please try again.

2.8 问题三  

登录是出现如下提示:  ssh root@172.16.81.221  

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  

@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @  

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  

IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!  Someone could be eavesdropping on you right now (man-in-the-middle attack)!  It is also possible that the RSA host key has just been changed.  The fingerprint for the RSA key sent by the remote host is  76:fb:b3:70:14:48:19:d6:29:f9:ba:42:46:be:fb:77.  Please contact your system administrator.  

Add correct host key in /home/fante/.ssh/known_hosts to get rid of this  message.  

Offending key in /home/fante/.ssh/known_hosts:68  

RSA host key for 172.16.81.221 has changed and you have requested strict checking.  Host key verification failed.  

server端密码或是其他发生改变的时候。解决方法一般就需要删除~/.ssh/known_hosts的对应行,然后再登录即可。

 

第3章 ssh

3.1 SSH服务协议说明:

01. SSHSecure Shell Protocol的简写

02. SSH先对联机数据包通过加密技术进行加密处理,加密后在进行数据传输。确保了传递的数据安全

 

3.2 SSH服务主要提供两个服务功能:

01. 一是提供类似telnet远程联机服务器的服务,即上面提到的SSH服务;

02. 另一个是类似FTP服务的sftp-server,借助SSH协议来传输数据的,提供更安全的SFTP服务(vsftp,proftp)

 

3.3 SSH服务软件详细说明

openssh-clients软件大礼包包含内容

/etc/ssh/ssh_config    ---ssh服务客户端配置文件

/usr/bin/.ssh.hmac

/usr/bin/scp           ---ssh服务远程传输复制命令

/usr/bin/sftp          ---ssh服务远程传输文件服务

/usr/bin/slogin        ---ssh远程登录命令

/usr/bin/ssh           ---ssh远程登录命令

/usr/bin/ssh-add

/usr/bin/ssh-agent

/usr/bin/ssh-copy-id   ---远程分发公钥命令

/usr/bin/ssh-keyscan

 

openssh-server软件大礼包包含内容

/etc/rc.d/init.d/sshd  ---ssh服务启动脚本文件

/etc/ssh/sshd_config   ---ssh服务配置文件

/etc/sysconfig/sshd    ---ssh服务创建秘钥有关  

/usr/sbin/.sshd.hmac   ---ssh加密算法有关文件

/usr/sbin/sshd         ---ssh服务进程启动命令

 

 

ssh服务认证类型:

01. 基于口令认证方式

02. 基于秘钥认证方式

 

3.4 ssh服务配置文件说明:

01. 配置文件中所有注释信息,表示默认参数配置

02. 配置文件中#空格 后面内容表示说明信息

              #参数 表示配置参数信息

03. 配置文件参数信息修改后,一旦变为注释,即还原为默认配置

              

3.4.1 重点配置参数说明:

#Port 22                --- 表示修改ssh服务默认端口号

#AddressFamily any      --- 指定监听ipv4地址,或是ipv6地址,或者所有都监听

#ListenAddress 0.0.0.0  --- 监听地址只能监听本地网卡上配置的地址,监听的网卡可以对请求做出相应

 

 

 

 

3.4.2 配置步骤

利用telnet协议实现远程登录方法:

第一个里程:安装telnet服务

yum install telnet telnet-server -y

 

第二个里程:启动telnet服务,利用xinetd服务启动

vim /etc/xinetd.d/telnet

disabled = no

/etc/init.d/xinetd start

 

第三个里程:远程登录测试

telnet 10.0.0.31

login

password

至此,登录成功

 

 

3.4.3 基于秘钥登录配置过程:

#环境准备:

01. 管理服务器 m01     10.0.0.61

02. 备份服务器 backup  10.0.0.41

03. 存储服务器 nfs01   10.0.0.31

 

backup  (创建秘钥对)

nfs01   (被管理服务器,接收公钥信息)

 

第一个里程碑:创建秘钥对

# ssh-keygen -t rsa

Generating public/private rsa key pair.

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

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:

1a:80:62:7e:50:5b:3e:30:16:1f:f1:fa:38:94:0f:c2 root@backup

The key's randomart image is:

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

|   *.+.          |

|  o.B o          |

|.o...+ .         |

|o.o  .+          |

| . E =. S        |

|  . o =o         |

|     o.o         |

|      .          |

|                 |

+-----------------+

[root@backup ssh]# ll ~/.ssh/

total 12

-rw------- 1 root root 1675 Oct 18 11:07 id_rsa

-rw-r--r-- 1 root root  393 Oct 18 11:07 id_rsa.pub

-rw-r--r-- 1 root root  784 Oct 11 16:27 known_hosts

 

 

第二个里程:将公钥分发给存储服务器

ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.31

root@172.16.1.31's password:

Now try logging into the machine, with "ssh '172.16.1.31'", and check in:

 

  .ssh/authorized_keys

 

to make sure we haven't added extra keys that you weren't expecting.

 

第三个里程碑:进行登录测试

[root@backup ssh]# ssh 172.16.1.31

Last login: Wed Oct 18 10:16:12 2017 from 10.0.0.41

[root@nfs01 ~]# exit

 

 

3.5 ssh基本语法使用

ssh p52113 oldboy@10.0.0.150 [命令]

#SSH连接远程主机命令的基本语法;

#-p(小写)接端口,默认22端口时可以省略-p22;

#→“@”前面为用户名,如果用当前用户连接,可以不指定用户。

#→“@”后面为要连接的服务器的IP. 更多用法,请man ssh;

ssh p52113 oldboy@10.0.0.150 [命令]   --- 远程执行命令

ssh p52113 oldboy@10.0.0.150          --- 远程登录

 

3.6 scp的基本语法使用

scp的基本语法使用:scp - secure copy (remote file copy program)

每次都是全量拷贝,增量拷贝rsync

推:PUSH

scp -P22 -rp /tmp/oldboy oldboy@10.0.0.143:/tmp

<- -P(大写,注意和ssh命令的不同)接端口,默认22端口时可以省略-P22;

<- -r递归,表示拷贝目录;

<- -p表示在拷贝前后保持文件或目录属性;

<- -l limit 限制速度。

<- /tmp/oldboy为本地的目录。“@”前为用户名,“@”后为要连接的服务器的IP

IP后的:/tmp目录,为远端的目标目录。

说明:以上命令作用是把本地/tmp/oldboy拷贝到远端服务器10.0.0.143/tmp目录;

 

拉:PULL

scp -P22 -rp root@10.0.0.7:/tmp/oldboy /opt/

说明:还可以把远端目录抓到本地

结论:scp为远程拷贝文件或目录的命令,更多用法,请man scp;拷贝权限为连接的用户对应的权限。

 

3.6.1 登陆FTP

登陆FTP的方法就是sftp oldboy@10.0.0.142 如果ssh端口为52113则登陆命令为如下:

[root@A ~]# sftp -oPort=52113 oldboy@10.0.0.142 <- 特殊端口的sftp连接命令

Connecting to 10.0.0.142...

oldboy@10.0.0.142's password:

sftp> cd /home/oldboy <- 还可以像命令行那样切到目标目录

sftp> put /etc/hosts <- /etc/hosts从客户端本地传到sftp服务器当前连接目录,

/home/oldboy  

 

ls    

cd

pwd

说明:操作远程服务器

 

lls

lcd

lpwd

说明:操作本地服务器

 

get   --- 表示从远程服务器下载数据(单个文件)

mget  --- 表示下载多个文件

put   --- 表示从本地服务器上传数据(单个文件)

mput  --- 表示上传多个文件

 

posted @ 2019-02-19 23:44  马昌伟  阅读(2887)  评论(0编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/