Linux Centos 服务器免密验证(ansible版/非root用户)

简介: Ansible中,-k或--ask-pass选项用于提示输入SSH密码。这在你需要通过SSH连接到目标主机,但又没有设置SSH密钥对的情况下非常有用。使用-k选项后,Ansible将在执行playbook或命令时提示你输入SSH密码。

Ansible中,-k或--ask-pass选项用于提示输入SSH密码。这在你需要通过SSH连接到目标主机,但又没有设置SSH密钥对的情况下非常有用。使用-k选项后,Ansible将在执行playbook或命令时提示你输入SSH密码。


在使用ansible的时候会遇到如果不加 -k( 通过key验证)这个参数时认证失败。

20200430223120743.png可以通过 ssh-keygen - > ssh-copy-id [-i [identity_file]] [user@]machine 把操作机的私钥添加到目标主机的密钥列表中。

ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub  root@10.50.**.**
root@10.50.10.**'s password:
Now try logging into the machine, with "ssh 'root@10.50.**.1*'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
 .ssh]# ssh 10.50.10.161
Last login: Mon May 24 10:19:54 2021 from 10.56.**.**

20200430223251696.png需求 :A 主机想通过shh 、scp...等通过ssh协议连接主机的命令来连接B主机,但是不想输入密码。


具体操作:

1、ssh-keygen  用这个命令是用来生成本机的公钥和私钥的

2、将A主机的id_rsa.pub copy到B主机上

ansible gpservers -m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root/.ssh/' -k

3、在B主机2中的的id_rsa.pub 重定向到B主机的authorized_keys(一个信任主机列表)。注意一点要用 >> 追加,以免覆盖掉其他的密钥验证。造成其他程序报错。这点很重要

ansible gpservers -m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root/.ssh/' -k

以上2 、3两步可以用ssh-copy-id(会自动将第一步中生成的id_rsa.pub添加到目标主机的authorized_keys中。) 命令来实现。如果是多个主机需要一个一个执行,用ansible批量操作还是较快。


如果需要相互信任,只需要将如上步骤逆向进行即可。


需要用到ansible的fetch模块。


1、现将gpserver的18台机器上的公钥fetch到控制机

注意fetch 回来是不会被覆盖的,fetch回来的文件是放在以ip命名的一个文件夹里面

2、重定向。


------------------------------update 2020年5月24日21:38:15--------------------------------


如若是非root用于,别于root的用户的就是需要对authorized_keys 赋权。


默认是664

ssh]$ ll
total 16
-rw-rw-r-- 1 gpadmin gpadmin  796 May 24 21:35 authorized_keys
-rw------- 1 gpadmin gpadmin 1675 May 24 21:17 id_rsa
-rw-r--r-- 1 gpadmin gpadmin  398 May 24 21:17 id_rsa.pub
-rw-r--r-- 1 gpadmin gpadmin  403 May 22 22:00 known_hosts

非root用户ssh 自己需要710权限。这一点容易忽视,请注意

ssh]$ ll
total 16
-rwx--x--- 1 gpadmin gpadmin  796 May 24 21:35 authorized_keys
-rw------- 1 gpadmin gpadmin 1675 May 24 21:17 id_rsa
-rw-r--r-- 1 gpadmin gpadmin  398 May 24 21:17 id_rsa.pub
-rw-r--r-- 1 gpadmin gpadmin  403 May 22 22:00 known_hosts

20210412092243497.png如果希望ssh公钥生效需满足至少下面两个条件:

    1) .ssh目录的权限必须是700

     2) .ssh/authorized_keys文件权限必须是600


---------------update 2021年7月28日16:47:52


在建立hadoop集群时,做免密验证时有如下报错


centos7

 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop102
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

这个配置需要修改

1. /etc/ssh/sshd_config
2. PasswordAuthentication yes
~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.56.12
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.12's password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.56.12'"
and check to make sure that only the key(s) you wanted were added.
[root@hadoop100 ~]#
[root@hadoop100 ~]#
[root@hadoop100 ~]# ssh hadoop102
Last login: Wed Jul 28 07:57:20 2021 from 192.168.56.12
[root@hadoop102 ~]# exit

--update 2022年1月16日22:59:00

使用expect 多进程完成免密验证

注意点:

1. ip.txt 中的内容

192.168.56.11 ninesun0318 
192.168.56.12 ninesun0318
8.142.104.169 ro....

2. expect交互那块,根据实际情况修改

注意EOF 时不要随意敲空格,一定要用table键,send完消息之后一定要使用\r 回车。

  • spawn:触发,执行命令
  • expect:识别输出
  • send:发送信息
#!/bin/bash
# 2022年1月16日22:10:51
# ninesun
while read ip;do
#判断当前主机上是否有共要文件
if [ ! -f ~/.ssh/id_rsa ];then
        ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
#从文件中ip 和密码
ipaddr=`echo $ip | awk '{print $1}'`
passwd=`echo $ip | awk '{print $2}'`
echo "$ipaddr"
echo "$passwd"
#判断是否可以ping通
{
ping -c1 -W1 $ipaddr >&/dev/null
if [ $? -eq 0 ];then
        /usr/bin/expect <<-EOF
        set timeout 10
        spawn ssh-copy-id  $ipaddr
        expect {
                #"yes/no" { send "yes\r"; exp_continue }
                "password:" { send "$passwd\r" }
        }
        expect eof
        EOF
fi
}&
done <ip.txt
wait
echo "all finished"

--update

2022年5月30日15:49:18

