Linux下实现普通用户免密码登录【超详细】
需求:服务器的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下实现普通用户免密码登录【超详细】的更多相关文章
- linux下设置了SSH免密码登录但还是需要输入密码的解决办法
今天在设置linux的免密码登录后,仍然需要输入密码,后来找到了原因,是因为用户没有权限修改.ssh目录下的know_hosts文件导致的. 具体情况是这样的: 在/home/username/.ss ...
- Linux下实现普通用户免密登录并执行root权限
需求:服务器的root和密码登录都禁用,只开放普通用户登录,这时需要给普通用户配置秘钥文件,实现无密码登录 需要注意的是使用什么用户,就把秘钥文件拷入到该用户的家目录下,如果是root用户,就直接拷贝 ...
- 【Hadoop系列】linux下 root用户免密码登录远程主机 ssh
SSH原理:[Hadoop系列]linux SSH原理解析 操作环境: CentOS 6.5 操作对象: 用户A主机和远程主机B 正文部分:斜体加粗代表linux指令. linux下 非root用户免 ...
- Linux使用ssh公钥实现免密码登录Linux
ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例.有机器A(192.168.1.155),B(192.168.1.181).现想A ...
- redhat6.2下的ssh密钥免密码登录(原创)
这个是我自己写的,鼓励转载,请说明转载地址:http://www.cnblogs.com/nucdy/p/5664840.html 在进行hadoop的免密码的登录操作是,老是发生no route等错 ...
- linux 服务器名 访问 shh免密码登录
以根用户登录,或者登录后切换到根用户,然后在提示符下输入hostname命令,可以看出当前系统的主机名为localhost.localdomain. 更改/etc/sysconfig下的netwo ...
- linux ssh公钥免密码登录
ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 一.SSH公钥登录原理 在平时工作中我们经常要远程登录服务器,这就要用到SSH ...
- SSH2免密码登录OpenSSH
OpenSSH免密码登录SSH2http://blog.csdn.net/aquester/article/details/23836245 两个SSH2间免密码登录http://blog.csdn. ...
- OpenSSH免密码登录SSH2
SSH2免密码登录OpenSSHhttp://blog.csdn.net/aquester/article/details/23836299 两个SSH2间免密码登录http://blog.csdn. ...
随机推荐
- 第二篇:Python数据类型
一.引子 1.什么是数据? x= #是我们要存储的数据 2.为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类型 数字(整型,长整型,浮点型,复数) 字 ...
- nyoj 聪明的kk
聪明的kk 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 聪明的"KK"非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国 ...
- 使用静态基类方案让 ASP.NET Core 实现遵循 HATEOAS Restful Web API
Hypermedia As The Engine Of Application State (HATEOAS) HATEOAS(Hypermedia as the engine of applicat ...
- php的开发的apache的配置及伪静态的应用
1.Apache之所以能够解析php代码是游览器首先发送数据到模版页面,然后模版页提交数据到php页面,然后php代码经过Apache解析过后生成结果的,所以是 在Apache的配置文件中是可以看到开 ...
- javaScript识别网址文本并转为链接文本
最近项目有个需求:用户之间发送消息时,如果发送者输入的信息中含有网址文本,要在接受者界面中显示网址链接,点击该链接直接跳转到网页.这个功能和 QQ 发送网址文本的效果非常像,可以说是一模一样的. 思路 ...
- Python内置函数(38)——zip
英文文档: zip(*iterables) Make an iterator that aggregates elements from each of the iterables. Returns ...
- Mego开发文档 - 保存关系数据
保存关系数据 由于没有对象的更改跟踪,因此关系的操作需要开发者明确指定,在成功执行后Mego会影响到相应的关系属性中. 添加关系 在以下示例中如果成功执行则source的Customer属性会变为ta ...
- React中路由传参及接收参数的方式
注意: 路由表改变后要重启服务 方式 一: 通过params 1.路由表中 <Route path=' /s ...
- git出现错误原因解释
原因,在pull下拉代码或者push之前,你本地还有代码没有进行commit. 引起下面的错误. 建议commit后先pull再看看有没有冲突在进行push. git.exe push --pro ...
- 框架学习之Struts2(二)---基本配置和封装表单数据
一.结果页面配置 1.局部结果页面配置 <!-- 局部结果页面配置--> <package name = "demo" extends = "strut ...