zoukankan      html  css  js  c++  java
  • Linux服务器之SSH

    SSH

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

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

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

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

    SSH服务认证类型

    基于口令的安全验证

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

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

    -p接端口,默认22端口

    @前面为用户名

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

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

    ssh总结:

    1、切换到别的机器上ssh -p52113 user@ip。

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

    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. [root@server ~]# scp -P22 -r -p /etc root@192.168.31.132:/tmp
    2. root@192.168.31.132'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 root@192.168.31.132

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

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

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

    批量分发文件、执行命令

    配置ip主机名

    1. [root@server ~]# 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. [root@backup data]# useradd oldgirl
    2. [root@backup data]# echo system|passwd --stdin oldgirl
    3. Changing password for user oldgirl.
    4. passwd: all authentication tokens updated successfully.

    创建公私钥

    1. [root@server data]# su - oldgirl
    2. [oldgirl@server ~]$ ssh
    3. ssh ssh-agent sshd ssh-keyscan
    4. ssh-add ssh-copy-id ssh-keygen
    5. [oldgirl@server ~]$ ssh-keygen -t dsa #dsa
    6. Generating public/private dsa key pair.
    7. Enter file in which to save the key (/home/oldgirl/.ssh/id_dsa): #存放密钥的路径
    8. Created directory '/home/oldgirl/.ssh'.
    9. Enter passphrase (empty for no passphrase):
    10. Enter same passphrase again:
    11. Your identification has been saved in /home/oldgirl/.ssh/id_dsa.
    12. Your public key has been saved in /home/oldgirl/.ssh/id_dsa.pub.
    13. The key fingerprint is:
    14. 5a:64:22:18:c1:4c:70:ea:dd:64:9d:82:81:0b:99:07 oldgirl@server
    15. The key's randomart image is:
    16. +--[ DSA 1024]----+
    17. |EO*o |
    18. |+++oo . . |
    19. |o.o..+.oo |
    20. |... +..+ |
    21. | . . . S |
    22. | o |
    23. | . |
    24. | |
    25. | |
    26. +-----------------+
    1. [oldgirl@server ~]$ 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. [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub oldgirl@182.168.31.134 #默认22端口
    2. ssh: connect to host 182.168.31.134 port 22: Connection refused
    3. [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub oldgirl@192.168.31.134
    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. oldgirl@192.168.31.134's password:
    9. Now try logging into the machine, with "ssh 'oldgirl@192.168.31.134'", 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. [root@lnmp ~]# su - oldgirl
    2. [oldgirl@lnmp ~]$ 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 oldgirl@192.168.31.134"

    1. [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub oldgirl@192.168.31.133
    2. [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub oldgirl@192.168.31.132

    测试免密查询ip

    1. [oldgirl@server ~]$ ssh -P22 oldgirl@192.168.31.134 /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. [oldgirl@server ~]$ cp /etc/hosts .
    2. [oldgirl@server ~]$ ls
    3. hosts
    4. [oldgirl@server ~]$ scp -P22 hosts oldgirl@192.168.31.133:~
    5. hosts 100% 243 0.2KB/s 00:00
    1. [root@lamp ~]# ls /home/oldgirl/
    2. hosts

    编写批量分发脚本

    1. [oldgirl@server ~]$ cat fenfa.sh
    2. scp -P22 hosts oldgirl@192.168.31.132:~
    3. scp -P22 hosts oldgirl@192.168.31.133:~
    4. scp -P22 hosts oldgirl@192.168.31.134:~
    1. [oldgirl@server ~]$ 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. [oldgirl@server ~]$ cat fenfa.sh
    2. #!/bin/sh
    3. for n in 132 133 134
    4. do
    5.   scp -P22 hosts oldgirl@192.168.31.$n:~
    6. done
    1. [oldgirl@server ~]$ 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.1oldgirl@192.168.31.n:~ &>/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. [oldgirl@server ~]$ sh fenfa.sh hosts
    2. fenfa hosts ok [ OK ]
    3. fenfa hosts ok [ OK ]
    4. fenfa hosts ok [ OK ]
    1. [oldgirl@server ~]$ cp /server/scripts/inotify.sh .
    2. [oldgirl@server ~]$ sh fenfa.sh inotify.sh
    3. fenfa inotify.sh ok [ OK ]
    4. fenfa inotify.sh ok [ OK ]
    5. fenfa inotify.sh ok [ OK ]
    1. [oldgirl@server ~]$ 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.1oldgirl@192.168.31.n:~ &>/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. [oldgirl@server ~]$ sh fenfa.sh
    2. USAGE:fenfa.sh {FILENAME|DIRNAME}
    3. [oldgirl@server ~]$ cp -r /data/ .
    4. [oldgirl@server ~]$ sh fenfa.sh data/
    5. fenfa data/ ok [ OK ]
    6. fenfa data/ ok [ OK ]
    7. fenfa data/ ok [ OK ]
    1. [oldgirl@server ~]$ 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 oldgirl@192.168.31.nn1
    12. done
    1. [oldgirl@server ~]$ sh view.sh
    2. USAGE:view.sh COMMAND
    3. [oldgirl@server ~]$ 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. [root@lnmp ~]# echo 'oldgirl ALL=(ALL) NOPASSWD:/usr/bin/rsync' >>/etc/sudoers
    2. [root@lnmp ~]# visudo -c
    3. /etc/sudoers: parsed OK
    4. [root@lnmp ~]# grep oldgirl /etc/sudoers
    5. oldgirl ALL=(ALL) NOPASSWD:/usr/bin/rsync
    1. [oldgirl@server ~]$ scp -P22 -r hosts oldgirl@192.168.31.133:~
    2. hosts 100% 255 0.3KB/s 00:00
    3. [oldgirl@server ~]$ ssh -t oldgirl@192.168.31.133 sudo rsync hosts /etc/
    4. Connection to 192.168.31.133 closed.
    1. [oldgirl@server ~]$ 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.1oldgirl@192.168.31.n:~ &>/dev/null &&
    12.   ssh -t oldgirl@192.168.31.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. [oldgirl@server ~]$ sh fenfa_good.sh
    21. USAGE:fenfa_good.sh {FILENAME|DIRNAME} REMOTEDIR
    22. [oldgirl@server ~]$ 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. [root@lnmp ~]# ll `which rsync`
    2. -rwxr-xr-x. 1 root root 414968 Apr 30 2014 /usr/bin/rsync
    3. [root@lnmp ~]# chmod 4755 /usr/bin/rsync
    4. [root@lnmp ~]# 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。

  • 相关阅读:
    来电科技:基于Flink+Hologres的实时数仓演进之路
    实时计算 Flink 版总体介绍
    阿里云江岑:云原生在边缘形态下的升华
    xshell帮助
    版本控制工具之git
    批量修改ubuntu用户sudo免密码
    matplotlib按钮控制图像显示
    为ssh主机设置别名
    VScode正则表达式批量删除字符串
    SQL Server 操作XML数据
  • 原文地址:https://www.cnblogs.com/chengjian-physique/p/8170193.html
Copyright © 2011-2022 走看看