如何對某個用戶配置免密:
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
根據(jù)上述操作生成相關(guān)密鑰后,執(zhí)行ssh localhost养盗,依然需要輸入密碼巨税,排查問題:
先檢查ssh配置文件,包括客戶端配置文件ssh_config和服務(wù)端配置文件sshd_config
客戶端配置文件路徑:/etc/ssh/ssh_config
文件內(nèi)容:
Host *
GSSAPIAuthentication yes
# If this option is set to yes then remote X11 clients will have full access
# to the original X11 display. As virtually no X11 client supports the untrusted
# mode correctly we set this to yes.
ForwardX11Trusted yes
# Send locale-related environment variables
SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
SendEnv XMODIFIERS
# 如果是使用dsa生成密鑰的話张弛,那么必須要配置上這個參數(shù)
PubkeyAcceptedKeyTypes +ssh-dss
服務(wù)端配置文件路徑:/etc/ssh/sshd_config
文件內(nèi)容:
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
# Authentication:
# 配置允許root用戶登錄,yes
# 如果不允許root登錄酪劫,需要把它改為no
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
GSSAPICleanupCredentials no
X11Forwarding yes
UsePrivilegeSeparation sandbox
UseDNS no
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
KerberosAuthentication no
PubkeyAuthentication yes
UsePAM yes
AuthorizedKeysCommand /usr/bin/sss_ssh_authorizedkeys
AuthorizedKeysCommandUser nobody
GSSAPIAuthentication yes
ChallengeResponseAuthentication yes
# 如果使用dsa生成密鑰的話吞鸭,則必須
PubkeyAcceptedKeyTypes +ssh-dss
KexAlgorithms diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
服務(wù)端和客戶端按這個配置文件來配是沒有問題的,親測覆糟。
PubkeyAcceptedKeyTypes +ssh-dss 是我后加上的參數(shù)刻剥,但是它并不是之前免密無效的原因,因為我之前生成的密鑰使用的是rsa而不是dsa滩字。
所以配置文件這塊是沒有問題的造虏。
再排查權(quán)限問題
首先排查密鑰文件authorized_keys的權(quán)限,它的權(quán)限嚴格要求是600麦箍,因為在步驟1中做了這個操作漓藕,所以它的權(quán)限是正確的;
接著排查/home/xxx/.ssh目錄的權(quán)限挟裂,它的權(quán)限須是700享钞,檢查后發(fā)現(xiàn)權(quán)限是正常的700;
再接著用戶主目錄的權(quán)限诀蓉,ll /home 查看主目錄/home/xxx的權(quán)限栗竖,發(fā)現(xiàn)它的權(quán)限是777,然后我把他改小點渠啤,改為755狐肢,然后再通過ssh localhost測試,這時免密就成功了沥曹。
所以之前免密失效的根本原因還是權(quán)限的問題份名,ssh不允許用戶的主目錄和.ssh目錄以及authorized_keys文件的權(quán)限開放得太大。