SSH

Posted physique

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH相关的知识,希望对你有一定的参考价值。

SSH

1、ssh是安全的加密协议,用于远程连接linux服务器。

2、ssh默认端口是22,安全协议版本ssh2。

3、ssh服务端主要包含两个服务功能ssh远程连接,sftp服务。

4、linux ssh客户端包含ssh远程连接命令,以及远程拷贝scp命令等。

SSH服务认证类型

基于口令的安全验证

  1. The authenticity of host ‘192.168.31.132 (192.168.31.132)‘ can‘t be established.
  2. RSA key fingerprint is a0:60:7f:c8:e1:2c:d4:3b:2c:63:b7:3d:66:ad:f2:18.
  3. Are you sure you want to continue connecting (yes/no)? yes
  4. Warning: Permanently added ‘192.168.31.132‘ (RSA) to the list of known hosts.
  5. [email protected]‘s password:
  6. Last login: Mon Feb 6 13:33:19 2017 from 192.168.31.1

ssh连接远程主机命令的基本语法:

-p接端口,默认22端口

@前面为用户名

@后面为要连接的服务器ip

  1. [[email protected] ~]# cat ~/.ssh/known_hosts
  2. 192.168.31.132 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAzH2jCItapPoUp5IKjVNtNOfXM5FmPQ3i27SjDQzzblL2vVaqAzfA10IsHw/QLfUbBpVERmbxZMW1SRrdcxXOWPFatuYmZMJDja4gi2FstEVxvV+ozelhuxEF9khZEYJHndfh5jqBXOYAe6NXOhY6rheEUmao3Wi5FLqdQ9cE0PBfI7SEn6dWCZ5dTJ76qtyLbctTmHC/tgCi3bqmrMR+hCH+PoiHYitEztwXAEWYxAka0d0ET96Z19DMDF9ai8YsrfAH/BgRiAoeUgNhtc/LrmVKWSeeHk15UvvY8Ba2nzI1jYkVH2UOROYn4jnYhSlY7cI7umdkU5LflGvrHmfJcQ==

ssh总结:

1、切换到别的机器上ssh -p52113 [email protected]

2、到其他机器执行命令(不会切换到别的机器上)ssh -p 52113 [email protected] 命令(全路径)。

3、当第一次ssh连接的时候,本地会产生一个密钥文件~/.ssh/known_hosts(多个密钥)。

基于密钥的安全验证

事先建立一对密钥对,然后把公用密钥(public key)放在需要访问的目标服务器上,另外,还需要把私有密钥(private key)放到ssh的客户端对应的客户端服务器上。

根据端口号(111)查出对应的服务:

lsof -i:111

netstat -lntup|grep 111

根据进程名(sshd)查出对应的端口号:

netstat -lntup|grep sshd

更改默认ssh登录配置

  1. #更改前备份
  2. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori
  3. vi /etc/ssh/sshd_config
  4. Port 52113 #默认22端口,为了提高安全级别建议修改
  5. PermitRootLogin no #root超级用户禁止远程登录
  6. PermiEmptyPasswords no #禁止空密码登录
  7. UseDNS no #不使用DNS
  8. GSSAPIAuthenication no

ssh客户端附带的远程拷贝scp命令

  1. [[email protected] ~]# scp -P22 -r -p /etc [email protected]:/tmp
  2. [email protected]‘s password:
  3. system-release-cpe 100% 25 0.0KB/s 00:00
  4. K89rdisc 100% 1513 1.5KB/s 00:00

-P(大写)接端口。

-r递归,表示拷贝目录。

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

-l limit限制速度

scp总结:

1、scp是加密的远程拷贝,cp仅为本地拷贝。

2、可以把数据从一台机器推送到另一台机器,也可以从其它服务器把数据拉回到本地执行命令的服务器。

3、每次都是全量完整拷贝,因此,效率不高,适合第一次拷贝用,如果需要增量拷贝用rsync。

sftp

1、linux下连接命令sftp -oPort=22 [email protected]

