1. 程式人生 > >ssh的配置,ssh打開密鑰登陸,關閉密碼登陸。

ssh的配置,ssh打開密鑰登陸,關閉密碼登陸。

服務器端 方式 nor 請求 日誌類 ava 拒絕 p地址 消息

剛裝玩fedora,那麽我們就以fedora為例來說一下怎麽配置:

1、先確認是否已安裝ssh服務:

[root@localhost ~]# rpm -qa | grep openssh-server

 openssh-server-5.3p1-19.fc12.i686 (這行表示已安裝)

若未安裝ssh服務,可輸入:

#yum install openssh-server

進行安裝

2、winscp登陸,用root密碼登陸。

3、修改配置文件:

#vi /etc/ssh/sshd_config

winscp登陸的話,就直接修改/etc/ssh/sshd_config

找到AuthorizedKeysFile .ssh/authorized_keys

這就是存在服務器上的公鑰文件,一行一個。

自己創建一下

完整路徑是 /root/.ssh

在winscp下要用一下手動打開(ctrl+o),.ssh隱藏了,打開路徑/root/.ssh

如果不存在,就在root下手動創建一個.ssh文件夾

技術分享圖片

新建文件,復制你的公鑰那行保存

找到:PasswordAuthentication yes

允許密碼登陸,改為no,禁止密碼登陸。

技術分享圖片
下面是對SSH配置文件的一些選項的分解說明,ssh_config是OpenSSH客戶端的配置文件,sshd_config是OpenSSH服務器端的配置文件。

ssh_config的內容如下:


