SSH 1、ssh是安全的加密協議,用於遠程連接linux伺服器。 2、ssh預設埠是22,安全協議版本ssh2。 3、ssh服務端主要包含兩個服務功能ssh遠程連接,sftp服務。 4、linux ssh客戶端包含ssh遠程連接命令,以及遠程拷貝scp命令等。 SSH服務認證類型 基於口令的安全... ...
SSH
1、ssh是安全的加密協議,用於遠程連接linux伺服器。
2、ssh預設埠是22,安全協議版本ssh2。
3、ssh服務端主要包含兩個服務功能ssh遠程連接,sftp服務。
4、linux ssh客戶端包含ssh遠程連接命令,以及遠程拷貝scp命令等。
SSH服務認證類型
基於口令的安全驗證
- [root@server ~]# ssh -p22 [email protected]
- The authenticity of host '192.168.31.132 (192.168.31.132)' can't be established.
- RSA key fingerprint is a0:60:7f:c8:e1:2c:d4:3b:2c:63:b7:3d:66:ad:f2:18.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.31.132' (RSA) to the list of known hosts.
- [email protected]'s password:
- Last login: Mon Feb 6 13:33:19 2017 from 192.168.31.1
- [root@backup ~]#
ssh連接遠程主機命令的基本語法:
-p接埠,預設22埠
@前面為用戶名
@後面為要連接的伺服器ip
- [root@server ~]# cat ~/.ssh/known_hosts
- 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登錄配置
- #更改前備份
- cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori
- vi /etc/ssh/sshd_config
- Port 52113 #預設22埠,為了提高安全級別建議修改
- PermitRootLogin no #root超級用戶禁止遠程登錄
- PermiEmptyPasswords no #禁止空密碼登錄
- UseDNS no #不使用DNS
- GSSAPIAuthenication no
ssh客戶端附帶的遠程拷貝scp命令
- [root@server ~]# scp -P22 -r -p /etc [email protected]:/tmp
- [email protected]'s password:
- system-release-cpe 100% 25 0.0KB/s 00:00
- 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主機名
- [root@server ~]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.31.128 server
- 192.168.31.134 lnmp
- 192.168.31.133 lamp
- 192.168.31.132 backup
IT公司企業級批量分發、管理方案
1、中小企業基本sshkey密鑰的方案。
2、門戶網站sina puppet(複製、太重)。
3、趕集、小米saltstack批量管理(輕量)。
創建用戶oldgirl,密碼system
- [root@backup data]# useradd oldgirl
- [root@backup data]# echo system|passwd --stdin oldgirl
- Changing password for user oldgirl.
- passwd: all authentication tokens updated successfully.
創建公私鑰
- [root@server data]# su - oldgirl
- [oldgirl@server ~]$ ssh
- ssh ssh-agent sshd ssh-keyscan
- ssh-add ssh-copy-id ssh-keygen
- [oldgirl@server ~]$ ssh-keygen -t dsa #dsa
- Generating public/private dsa key pair.
- Enter file in which to save the key (/home/oldgirl/.ssh/id_dsa): #存放密鑰的路徑
- Created directory '/home/oldgirl/.ssh'.
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /home/oldgirl/.ssh/id_dsa.
- Your public key has been saved in /home/oldgirl/.ssh/id_dsa.pub.
- The key fingerprint is:
- 5a:64:22:18:c1:4c:70:ea:dd:64:9d:82:81:0b:99:07 oldgirl@server
- The key's randomart image is:
- +--[ DSA 1024]----+
- |EO*o |
- |+++oo . . |
- |o.o..+.oo |
- |... +..+ |
- | . . . S |
- | o |
- | . |
- | |
- | |
- +-----------------+
- [oldgirl@server ~]$ ls -l .ssh/
- total 8
- -rw-------. 1 oldgirl oldgirl 668 Feb 7 09:41 id_dsa #私鑰
- -rw-r--r--. 1 oldgirl oldgirl 604 Feb 7 09:41 id_dsa.pub #公鑰
分發公鑰
- [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected] #預設22埠
- ssh: connect to host 182.168.31.134 port 22: Connection refused
- [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected]
- The authenticity of host '192.168.31.134 (192.168.31.134)' can't be established.
- RSA key fingerprint is c1:0f:e0:45:05:79:c9:f0:48:d3:2f:6b:dc:66:6a:fe.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.31.134' (RSA) to the list of known hosts.
- [email protected]'s password:
- Now try logging into the machine, with "ssh '[email protected]'", and check in:
- .ssh/authorized_keys
- to make sure we haven't added extra keys that you weren't expecting.
- [root@lnmp ~]# su - oldgirl
- [oldgirl@lnmp ~]$ ls -l .ssh/
- total 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]"
- [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected]
- [oldgirl@server ~]$ ssh-copy-id -i .ssh/id_dsa.pub [email protected]
測試免密查詢ip
- [oldgirl@server ~]$ ssh -P22 [email protected] /sbin/ifconfig eth0
- eth0 Link encap:Ethernet HWaddr 00:0C:29:03:06:08
- inet addr:192.168.31.134 Bcast:192.168.31.255 Mask:255.255.255.0
- inet6 addr: fe80::20c:29ff:fe03:608/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
- RX packets:4867 errors:0 dropped:0 overruns:0 frame:0
- TX packets:585 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:473730 (462.6 KiB) TX bytes:91553 (89.4 KiB)
分發文件
- [oldgirl@server ~]$ cp /etc/hosts .
- [oldgirl@server ~]$ ls
- hosts
- [oldgirl@server ~]$ scp -P22 hosts [email protected]:~
- hosts 100% 243 0.2KB/s 00:00
- [root@lamp ~]# ls /home/oldgirl/
- hosts
編寫批量分發腳本
- [oldgirl@server ~]$ cat fenfa.sh
- scp -P22 hosts [email protected]:~
- scp -P22 hosts [email protected]:~
- scp -P22 hosts [email protected]:~
- [oldgirl@server ~]$ sh fenfa.sh
- hosts 100% 243 0.2KB/s 00:00
- hosts 100% 243 0.2KB/s 00:00
- hosts 100% 243 0.2KB/s 00:00
- [oldgirl@server ~]$ cat fenfa.sh
- #!/bin/sh
- for n in 132 133 134
- do
- scp -P22 hosts [email protected].$n:~
- done
- [oldgirl@server ~]$ cat fenfa.sh
- #!/bin/sh
- . /etc/init.d/functions
- for n in 132 133 134
- do
- scp -P22 $1 [email protected].$n:~ &>/dev/null
- if [ $? -eq 0 ]
- then
- action "fenfa $1 ok" /bin/true
- else
- action "fenfa $1 error" /bin/false
- fi
- done
- [oldgirl@server ~]$ sh fenfa.sh hosts
- fenfa hosts ok [ OK ]
- fenfa hosts ok [ OK ]
- fenfa hosts ok [ OK ]
- [oldgirl@server ~]$ cp /server/scripts/inotify.sh .
- [oldgirl@server ~]$ sh fenfa.sh inotify.sh
- fenfa inotify.sh ok [ OK ]
- fenfa inotify.sh ok [ OK ]
- fenfa inotify.sh ok [ OK ]
- [oldgirl@server ~]$ cat fenfa.sh
- #!/bin/sh
- . /etc/init.d/functions
- if [ $# -ne 1 ]
- then
- echo "USAGE:$0 {FILENAME|DIRNAME}"
- exit 1
- fi
- for n in 132 133 134
- do
- scp -P22 -r $1 [email protected].$n:~ &>/dev/null
- if [ $? -eq 0 ]
- then
- action "fenfa $1 ok" /bin/true
- else
- action "fenfa $1 error" /bin/false
- fi
- done
- [oldgirl@server ~]$ sh fenfa.sh
- USAGE:fenfa.sh {FILENAME|DIRNAME}
- [oldgirl@server ~]$ cp -r /data/ .
- [oldgirl@server ~]$ sh fenfa.sh data/
- fenfa data/ ok [ OK ]
- fenfa data/ ok [ OK ]
- fenfa data/ ok [ OK ]
- [oldgirl@server ~]$ cat view.sh
- #!/bin/sh
- if [ $# -ne 1 ]
- then
- echo "USAGE:$0 COMMAND"
- exit 1
- fi
- for n in 132 133 134
- do
- ssh -p22 [email protected].$n $1
- done
- [oldgirl@server ~]$ sh view.sh
- USAGE:view.sh COMMAND
- [oldgirl@server ~]$ sh view.sh "cat /etc/redhat-release"
- CentOS release 6.6 (Final)
- CentOS release 6.6 (Final)
- CentOS release 6.6 (Final)
ssh批量分發與管理
1、利用root做ssh key驗證。
優點:簡單、易用。
缺點:安全差,同時無法禁止root遠程連接這個功能。
2、利用普通用戶(推薦)
先把分發的文件拷貝到伺服器用戶家目錄,然後sudo提權拷貝分發的文件。
優點:安全。無需停止root遠程連接這個功能。
缺點:配置比較複雜。
3、設置suid對固定命令
優點:相對安全
缺點:複雜,安全性較差。任何人都可以處理帶有suid許可權的命令。
- [root@lnmp ~]# echo 'oldgirl ALL=(ALL) NOPASSWD:/usr/bin/rsync' >>/etc/sudoers
- [root@lnmp ~]# visudo -c
- /etc/sudoers: parsed OK
- [root@lnmp ~]# grep oldgirl /etc/sudoers
- oldgirl ALL=(ALL) NOPASSWD:/usr/bin/rsync
- [oldgirl@server ~]$ scp -P22 -r hosts [email protected]:~
- hosts 100% 255 0.3KB/s 00:00
- [oldgirl@server ~]$ ssh -t [email protected] sudo rsync hosts /etc/
- Connection to 192.168.31.133 closed.
- [oldgirl@server ~]$ cat fenfa_good.sh
- #!/bin/sh
- . /etc/init.d/functions
- if [ $# -ne 2 ]
- then
- echo "USAGE:$0 {FILENAME|DIRNAME} REMOTEDIR"
- exit 1
- fi
- for n in 132 133 134
- do
- scp -P22 -r $1 [email protected].$n:~ &>/dev/null &&\
- ssh -t [email protected].$n sudo rsync $1 $2
- if [ $? -eq 0 ]
- then
- action "fenfa $1 ok" /bin/true
- else
- action "fenfa $1 error" /bin/false
- fi
- done
- [oldgirl@server ~]$ sh fenfa_good.sh
- USAGE:fenfa_good.sh {FILENAME|DIRNAME} REMOTEDIR
- [oldgirl@server ~]$ sh fenfa_good.sh hosts /etc/
- Connection to 192.168.31.132 closed.
- fenfa hosts ok [ OK ]
- Connection to 192.168.31.133 closed.
- fenfa hosts ok [ OK ]
- Connection to 192.168.31.134 closed.
- fenfa hosts ok [ OK ]
- [root@lnmp ~]# ll `which rsync`
- -rwxr-xr-x. 1 root root 414968 Apr 30 2014 /usr/bin/rsync
- [root@lnmp ~]# chmod 4755 /usr/bin/rsync
- [root@lnmp ~]# ll `which rsync`
- -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。