2、上传put加客户端本地路径put /etc/hosts,也可以指定路径上传put /etc/hosts /tmp。

3、下载get服务端的内容get hosts,linux下载到本地连接前的目录,也可以指定下载路径get /etc/hosts /tmp。

4、连接的远端家目录为默认目录,也可以切换到其他有权限的目录下。

批量分发文件、执行命令

配置ip主机名

  1. [[email protected] ~]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4.  
  5. 192.168.31.128 server
  6. 192.168.31.134 lnmp
  7. 192.168.31.133 lamp
  8. 192.168.31.132 backup

IT公司企业级批量分发、管理方案

1、中小企业基本sshkey密钥的方案。

2、门户网站sina puppet(复制、太重)。

3、赶集、小米saltstack批量管理(轻量)。

创建用户oldgirl,密码system

  1. [[email protected] data]# useradd oldgirl
  2. [[email protected] data]# echo system|passwd --stdin oldgirl
  3. Changing password for user oldgirl.
  4. passwd: all authentication tokens updated successfully.

创建公私钥

  1. [[email protected] data]# su - oldgirl
  2. ssh ssh-agent sshd ssh-keyscan
  3. ssh-add ssh-copy-id ssh-keygen
  4. [[email protected] ~]$ ssh-keygen -t dsa #dsa
  5. Generating public/private dsa key pair.
  6. Enter file in which to save the key (/home/oldgirl/.ssh/id_dsa): #存放密钥的路径
  7. Created directory ‘/home/oldgirl/.ssh‘.
  8. Enter passphrase (empty for no passphrase):
  9. Enter same passphrase again:
  10. Your identification has been saved in /home/oldgirl/.ssh/id_dsa.
  11. Your public key has been saved in /home/oldgirl/.ssh/id_dsa.pub.
  12. The key fingerprint is:
  13. 5a:64:22:18:c1:4c:70:ea:dd:64:9d:82:81:0b:99:07 [email protected]
  14. The key‘s randomart image is:
  15. +--[ DSA 1024]----+
  16. |EO*o |
  17. |+++oo . . |
  18. |o.o..+.oo |
  19. |... +..+ |
  20. | . . . S |
  21. | o |
  22. | . |
  23. | |
  24. | |
  25. +-----------------+

 

  1. [[email protected] ~]$ ls -l .ssh/
  2. total 8
  3. -rw-------. 1 oldgirl oldgirl 668 Feb 7 09:41 id_dsa #私钥
  4. -rw-r--r--. 1 oldgirl oldgirl 604 Feb 7 09:41 id_dsa.pub #公钥

分发公钥

  1. [[email protected] ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected] #默认22端口
  2. ssh: connect to host 182.168.31.134 port 22: Connection refused
  3. [[email protected] ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected]
  4. The authenticity of host ‘192.168.31.134 (192.168.31.134)‘ can‘t be established.
  5. RSA key fingerprint is c1:0f:e0:45:05:79:c9:f0:48:d3:2f:6b:dc:66:6a:fe.
  6. Are you sure you want to continue connecting (yes/no)? yes
  7. Warning: Permanently added ‘192.168.31.134‘ (RSA) to the list of known hosts.
  8. [email protected]‘s password:
  9. Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
  10.  
  11.   .ssh/authorized_keys
  12.  
  13. to make sure we haven‘t added extra keys that you weren‘t expecting.

 

  1. [[email protected] ~]# su - oldgirl
  2. [[email protected] ~]$ ls -l .ssh/
  3. total 4
  4. -rw-------. 1 oldgirl oldgirl 604 Feb 7 09:49 authorized_keys #权限600,名字改变

如果不是22端口(52113端口):ssh-copy-id -i .ssh/id_dsa.pub "-P 52113 [email protected]"

  1. [[email protected] ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected]
  2. [[email protected] ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected]