# This is the ssh client system-wide configuration file.  See ssh_config(5) for more information.  This file provides defaults for users, and the values can be changed in 
# er-user configuration files or on the command line. # Configuration data is parsed as follows: // 配置選項生效的優先級 # 1. command line options // 命令行選項 # 2. user-specific file // 用戶指定文件 # 3. system-wide file // 系統範圍的文件 # Any configuration value is only changed the first time it is set. Thus, host-specific definitions should be at the beginning of the configuration file, and defaults at the end.
# Site-wide defaults for some commonly used options. For a comprehensive list of available options, their meanings and defaults, please see the ssh_config(5) man page. Host * // 使用的計算機範圍,‘*‘表示全部 # ForwardAgent no // 設置連接是否經過驗證代理(如果存在)轉發給遠程計算機 # ForwardX11 no // 設置X11連接是否被自動重定向到安全的通道和顯示集 # ForwardX11Trusted yes // 是否允許轉發X11會話 # RhostsRSAAuthentication no // 設置是否使用RSA進行rhosts的安全驗證 # RSAAuthentication yes // 設置是否使用RSA進行安全驗證 # PasswordAuthentication yes // 設置是否需要口令驗證 # HostbasedAuthentication no # GSSAPIAuthentication no # GSSAPIDelegateCredentials no # GSSAPIKeyExchange no # GSSAPITrustDNS no # BatchMode no // 如果為yes,則交互輸入口令時的提示(passphrase/password的提示)信息將被禁止 # CheckHostIP yes // 設置SSH是否查看連接到服務器的主機的IP地址以防止DNS欺騙。建議設置為yes # AddressFamily any # ConnectTimeout 0 # StrictHostKeyChecking ask // 如果設置成yes,SSH就不會自動把計算機的密鑰加入$HOME/.ssh/known_hosts文件,並且一旦計算機的密匙發生了變化,就拒絕連接 # IdentityFile ~/.ssh/identity # IdentityFile ~/.ssh/id_rsa // RSA安全驗證文件的位置 # IdentityFile ~/.ssh/id_dsa // DSA安全驗證文件的位置 # Port 22 // 服務器端口 # Protocol 2,1 // 使用的SSH協議 # Cipher 3des // 設置加密的方式 # Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc # MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160 # EscapeChar ~ // 設置Escape(轉義)字符 # Tunnel no # TunnelDevice any:any # PermitLocalCommand no # VisualHostKey no # ProxyCommand ssh -q -W %h:%p gateway.example.com SendEnv LANG LC_* // 局部環境變量 HashKnownHosts yes GSSAPIAuthentication yes GSSAPIDelegateCredentials no sshd_config的內容如下: # Package generated configuration file See the sshd_config(5) manpage for details # What ports, IPs and protocols we listen for Port 22 // sshd的監聽端口號,默認為22 # Use these options to restrict which interfaces/protocols sshd will bind to #ListenAddress :: #ListenAddress 0.0.0.0 // 設置sshd服務綁定的IP地址,0.0.0.0表示偵聽所有地址 Protocol 2 // 默認只使用2.*版本的SSH協議 # HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key // SSH2版本的RSA密鑰存放位置 HostKey /etc/ssh/ssh_host_dsa_key // SSH2版本的DSA密鑰存放位置 HostKey /etc/ssh/ssh_host_ecdsa_key // SSH2版本的ECDSA密鑰存放位置 #Privilege Separation is turned on for security UsePrivilegeSeparation yes # Lifetime and size of ephemeral version 1 server key KeyRegenerationInterval 3600 // 密鑰每隔1小時生成一次 ServerKeyBits 768 // SSH服務器密鑰的位數 # Logging SyslogFacility AUTH // 設置sshd發送到syslog所使用的日誌類型 // 設置在記錄來自sshd的消息時是否給出facility code LogLevel INFO // syslog日誌級別 # Authentication: LoginGraceTime 120 // 設置如果用戶不能成功登錄,sshd將會在這個配置參數指定的時間過後斷開連接(單位為秒) PermitRootLogin yes // 如果為yes則允許root用戶使用ssh登錄,為no則表示不允許root進行ssh登錄 StrictModes yes // 設置sshd在接受登錄請求前是否檢查用戶的主目錄以及rhosts文件的權限和所有者等信息。防止目錄和文件設成任何人都有寫權限 RSAAuthentication yes // 是否允許RSA驗證 PubkeyAuthentication yes // 是否允許公鑰驗證 AuthorizedKeysFile %h/.ssh/authorized_keys// 公鑰文件存放的位置 # Don‘t read the user‘s ~/.rhosts and ~/.shosts files IgnoreRhosts yes // 驗證時是否使用‘~/.rhosts‘和‘~/.shosts‘文件 # For this to work you will also need host keys in /etc/ssh_known_hosts RhostsRSAAuthentication no // 設置是否允許用rhosts或/etc/hosts.equiv加上RSA進行安全驗證 # similar for protocol version 2 HostbasedAuthentication no # Uncomment if you don‘t trust ~/.ssh/known_hosts for RhostsRSAAuthentication #IgnoreUserKnownHosts yes // 設置sshd在進行RhostsRSAAuthentication安全驗證時是否信任用戶的‘~/.ssh/known_hosts‘文件 # To enable empty passwords, change to yes (NOT RECOMMENDED) PermitEmptyPasswords no // 設置是否允許用空口令登錄 # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) ChallengeResponseAuthentication no // 設置是否允許使用提示/應答式認證。sshd支持login.conf文件中定義的所有認證類型 # Change to no to disable tunnelled clear text passwords #PasswordAuthentication yes // 設置是否需要口令驗證,默認為yes # Kerberos options // Kerneros驗證 #KerberosAuthentication no #KerberosGetAFSToken no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes # GSSAPI options // GSSAPI 驗證 #GSSAPIAuthentication no #GSSAPICleanupCredentials yes // 清除驗證信息 X11Forwarding yes // 設置sshd是否允許X11轉發,默認為允許 X11DisplayOffset 10 PrintMotd no // 設置sshd是否在用戶登錄時顯示/etc/motd中的信息 PrintLastLog yes // 交互式登錄時是否輸出用戶上次登錄的日期和時間 TCPKeepAlive yes // TCP活動保持 #UseLogin no // 指定login命令是否可用於交互式登錄會話 #MaxStartups 10:30:60 #Banner /etc/issue.net // 設置保存banner信息的文件位置,用戶登錄後會顯示該banner信息 # Allow client to pass locale environment variables AcceptEnv LANG LC_* // 指定客戶端發送的那些環境變量能後復制到當前會話的運行環境(客戶端需要設置其配置文件ssh_config中的SendEnv參數) Subsystem sftp /usr/lib/openssh/sftp-server // 用於配置一個外部的服務程序,如文件傳輸服務器sftp-server。配置參數的值應該是一個系統名與命令,能夠基於客戶系統的請求開始運行。sftp-server命令實現了sftp文件傳輸子系統。這個配置參數僅使用與SSH2 # Set this to ‘yes‘ to enable PAM authentication, account processing, and session processing. If this is enabled, PAM authentication will be allowed through the # ChallengeResponseAuthentication and PasswordAuthentication. Depending on your PAM configuration, PAM authentication via ChallengeResponseAuthentication # may bypass the setting of "PermitRootLogin without-password". If you just want the PAM account and session checks to run without PAM authentication, then enable #this but set PasswordAuthentication and ChallengeResponseAuthentication to ‘no‘. UsePAM yes // 是否啟用PAM插件式認證模塊,默認為yes
View Code

關於配置文件的詳細說明,留著,不用看。

禁止了密碼登陸,設置了公鑰。

重啟sshd,在終端窗口運行:

systemctl restart sshd.service

可以配置winscp用私鑰登陸了。

ssh的配置,ssh打開密鑰登陸,關閉密碼登陸。