1 SSH
SSH是安全的加密协议,用于远程连接linux服务器。
SSH默认端口是22,有SSH1(有漏洞)、SSH2 两个版本。
SSH服务端主要包含两个服务功能SSH远程连接,SFTP服务。
Linux SSH客户端包含SSH远程连接命令,以及远程拷贝SCP命令等。
1.1 ssh,scp,sftp
ssh小结:
1、切换到别的机器上 ssh -p52113 user@ip ([user@hostname [command])
2、到其他机器执行命令(不会切到机器上),ssh -p52113 user@ip 全路径命令
3、当第一次ssh连接的时候,本地会产生一个密钥文件 ~/.ssh/known_host
scp小结:
1、scp是加密的远程拷贝,而cp仅为本地拷贝
2、可以把数据从一台机器推送到另一台,也可以从其他机器把数据拉回到本地执行命令的机器
3、每次都是全量完整拷贝,因此,效率不高,适合第一次拷贝用,如果需要增量拷贝,用rsync
sftp小结:
1、linux下连接命令 sftp -oPort=52113 [email protected]
2、上传put加客户端本地路径 put /etc/hosts
也可以指定路径上传 put /etc/hosts /tmp
3、下载get服务端的内容 get hosts,linux下载到本地连接前的目录
也可以指定路径下载, get hosts /etc/
4、链接到的远端家目录为默认目录,也可以切换到其他有权限的目录下。
1.2 SSH重点小结
1、SSH为加密的远程连接协议。相关软件有openssh,openssl
2、默认端口22
3、协议版本1.x和2.x,2.x更安全,了解SSH协议原理
4、服务端SSH远程连接服务,sftp服务。sshd守护进程,开机要自启动
5、SSH客户端包含ssh,scp,sftp命令
6、SSH安全验证方式:口令和密钥,这两种都是基于口令的,理解SSH密钥登录的原理
7、SSH服务安全优化,修改默认端口22,禁止root远程连接,禁止DNS,SSH只监听内网IP
8、SSH密钥对,公钥在服务器端,锁;私钥在客户端,钥匙
1.3 命令实例
ssh -p52113 [email protected]
ssh -p52113 [email protected] /sbin/ifconfig eth0 连接到远端执行命令,但是不连过去
.ssh/known_hosts里面存放的是RSA密钥信息
scp -P52113 -r -p /data/ [email protected]:/tmp/ 拷贝到远端
-r 递归 -p 保持属性
sftp -oPort=52113 [email protected] 登录到远端
put /data/test.txt /tmp/ 上传到/tmp/
get test.txt /tmp/ 下载到/tmp/
1.4 批量管理批量分发集群方案
1、批量分发
2、批量部署,执行命令
3、批量配置管理
创建用户
useradd wangxin echo 123456|passwd --stdin wangxin
创建密钥
ssh-keygen -t dsa [wangxin@nfs-server ~]$ ls -l .ssh/ -rw------- 1 wangxin incahome 668 6月 11 16:11 id_dsa <========私钥 -rw-r--r-- 1 wangxin incahome 608 6月 11 16:11 id_dsa.pub <======公钥
将公钥分发给其他主机
[wangxin@nfs-server ~]$ ssh-copy-id -i .ssh/id_dsa.pub "-p 52113 [email protected]" The authenticity of host '[192.168.4.121]:52113 ([192.168.4.121]:52113)' can't be established. RSA key fingerprint is 4c:f0:cf:b1:6f:b4:bf:d6:62:7f:64:07:7a:10:fa:55. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.4.121]:52113' (RSA) to the list of known hosts. [email protected]'s password: Now try logging into the machine, with "ssh '-p 52113 [email protected]'", and check in: .ssh/authorized_keys 《========公钥分发后改名字了 to make sure we haven't added extra keys that you weren't expecting.
验证公钥有没有分发成功
[wangxin@nfs-server ~]$ ssh -p52113 [email protected] /sbin/ifconfig eth0
分发hosts
scp -P52113 hosts [email protected]:~ 分发到wangxin的家目录
密钥的权限一定不能改变,若改变了就不能免密认证了
1.5 利用脚本实现多台机器分发
批量分发sshkey
[root@nfs-server ~]# cat /server/scripts/fenfa.sh #!/bin/sh . /etc/init.d/functions if [ $# -ne 1 ] then echo "USAGE:$0 {FILENAME|DIRNAME}" exit 1 fi for n in 121 122 123 do scp -P52113 –r $1 [email protected].$n:~ &>/dev/null if [ $? -eq 0 ] then action "fenfa $1 ok" /bin/true else action "fenfa $1 ok" /bin/false fi done
验证分发是否成功
[root@nfs-server ~]# cat /server/scripts/view.sh #!/bin/sh if [ $# -ne 1 ] then echo "USAGE:$0 COMMAND" exit 1 fi for n in 121 122 123 do ssh -p52113 [email protected].$n $1 done
expect语言 解决非交互式的问题
[root@nfs-server ~]# yum install expect -y [root@nfs-server ~]# which expect /usr/bin/expect [root@nfs-server scripts]# expect fenfa_sshkey.exp
批量分发脚本 exp
[root@nfs-fenfa ~]# cat /server/scripts/fenfa_sshkey.exp #!/usr/bin/expect if { $argc != 2 } { send_user "usage: expect fenfa_sshkey.exp file host\n" exit } set file [lindex $argv 0] set host [lindex $argv 1] set password "123456" spawn ssh-copy-id -i $file "-p 9923 $host" expect { "yes/no" {send "yes\r";exp_continue} "*password" {send "$password\r"} } expect eof
批量分发脚本 shell
[root@nfs-fenfa ~]# cat /server/scripts/fenfa_sshkey.sh #!/bin/sh . /etc/init.d/functions
#如果没有密钥文件自动创建
[ ! -s ~/.ssh/id_dsa.pub ] && ssh-keygen -t dsa –P ' ' -f ~/.ssh/id_dsa >/dev/null 2>&1 for ip in `cat /server/scripts/iplist.txt` do expect /server/scripts/fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip >/dev/null 2>&1 if [ $? -eq 0 ] then action "fenfa $ip" /bin/true else action "fenfa $ip" /bin/false fi done iplist [root@nfs-fenfa ~]# cat /server/scripts/iplist.txt 192.168.4.185 192.168.4.186
1.6 邮件服务
[root@backup ~]# /etc/init.d/postfix restart 关闭 postfix: [确定] 启动 postfix: [确定] [root@backup ~]# lsof -i :25 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME master 3157 root 12u IPv4 19418 0t0 TCP localhost:smtp (LISTEN) master 3157 root 13u IPv6 19420 0t0 TCP localhost:smtp (LISTEN) mail -s "wangxin title" [email protected] </etc/hosts mailq 查看邮件队列是否有邮件,是否发送成功
发送邮件提醒分发成功或者失败
ssh -t [email protected].$n sudo rsync $1 $2 &>/dev/null if [ $? -eq 0 ] then echo "$1 to $2 0.$n is ok" >>/tmp/ok.log else echo "$1 to $2 0.$n is fail" >>/tmp/fail.log fi if [ -s /tmp/fail.log ] then mail -s "$(date +%F\ %T) fenfa hosts" [email protected] </tmp/fail.log >/tmp/ok.log >/tmp/fail.log fi