测试免密查询ip

  1. [[email protected] ~]$ ssh -P22 [email protected] /sbin/ifconfig eth0
  2. eth0 Link encap:Ethernet HWaddr 00:0C:29:03:06:08
  3.           inet addr:192.168.31.134 Bcast:192.168.31.255 Mask:255.255.255.0
  4.           inet6 addr: fe80::20c:29ff:fe03:608/64 Scope:Link
  5.           UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  6.           RX packets:4867 errors:0 dropped:0 overruns:0 frame:0
  7.           TX packets:585 errors:0 dropped:0 overruns:0 carrier:0
  8.           collisions:0 txqueuelen:1000
  9.           RX bytes:473730 (462.6 KiB) TX bytes:91553 (89.4 KiB)

分发文件

  1. [[email protected] ~]$ cp /etc/hosts .
  2. hosts
  3. hosts 100% 243 0.2KB/s 00:00

 

  1. [[email protected] ~]# ls /home/oldgirl/
  2. hosts

编写批量分发脚本

  1. [[email protected] ~]$ cat fenfa.sh
  2. scp -P22 hosts [email protected]:~
  3. scp -P22 hosts [email protected]:~
  4. scp -P22 hosts [email protected]:~

 

  1. [[email protected] ~]$ sh fenfa.sh
  2. hosts 100% 243 0.2KB/s 00:00
  3. hosts 100% 243 0.2KB/s 00:00
  4. hosts 100% 243 0.2KB/s 00:00

 

  1. [[email protected] ~]$ cat fenfa.sh
  2. #!/bin/sh
  3. for n in 132 133 134
  4. do
  5.   scp -P22 hosts [email protected]$n:~
  6. done

 

  1. [[email protected] ~]$ cat fenfa.sh
  2. #!/bin/sh
  3. . /etc/init.d/functions
  4. for n in 132 133 134
  5. do
  6.   scp -P22 1oldgirl@192.168.31.[email protected]:~ &>/dev/null
  7.   if [ $? -eq 0 ]
  8.     then
  9.       action "fenfa $1 ok" /bin/true
  10.   else
  11.       action "fenfa $1 error" /bin/false
  12.   fi
  13. done

 

  1. [[email protected] ~]$ sh fenfa.sh hosts
  2. fenfa hosts ok [ OK ]
  3. fenfa hosts ok [ OK ]
  4. fenfa hosts ok [ OK ]

 

  1. [[email protected] ~]$ cp /server/scripts/inotify.sh .
  2. [[email protected] ~]$ sh fenfa.sh inotify.sh
  3. fenfa inotify.sh ok [ OK ]
  4. fenfa inotify.sh ok [ OK ]
  5. fenfa inotify.sh ok [ OK ]

 

  1. [[email protected] ~]$ cat fenfa.sh
  2. #!/bin/sh
  3. . /etc/init.d/functions
  4. if [ $# -ne 1 ]
  5.   then
  6.     echo "USAGE:$0 {FILENAME|DIRNAME}"
  7.     exit 1
  8. fi
  9. for n in 132 133 134
  10. do
  11.   scp -P22 -r 1oldgirl@192.168.31.[email protected]:~ &>/dev/null
  12.   if [ $? -eq 0 ]
  13.     then
  14.       action "fenfa $1 ok" /bin/true
  15.   else
  16.       action "fenfa $1 error" /bin/false
  17.   fi
  18. done

 

  1. [[email protected] ~]$ sh fenfa.sh
  2. USAGE:fenfa.sh {FILENAME|DIRNAME}
  3. [[email protected] ~]$ cp -r /data/ .
  4. [[email protected] ~]$ sh fenfa.sh data/
  5. fenfa data/ ok [ OK ]
  6. fenfa data/ ok [ OK ]
  7. fenfa data/ ok [ OK ]

 

  1. [[email protected] ~]$ cat view.sh
  2. #!/bin/sh
  3.  
  4. if [ $# -ne 1 ]
  5.   then
  6.     echo "USAGE:$0 COMMAND"
  7.     exit 1
  8. fi
  9. for n in 132 133 134
  10. do
  11.   ssh -p22 [email protected]nn1
  12. done

 

  1. [[email protected] ~]$ sh view.sh
  2. USAGE:view.sh COMMAND
  3. [[email protected] ~]$ sh view.sh "cat /etc/redhat-release"
  4. CentOS release 6.6 (Final)
  5. CentOS release 6.6 (Final)
  6. CentOS release 6.6 (Final)

ssh批量分发与管理

1、利用root做ssh key验证。

优点:简单、易用。

缺点:安全差,同时无法禁止root远程连接这个功能。

2、利用普通用户(推荐)

先把分发的文件拷贝到服务器用户家目录,然后sudo提权拷贝分发的文件。

优点:安全。无需停止root远程连接这个功能。

缺点:配置比较复杂。

3、设置suid对固定命令

优点:相对安全

缺点:复杂,安全性较差。任何人都可以处理带有suid权限的命令。

  1. [[email protected] ~]# echo ‘oldgirl ALL=(ALL) NOPASSWD:/usr/bin/rsync‘ >>/etc/sudoers
  2. [[email protected] ~]# visudo -c
  3. /etc/sudoers: parsed OK
  4. [[email protected] ~]# grep oldgirl /etc/sudoers
  5. oldgirl ALL=(ALL) NOPASSWD:/usr/bin/rsync

 

  1. [[email protected] ~]$ scp -P22 -r hosts [email protected]:~
  2. hosts 100% 255 0.3KB/s 00:00
  3. [[email protected] ~]$ ssh -t [email protected] sudo rsync hosts /etc/
  4. Connection to 192.168.31.133 closed.

 

  1. [[email protected] ~]$ cat fenfa_good.sh
  2. #!/bin/sh
  3. . /etc/init.d/functions
  4. if [ $# -ne 2 ]
  5.   then
  6.     echo "USAGE:$0 {FILENAME|DIRNAME} REMOTEDIR"
  7.     exit 1
  8. fi
  9. for n in 132 133 134
  10. do
  11.   scp -P22 -r 1oldgirl@192.168.31.[email protected]:~ &>/dev/null &&\
  12.   ssh -t [email protected]nsudorsyncnsudorsync1 $2
  13.   if [ $? -eq 0 ]
  14.     then
  15.       action "fenfa $1 ok" /bin/true
  16.   else
  17.       action "fenfa $1 error" /bin/false
  18.   fi
  19. done
  20. [[email protected] ~]$ sh fenfa_good.sh
  21. USAGE:fenfa_good.sh {FILENAME|DIRNAME} REMOTEDIR
  22. [[email protected] ~]$ sh fenfa_good.sh hosts /etc/
  23. Connection to 192.168.31.132 closed.
  24. fenfa hosts ok [ OK ]
  25. Connection to 192.168.31.133 closed.
  26. fenfa hosts ok [ OK ]
  27. Connection to 192.168.31.134 closed.
  28. fenfa hosts ok [ OK ]

 

  1. [[email protected] ~]# ll `which rsync`
  2. -rwxr-xr-x. 1 root root 414968 Apr 30 2014 /usr/bin/rsync
  3. [[email protected] ~]# chmod 4755 /usr/bin/rsync
  4. [[email protected] ~]# ll `which rsync`
  5. -rwsr-xr-x. 1 root root 414968 Apr 30 2014 /usr/bin/rsync

企业级生产场景批量管理,自动化管理方案:

1、最简单最常见shh key,功能最强大的。一般中小型企业会用,50—100台以下。

2、门户级别puppet批量管理工具。

3、saltstack批量管理工具。

4、http+cron

批量管理路线:sshkeyàpuppetàsaltstack/ansible。

以上是关于SSH的主要内容,如果未能解决你的问题,请参考以下文章

PyCharm SSH 隧道通过本地 ssh 配置 (~/.ssh/config)

salt-ssh与ssh的区别

ssh 命令怎么用?

ssh(不是openssh) 做无密码登陆,怎么每次ssh 登陆还是要输入密码呀?

万字详解SSH(SSH登录原理+SSH配置+模拟实现SSH免密登录)

SSH服务审计工具ssh-audit