免密登录Linux主机

2018-07-30 宋洋葱 宋洋葱

安装 sshd

yum install -y openssl openssh-server

vi /etc/ssh/sshd_config
Port 22
PermitRootLogin yes

启动ssh服务
systemctl start sshd.service

重启网络
service network restart

设置开机启动ssh服务
systemctl enable sshd.service

每次在终端使用ssh链接服务器,需要输入账号、密码、端口、域名等一大堆东西,比较繁琐。若不想输入密码端口等信息,可以使用 ssh 密钥方式连接服务器。

假设Linux服务器的ip为:192.168.1.2,ssh端口:3322,以下以mac操作为例。

生成公钥和私钥

使用ssh-keygen生成密钥:ssh-keygen -f ~/.ssh/id_rsa_sxy

可以使用 -C 添加注释,如: ssh-keygen -f ~/.ssh/id_rsa_sxy -C "sxy21cn@qq.com"

把公钥复制到需要登录的主机

ssh-copy-id -p 3322 -i ~/.ssh/id_rsa_sxy.pub  songxueyan@192.168.1.2

或者把公钥内容复制到 ~/.ss/authorized_keys

验证登录

成功以后即可私钥链接服务器(服务器用公钥验证),使用 -i 参数指定密钥文件。

ssh -i ~/.ssh/id_rsa_sxy -p 3322  songxueyan@192.168.1.2

如果密钥名称是 id_rsa 则不需要 -i 参数

若是从别的地方下载或者拷贝的密钥,链接时可能出现错误:

Permissions 0644 for '/Users/songyangcong/.ssh/id_rsa_sxy' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/songyangcong/.ssh/id_rsa_sxy": bad permissions
songxueyan@192.168.1.2: Permission denied (publickey).

意思是权限太大了,私钥只能创建者有读写权限,其他人不可读写。改一下权限即可。

chmod og-rw ~/.ssh/id_rsa_sxy.pub

配置 config

虽然少了账号密码,但需要指定端口和密钥,能不能一块去掉呢。

先编辑host sudo vim /etc/hosts

绑定一个域名如下: 192.168.1.2 www.sxy91.com sxy91

测试:ping sxy91

然后编辑ssh的config文件:vim ~/.ssh/config,内容如下:

Host sxy91
    HostName www.sxy91.com
    User songxueyan
    Port 3322
    IdentityFile ~/.ssh/id_rsa_sxy

此时,无须输入端口和密码即可连接成功

ssh sxy91

遇到的问题

还需要密码

若仍需输入密码,一般为权限问题。

服务器权限问题

# 用密码登录服务器查看 ~/.ssh 目录下文件的权限
ls -lh ~/.ssh
如果不是“ -rw-------” 那么权限不正确 
# 需要把权限改成仅自己可读写
chmod -R og-wxr ~/.ssh
chmod u-x ~/.ssh/authorized_keys

说明:安全起见,sshd强制对key的文件权限进行检查,authorized_keys文件所在的目录(包括上层目录)的权限只有自己能读写,他人和组只能读。

若还有问题,调试过程如下:

ssh -vT sxy91 # 打印调试信息,查看Next authentication method: publickey 以后的行。
#发现有Offering public key,但没有Server accepts key

# 查看sshd日志
tail -f /var/log/secure
# 发现一行Authentication refused: bad ownership or modes for file ~/.ssh/authorized_keys

客户端权限问题 说明:私钥仅能自己读写。公钥其他人只能读。 若权限不正确,git或者ssh会出现id_rsa are too open错误如下:

Permissions 0644 for  are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored.

修改客户端的私钥公钥权限

chmod -R og-wxr ~/.ssh
chmod -R og+r ~/.ssh/*.pub
chmod -R og+r ~/.ssh/config
chmod -R og+r ~/.ssh/known_hosts

再次测试问题解决

ssh奇技淫巧

1、ssh端口转发(隧道)

#本机访问server端8080端口如下(s端防火墙只允许localhost访问):
ssh -L 8080:localhost:8080 sxy@server

2、访问server的多个绑定localhost的端口

ssh sxy@server -L 8080:localhost:8080 -L 8088:localhost:8088

3、免密转发ssh_config

# 把转发规则配置到 ~/.ssh/config

Host tunnel
    HostName database.example.com
    IdentityFile ~/.ssh/coolio.example.key
    User sxy91
    LocalForward 9906 127.0.0.1:3306
    LocalForward 8806 127.0.0.1:8806

alpline 错误

如果遇到错误: channel 3: open failed: administratively prohibited: open failed

查看 sshd 配置项:

sshd -T | grep -Ei 'TCPKeepAlive|AllowTcpForwarding|PermitOpen'

# print
tcpkeepalive yes
allowtcpforwarding yes
permitopen any

修改为正确的配置:

vi /etc/ssh/sshd_config
rc-service sshd restart

参考