#!/bin/bash
# Copyright 2018 Xiak.com.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################
# 脚本用法
# passwordless.sh 192.168.1.5,192.168.1.6,192.168.1.7 your-cluster-password
# 参数说明:
#   总共两个参数,参数之间用空格隔开
#   第一个参数定义了集群的IP地址,IP之间用逗号隔开
#   第二个参数为所有集群的密码
##########################################
# 脚本只要发生错误,就终止执行
set -o errexit
# 遇到不存在的变量就会报错,并停止执行
set -o nounset
# 只要一个子命令失败,整个管道命令就失败,脚本就会终止执行
set -o pipefail
# 工作目录
root_dir="/xiak/k8s"
# 生成的 cert 文件存放目录
cert_dir="$root_dir/cert"
hosts="${HOSTS:="${1}"}"
password="${2}"
# 工具 expect
if ! (hash expect) >/dev/null 2>&1; then
    echo "=== expect command not found: Aborting ===" 1>&2
    exit 2
fi
if ! (hash ssh-copy-id) >/dev/null 2>&1; then
    echo "=== ssh-copy-id command not found: Aborting ===" 1>&2
    exit 2
fi
##########################################
# Function: SshKeyGen()
# Usage:    SshKeyGen
# Params:   Null
# Comments: 生成 id_rsa id_rsa.pub
##########################################
SshKeyGen() {
    # Delete old id_rsa
    rm -f ~/.ssh/id_rsa
    rm -f ~/.ssh/id_rsa.pub
    # Generate new id_rsa.pub
    expect -c "
        set timeout -1;
        spawn ssh-keygen -t rsa;
        expect {
            */root/.ssh/id_rsa* {send -- \r;exp_continue;}
            *passphrase):*      {send -- \r;exp_continue;}
            *again:*            {send -- \r;exp_continue;}
            eof                 {exit 0;}
        };"
}
##########################################
# Function: SshKeyGen()
# Usage:    SshKeyGen 192.168.1.2,192.168.1.3 password
# Params:
#   $1 主机列表,主机之间以逗号分隔,不能有空格
#   $2 主机的密码
# Comments:
#   1. 生成 id_rsa id_rsa.pub
#   2. 免密登录
##########################################
SshWithoutAuth() {
    SshKeyGen
    IFS=',' read -ra host_array <<< "${1}"
    for host in "${host_array[@]}";
    do
        echo "ssh-copy-id to $host"
        expect -c "set timeout -1;
            spawn ssh-copy-id $host;
            expect {
                *(yes/no)* {send -- yes\r;exp_continue;}
                *assword:* {send -- ${2}\r;exp_continue;}
                eof        {exit 0;}
            }" >/dev/null 2>&1;
    done
}
SshWithoutAuth ${hosts} ${password}
echo "All tasks done!"

-- update 2022年7月15日13:36:57

# 在control_node节点上生成密钥对
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''
for host in 192.168.200.{27..33};do
  # 将各节点的主机信息(host key)写入control_node的
  # `~/.ssh/known_hosts`文件
  ssh-keyscan $host >>~/.ssh/known_hosts 2>/dev/null
  # 将control_node上的ssh公钥分发给各节点:
  sshpass -p'123456' ssh-copy-id root@$host &>/dev/null
done
目录
相关文章
|
8月前
|
Ubuntu Linux 索引
Centos 7、Debian及Ubuntu系统中安装和验证tree命令的指南。
通过上述步骤,我们可以在CentOS 7、Debian和Ubuntu系统中安装并验证 `tree`命令。在命令行界面中执行安装命令,然后通过版本检查确认安装成功。这保证了在多个平台上 `tree`命令的一致性和可用性,使得用户无论在哪种Linux发行版上都能使用此工具浏览目录结构。
691 78
|
8月前
|
弹性计算 安全 Linux
阿里云服务器ECS安装宝塔Linux面板、安装网站(新手图文教程)
本教程详解如何在阿里云服务器上安装宝塔Linux面板,涵盖ECS服务器手动安装步骤,包括系统准备、远程连接、安装命令执行、端口开放及LNMP环境部署,手把手引导用户快速搭建网站环境。
|
9月前
|
监控 Linux 网络安全
FinalShell SSH工具下载,服务器管理,远程桌面加速软件,支持Windows,macOS,Linux
FinalShell是一款国人开发的多平台SSH客户端工具,支持Windows、Mac OS X和Linux系统。它提供一体化服务器管理功能,支持shell和sftp同屏显示,命令自动提示,操作便捷。软件还具备加速功能,提升访问服务器速度,适合普通用户和专业人士使用。
2970 0
|
9月前
|
存储 安全 Linux
Linux服务器上安装配置GitLab的步骤。
按照以上步骤,一个基础的GitLab服务应该运行并可以使用。记得定期检查GitLab官方文档,因为GitLab的安装和配置步骤可能随着新版本而变化。
871 0
|
7月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
802 1
二、Linux文本处理与文件操作核心命令
|
7月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
463 137
|
7月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
1260 58
|
10月前
|
JSON 自然语言处理 Linux
linux命令—tree
tree是一款强大的Linux命令行工具,用于以树状结构递归展示目录和文件,直观呈现层级关系。支持多种功能,如过滤、排序、权限显示及格式化输出等。安装方法因系统而异常用场景包括:基础用法(显示当前或指定目录结构)、核心参数应用(如层级控制-L、隐藏文件显示-a、完整路径输出-f)以及进阶操作(如磁盘空间分析--du、结合grep过滤内容、生成JSON格式列表-J等)。此外,还可生成网站目录结构图并导出为HTML文件。注意事项:使用Tab键补全路径避免错误;超大目录建议限制遍历层数;脚本中推荐禁用统计信息以优化性能。更多详情可查阅手册mantree。
860 143
linux命令—tree
|
6月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
1147 2
|
7月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
659 0
Linux内存问题排查命令详解

热门文章

最新文章

下一篇
开通oss服务