需求:服务器的root和密码登录都禁用,只开放普通用户登录,这时需要给普通用户配置秘钥文件,实现无密码登录

需要注意的是使用什么用户,就把秘钥文件拷入到该用户的家目录下,如果是root用户,就直接拷贝到/root/.ssh/下(本次测试是使用普通用户oper测试的)

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

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

测试机器:

  host1:192.168.0.131

  host2:192.168.0.132

一、在新机器生成秘钥:

[root@host1 .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:O9C8LWfjT44gIJR+URWH3FFJhsgqMU8VmaDDZAGWR7k root@host1
The key's randomart image is:
+---[RSA ]----+
| o+=oo*=Oo=+. |
| ..==o. B.o.. |
| o.=* . |
| o Eooo |
| o o.. S |
| o . . + |
| . * = . |
| . B = |
| o.o |
+----[SHA256]-----+
[root@host1 .ssh]# ls
id_rsa id_rsa.pub known_hosts

二、将id_rsa.pub拷入需要登录的服务器的用户家目录下的.ssh/authorized_keys中(即192.168.0.132下的/home/oper/.ssh/authorized_keys)

  以下三种方法任选其一即可

  2.1:通过ssh-copy-id的方式

[root@host2 oper]# mkdir /home/oper/.ssh
[root@host2 oper]# chown oper:oper .ssh/
[root@host2 oper]# chmod .ssh
[root@host2 oper]# ls -la
总用量
drwx------. oper oper 4月 : .
drwxr-xr-x. root root 4月 : ..
-rw-------. oper oper 4月 : .bash_history
-rw-r--r--. oper oper 8月 .bash_logout
-rw-r--r--. oper oper 8月 .bash_profile
-rw-r--r--. oper oper 8月 .bashrc
drwx------. oper oper 4月 : .ssh [root@host1 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub oper@192.168.0.132
oper@192.168.0.132's password:
Number of key(s) added: Now try logging into the machine, with: "ssh 'oper@192.168.0.132'"
and check to make sure that only the key(s) you wanted were added.
#登录测试
[root@host1 .ssh]# ssh oper@192.168.0.132
Last failed login: Mon Apr :: CST from 192.168.0.131 on ssh:notty
There was failed login attempt since the last successful login.
Last login: Mon Apr :: from 192.168.0.131
[oper@host2 ~]$

  2.2:通过scp将内容写到对方的文件中

[root@host1 .ssh]# scp -p ~/.ssh/id_rsa.pub oper@192.168.0.132:/home/oper/.ssh/authorized_keys
oper@192.168.0.132's password:
id_rsa.pub
#登录测试 % .6KB/s :
[root@host1 .ssh]# ssh oper@192.168.0.132
Last login: Mon Apr :: from 192.168.0.131
[oper@host2 ~]$

  2.3:通过ssh命令写入,此方式可写成脚本,然后批量写入主机

  此方式也可直接把/root/.ssh/id_rsa.pub的内容直接复制粘贴到host2下/home/oper/.ssh/authorized_keys中

[root@host1 .ssh]# ssh oper@192.168.0.132 "echo `cat /root/.ssh/id_rsa.pub` >> /home/oper/.ssh/authorized_keys "
oper@192.168.0.132's password:
[root@host1 .ssh]# ssh oper@192.168.0.132
oper@192.168.0.132's password: [root@host2 .ssh]# pwd
/home/oper/.ssh
[root@host2 .ssh]# ll
总用量
-rw-rw-r--. oper oper 4月 : authorized_keys
[root@host2 .ssh]# chmod authorized_keys
[root@host2 .ssh]# ll
总用量
-rw-------. oper oper 4月 : authorized_keys
#登录测试
[root@host1 .ssh]# ssh oper@192.168.0.132
Last login: Mon Apr :: from 192.168.0.131
[oper@host2 ~]$ 出现需要密码登录是因为host2的authorized_keys权限没有设置为600;把权限设置为600后,再执行 ssh oper@192.168.0.132就可以了,如果之前有权限是600的authorized_keys;则可直接登录

PS:如果普通用户需要切换到root用户且无需输入密码,则在root用户模式下执行命令:visudo 

[root@host2 ~]# visudo  #最后一行加入
Cmnd_Alias SU = /bin/su
oper ALL = (root) NOPASSWD: SU
:wq
[root@host2 ~]# su - oper
#登录测试
[oper@host2 ~]$ sudo su #最好使用sudo su - 完全切换,su会加载不出来环境变量
[root@host2 oper]#
附:文件说明
     ~/.ssh/identity
             该用户默认的 RSA1 身份认证私钥(SSH-1)。此文件的权限应当至少限制为"600"。
             生成密钥的时候可以指定采用密语来加密该私钥(3DES)。
             ssh(1) 将在登录的时候读取这个文件。
     ~/.ssh/identity.pub
             该用户默认的 RSA1 身份认证公钥(SSH-1)。此文件无需保密。
             此文件的内容应该添加到所有 RSA1 目标主机的 ~/.ssh/authorized_keys 文件中。
     ~/.ssh/id_dsa
             该用户默认的 DSA 身份认证私钥(SSH-2)。此文件的权限应当至少限制为"600"。
             生成密钥的时候可以指定采用密语来加密该私钥(3DES)。
             ssh(1) 将在登录的时候读取这个文件。
     ~/.ssh/id_dsa.pub
             该用户默认的 DSA 身份认证公钥(SSH-2)。此文件无需保密。
             此文件的内容应该添加到所有 DSA 目标主机的 ~/.ssh/authorized_keys 文件中。
     ~/.ssh/id_rsa
             该用户默认的 RSA 身份认证私钥(SSH-2)。此文件的权限应当至少限制为"600"。
             生成密钥的时候可以指定采用密语来加密该私钥(3DES)。
             ssh(1) 将在登录的时候读取这个文件。
     ~/.ssh/id_rsa.pub
             该用户默认的 RSA 身份认证公钥(SSH-2)。此文件无需保密。
             此文件的内容应该添加到所有 RSA 目标主机的 ~/.ssh/authorized_keys 文件中。
     /etc/ssh/moduli
             包含用于 DH-GEX 的 Diffie-Hellman groups 。文件的格式在 moduli(5) 手册页中描述。

Linux下实现普通用户免密码登录【超详细】的更多相关文章

  1. linux下设置了SSH免密码登录但还是需要输入密码的解决办法

    今天在设置linux的免密码登录后,仍然需要输入密码,后来找到了原因,是因为用户没有权限修改.ssh目录下的know_hosts文件导致的. 具体情况是这样的: 在/home/username/.ss ...

  2. Linux下实现普通用户免密登录并执行root权限

    需求:服务器的root和密码登录都禁用,只开放普通用户登录,这时需要给普通用户配置秘钥文件,实现无密码登录 需要注意的是使用什么用户,就把秘钥文件拷入到该用户的家目录下,如果是root用户,就直接拷贝 ...

  3. 【Hadoop系列】linux下 root用户免密码登录远程主机 ssh

    SSH原理:[Hadoop系列]linux SSH原理解析 操作环境: CentOS 6.5 操作对象: 用户A主机和远程主机B 正文部分:斜体加粗代表linux指令. linux下 非root用户免 ...

  4. Linux使用ssh公钥实现免密码登录Linux

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例.有机器A(192.168.1.155),B(192.168.1.181).现想A ...

  5. redhat6.2下的ssh密钥免密码登录(原创)

    这个是我自己写的,鼓励转载,请说明转载地址:http://www.cnblogs.com/nucdy/p/5664840.html 在进行hadoop的免密码的登录操作是,老是发生no route等错 ...

  6. linux 服务器名 访问 shh免密码登录

    以根用户登录,或者登录后切换到根用户,然后在提示符下输入hostname命令,可以看出当前系统的主机名为localhost.localdomain.   更改/etc/sysconfig下的netwo ...

  7. linux ssh公钥免密码登录

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 一.SSH公钥登录原理 在平时工作中我们经常要远程登录服务器,这就要用到SSH ...

  8. SSH2免密码登录OpenSSH

    OpenSSH免密码登录SSH2http://blog.csdn.net/aquester/article/details/23836245 两个SSH2间免密码登录http://blog.csdn. ...

  9. OpenSSH免密码登录SSH2

    SSH2免密码登录OpenSSHhttp://blog.csdn.net/aquester/article/details/23836299 两个SSH2间免密码登录http://blog.csdn. ...

随机推荐

  1. JAVA_SE基础——47.接口

    如果一个抽象类中的所有方法都是抽象的,则可以将这个类用另一种方法来定义,即接口~ 在定义接口时,需要用interface关键字来声明,具体实例如code1 接口的定义格式:interface 接口名{ ...

  2. Crontab定时备份数据库

    1.创建一个shell脚本文件 cd /usr mkdir dbbackup cd /usr/dbbackup vim backup.sh echo "------------------- ...

  3. Python内置函数(54)——callable

    英文文档: callable(object) Return True if the object argument appears callable, False if not. If this re ...

  4. python 中 reduce 函数的使用

    reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...

  5. python入门(3)python的解释器

    python入门(3)python的解释器 Python写的程序是以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的, ...

  6. 2018年Web前端自学路线

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. Web前端入门的自学路线 新手入门前端,需要学习的基础内容有很多,如下. ...

  7. C#实现导出Excel

    这段时间用到了导出Excel的功能,这个功能还是比较常用的,我常用的有两个方法,现在整理一下,方便以后查看. 一.实现DataTable数据导出到本地,需要自己传进去导出的路径. /// <su ...

  8. Python之IO模型

    IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞 ...

  9. Lua编写wireshark插件初探——解析Websocket上的MQTT协议

    一.背景 最近在做物联网流量分析时发现, App在使用MQTT协议时往往通过SSL+WebSocket+MQTT这种方式与服务器通信,在使用SSL中间人截获数据后,Wireshark不能自动解析出MQ ...

  10. c# IPC实现本机进程之间的通信

    IPC可以实现本地进程之间通信.这种用法不是太常见,常见的替代方案是使用wcf,remoting,web service,socket(tcp/pipe/...)等其他分布式部署方案来替代进程之间的通 ...