來源 http://www.study-area.org/tips/ssh_tips.htm SSH 的一些安全小技巧 作者﹕網中人 /dev/null $ chmod 700 ~/.ssh $ touch ~/.ssh/authorized_keys $ chmod 644 ~/.ssh/authorized_keys -------------------------------------------------- 轉往 client 端: $ ssh-keygen -t rsa (按三下 enter 完成﹔不需設密碼,除非您會用 ssh-agent 。) $ scp ~/.ssh/id_rsa.pub user1@server.machine:id_rsa.pub (若是 windows client, 可用 puttygen.exe 產生 public key, 然後複制到 server 端後修改之, 使其內容成為單一一行.) --------------------------------------------------- 回到 server 端: $ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys $ rm ~/id_rsa.pub $ exit 3) 限制 su / sudo 名單: # vi /etc/pam.d/su auth required /lib/security/$ISA/pam_wheel.so use_uid # visudo %wheel ALL=(ALL) ALL # gpasswd -a user1 wheel 4) 限制 ssh 使用者名單 # vi /etc/pam.d/sshd auth required pam_listfile.so item=user sense=allow file=/etc/ssh_users onerr=fail # echo user1 >> /etc/ssh_users 5) 封鎖 ssh 連線並改用 web 控管清單 # iptables -I INPUT -p tcp --dport 22 -j DROP # mkdir /var/www/html/ssh_open # cat > /var/www/html/ssh_open/.htaccess < /var/www/html/ssh_open/ssh_open.php <"; echo "Pls Check your rights to dir $dir_path or file $ip_list"; } else { fputs($file,"$user_ip"); fclose($file); echo "client ip($user_ip) has put into $dir_path/$ip_list"; } } else { echo "Invalid IP format!!
ssh_open.txt was not changed."; } ?> END # touch /var/www/html/ssh_open/ssh_open.txt # chmod 640 /var/www/html/ssh_open/* # chgrp apache /var/www/html/ssh_open/* # chmod g+w /var/www/html/ssh_open/ssh_open.txt # chmod o+t /var/www/html/ssh_open # service httpd restart # mkdir /etc/iptables # cat > /etc/iptables/sshopen.sh <> /etc/services # cat > /etc/xinetd.d/sshopen < /etc/cron.d/sshopen < /etc/iptables/sshblock.sh <> $PERM_LIST } done END # chmod +x /etc/firewall/sshblock.sh # cat >> /etc/hosts.allow < /etc/xinetd.d/finger <> /etc/hosts.allow <"<) 再來把公鑰的內容導入到 authorized_keys #cat id_dsa.pub > authorized_keys 3.因為 Openssh 的私鑰格式和 putty 使用的格式不同, 所以需要由 puttygen.exe 轉換格式後才能使用,不然會有兩種錯誤的情況: -------------------------------------------------------------- 可能出現的幾種問題: (a)、Server refused our key 公鑰和私鑰不匹配,或者沒有 authorized_keys 文件 (b)、Unable to use key file "id_dsa.ppk" (SSH2 private key) 私鑰檔案的格式不正確 -------------------------------------------------------------- 3.1把 server 上的私鑰:id_dsa 拷貝到 PC 上 將 id_dsa 的內容顯示在螢幕上,再複製下來. #cat id_dsa 把複製的內容貼到記事本裡存成 id_dsa_by_ssh-keygen.ppk 再來就是開啟用 puttygen.exe → Conversions → Import Key 匯入後,在 Key passphrase 和 Confirm passphrase 輸入保護私鑰的密碼後, (不想打密碼的人,就保留成空白也可以,不過萬一私鑰掉了被撿到,那就慘了>"<), 然後,再從 File → Save Private Key 把私鑰另存新檔即可使用了。 4.Pietty/Putty 使用方法: 啟動 Pietty/Putty ,從 SSH → Auth 去指定私鑰的檔案路徑即可。 5.強烈建議:私鑰(Private Key)一定要用 Key Passphrase 來保護, 密碼(至少 8 位數以上,每三個月更換一次公/私鑰)千萬不要跟 root 的密碼一樣