存放路径
vim /etc/ssh/sshd_config
修改参数
Port xx #端口
ListenAddress x.x.x.x:x # 监听的地址
PermitRootLogin no # root连接
PermitEmptyPasswords no #空密码登陆
相信很多朋友连接ssh卡顿很久下面设置解决连接慢问题
GSSAPICleanupCredentials no #ssh连接慢问题
UseDNS no
一条命令实现
cd /etc/ssh/
sed -ir "13iPort 5566\nPermitRootLogin no\nPermitEmptyPasswords
no\nGSSAPICleanupCredentials no\nUseDNS no" sshd_config
重启service sshd restart 生效
ssh批量管理分发项目实践与介绍
实现这个需求
1、创建用户及密码
useradd yaodunlin
echo 123456|passwd --stdin yaodunlin
id yaodunlin
su - yaodunlin
(所有的机器都要执行)
2、创建秘钥
ssh-keygen -t dsa (一路回车)
一键生成秘钥对 不用按回车
方法一:ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa>/dev/null 2>&1
方法二:echo -e "\n"|ssh-keygen -t dsa -N ""
3、管理机分发送公钥到各机器
方法:没改22端口:ssh-copy-id -i .ssh/id_dsa.pub [email protected]
<mailto:[email protected]>
带更改端口的这么写:ssh-copy-id -i .ssh/id_dsa.pub "-p8888 [email protected]"
拷贝过去就可以查看对方的资源
ssh -p22 [email protected] /sbin/ifconfig ens33
4、测试
[yaodunlin@test-22 ~]$ cat view_ip.sh
#!/bin/sh
if [ $# -ne 1 ];then
echo "USAGE:/bin/sh$0 ARG1 "
exit 1
fi
for n in 43 186 192
do
echo ==========192.168.1.$n=========
ssh -p22 [email protected].$n "$1"
done
查看所有机器版本
成功标志!链接所有的机器,不提示密码就可以。
实现批量分发
先配置hosts
cat >/etc/hosts<<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.22 test-22
192.168.1.43 test-43
192.168.1.186 test-186
192.168.1.192 test-192
EOF
拷贝分发没有权限
企业里实现ssh方案:3种方法
1、直接root ssh key
条件:允许root ssh登录 (前面优化都禁止root登录,这条行不通)
2、sudo提权实现拷贝没有权限用户拷贝, 每一台的用户都要授权
第一步: visudo 最后一行配置用户授权
或者用追加进去
echo "yaodunlin ALL= NOPASSWD: /usr/bin/rsync">>/etc/sudoers
visudo -c (检查语法)
第二步
先拷贝到目标:
scp -P22 hosts [email protected]:~
远程sudo:
ssh -P22 -t [email protected] sudo rsync ~/hosts /etc/hosts
3、利用suid实现拷贝(做思维扩展了解,工作中不要用很危险)
第一步:先拷到家目录下面
目标机查看结果:
第二步:
ssh -p22 [email protected] rsync ~/hosts /etc/hosts 或cp (事先做个备份)
这里会报个错,未授权
此时要在目标机suid授权
ls -l `which rsync`
chmod u+s `which rsync`
结果
实现批量分发管理:
脚本1、
vim hosts.sh
#!/bin/sh
for n in 43 186 192
do
scp -P22 ~/hosts [email protected].${n}:~
ssh -P22 -t [email protected].${n} sudo rsync /etc/hosts /etc/hosts_$(date
+%F-%H-%M-%S).bak
ssh -P22 -t [email protected].${n} sudo rsync ~/hosts /etc/hosts
done
脚本2、
vim fenfa_file.sh
#!/bin/sh
. /etc/init.d/functions
for n in 43 186 192
do
scp -P22 ~/hosts [email protected].${n}:~ >/dev/null 2>&1&&\
ssh -P22 -t [email protected].${n} sudo rsync ~/hosts /etc/hosts >/dev/null
2>&1
if [ $? -eq 0 ];then
action "fenfa hosts 192.168.1.$n" /bin/true
else
action "fenfa hosts 192.168.1.$n" /bin/false
fi
done
脚本3、分发任意文件到任意目录
[yaodunlin@test-22 ~]$ cat fenfa_renyifile.sh
#!/bin/sh
if [ $# -ne 2 ];then
echo "USAGE:$0 ARG1 ARG2 "
exit 1
fi
. /etc/init.d/functions
for n in 43 186 192
do
scp -P22 ~/$1 [email protected].${n}:~ >/dev/null 2>&1&&\
ssh -P22 -t [email protected].${n} sudo rsync ~/$1 $2 >/dev/null 2>&1
if [ $? -eq 0 ];then
action "fenfa hosts 192.168.1.$n" /bin/true
else
action "fenfa hosts 192.168.1.$n" /bin/false
fi
done
增量更新
rsync -avz hosts -e 'ssh -p22'[email protected]:~
1、增量
2、加密
批量管理工具:
热门工具 换一换