前一陣遠程維護Linux服務器,使用的是SSH,傳說中的secure shell。
登錄:ssh [hostname] -u user
輸入密碼:*****
登 陸之後就能夠像控制本身的機器同樣控制它了,不過沒有可視化的界面。不過如今我所使用過的兩個版本Linux(SUSE和FC5)中有可使用相似FTP 界面的工具。使用工具鏈接時,選擇SSH,端口是填服務器的SSH端口,默認是22,可是仍是要手工填寫。鏈接上之後FTP界面和shell同時使用能夠 提升工做效率。
簡單的傳輸命令:scp
scp /etc/php.ini user@www.linuxidc.com:/home/user
會將本地的 /etc/php.ini 這個文件 copy 到 www.linuxidc.com,使用者user的主目錄下
執行命令以後須要輸入密碼,只後就開始傳送。
scp user@ww.linuxidc.com:/etc/php.ini /home/user2
將主機ww.liniux.cn 上的/etc/php.ini文件copy到本地/home/user2目錄下
ssh –l user –p 22 linuxidc.net
輸入密碼便可登陸
l login_name
指定登入於遠程機器上的使用者,若沒加這個選項,而直接打 ssh lost 也是能夠的,它是以讀者目前的使用者去作登入的動做。 例如: ssh –l root http://www.linuxidc.net
===================================================
-c blowfish|3des
在期間內選擇所加密的密碼型式。預設是3des,3des(做三次的資料加密) 是用三種不一樣的密碼鍵做三次的加密-解密-加密。 blowfish 是一個快速區塊密碼編制器,它比3des更安全以及更快速。
===================================================
-v
Verbose 模式。使ssh 去印出關於行程的除錯訊息,這在鏈接除錯,認 證和設定的問題上有很的幫助。
===================================================
-f
要求ssh 在背景執行命令,假如ssh要詢問密碼或通行證,可是使用者 想要它在幕後執行就能夠用這個方式,最好仍是加上-l user 例如在遠程場所上激活 X11,有點像是 ssh –f host xterm 。
===================================================
-i identity_file
選擇所讀取的 RSA 認證識別的檔案。預設是在使用者的家目錄 中的 .ssh/identity
===================================================
-n
重 導 stdin 到 /dev/null (其實是避免讀取 stdin)。必須當 ssh 在幕後執行時才使用。常見的招數是使用這選項在遠程機器上去執行 X11 的程序 例如,ssh -n shadows.cs.hut.fi emacs &,將在 shadows.cs.hut.fi 上激活 emace,而且 X11 鏈接將自動地在加密的信道上發送。ssh 程序將把它放 在幕後。(假如ssh須要去詢問密碼時,這將不會動做)
===================================================
-t
強制配置 pseudo-tty。這能夠在遠程機器上去執行任意的 screen-based 程 式,例如操做 menu services。
===================================================
-C
要 求壓縮全部資料(包含 stdin, stdout,stderr 和 X11 和 TCP/IP 鏈接) 壓縮演算規則與 gzip 相同,可是壓縮的等級不能控制。在調制解調器或 聯機速度很慢的地方,壓縮是個很好的選擇,但若是讀者的網絡速路很 快的話,速度反而會慢下來。
=====================================================
-p port
鏈接遠程機器上的 port。 不用這個選項,默認就是22
======================================================
-P
使用非特定的 port 去對外聯機。若是讀者的防火牆不淮許從特定的 port去聯機時,就可使用這個選項。注意這個選項會關掉 RhostsAuthentication 和 RhostsRSAAuthentication。
=====================================================
-L listen-port:host:port
指派本地的 port 到達端機器地址上的 port。
====================================================
-R listen-port:host:port
指派遠程上的 port 到本地地址上的 port。
-2 強制 ssh 去使用協議版本 2。
-4 強制 ssh 去使用 IPv4 地址。
-6 強制 ssh 去使用 IPv6 地址。
=====================================================
-g
容許遠程主機去鏈接本地指派的 ports。
-a
關閉認證代理聯機。
-e character
設定跳脫字符
scp 使用 scp 在遠程機器上 copy 檔案
======================================================
copy 本地的檔案到遠程的機器上
scp /etc/lilo.conf my@www.linuxidc.net:/home/my
會將本地的 /etc/lilo.conf 這個檔案 copy 到 www.linuxidc.net,使用者my 的家目錄下。
=====================================================
copy遠程機器上的檔案到本地來
scp my@www.linuxidc.net:/etc/lilo.conf /etc
會將 http://www.linuxidc.net 中 /etc/lilo.conf 檔案 copy 到本地的 /etc 目錄下。
=====================================================
保持歷來源 host 檔案的屬性
scp –p my@www.linuxidc.net:/etc/lilo.conf /etc
ssh-keygen
產生公開鑰 (pulib key) 和私人鑰 (private key),以保障 ssh 聯機的安性.
當 ssh 連 shd 服務器,會交換公開鑰上,系統會檢查 /etc/ssh_know_hosts 內儲存的 key,若是找到客戶端就用這個 key 產生一個隨機產生的session key 傳給服務器,兩端都用這個 key 來繼續完成 ssh 剩下來的階段。
它會產生 identity.pub、identity 兩個檔案,私人鑰存放於identity,公開鑰 存放於 identity.pub 中,接下來使用 scp 將 identity.pub copy 到遠程機器的家目錄下.ssh下的authorized_keys。 .ssh/authorized_keys(這個 authorized_keys 檔案至關於協議的 rhosts 檔案),以後使用者可以不用密碼去登入。RSA的認證絕對是比 rhosts 認證更來的安全可靠。
執行:
scp identity.pub my@88181.com:.ssh/authorized_keys
若在使用 ssh-keygen 產生鑰匙對時沒有輸入密碼,則如上所示不需輸入密碼便可從 http://www.linuxidc.net去登入 88181.com。
在此,這裏輸入的密碼能夠跟賬號的密碼不一樣,也能夠不輸入密碼。
SSH protocol version 1:
每一部主機均可以使用 RSA 加密方式來產生一個 1024-bit 的 RSA Key ,這個 RSA 的加密方式,主要就是用來產生公鑰與私鑰的演算方法!這個 version 1 的整個聯機的加密步驟能夠簡單的這麼看:
1. 當每次 SSH daemon (sshd) 激活時,就會產生一支 768-bit 的公鑰(或稱爲 server key)存放在 Server 中;
2. 如有 client 端的需求傳送來時,那麼 Server 就會將這一支公鑰傳給 client ,而 Client 藉由比對自己的 RSA 加密方式來確認這一支公鑰;
3. 在 Client 接受這個 768-bit 的 server key 以後,Client 本身也會隨機產生一支 256-bit 的私鑰(host key),而且以加密的方式將 server key 與 host key 整合成一支完整的 Key ,而且將這支 Key 也傳送給 server ;
4. 以後,Server 與 Client 在此次的聯機當中,就以這一支 1024-bit 的 Key 來進行資料的傳遞!
固然啦,由於 Client 端每次的 256-bit 的 Key 是隨機取的,因此你此次的聯機與下次的聯機的 Key 就會不同啦!
==============================================
SSH protocol version 2:
與 version 1 不一樣的是,在 version 2 當中將再也不產生 server key 了,因此,當 Client 端聯機到 Server 端時,二者將藉由 Diffie-Hellman key 的演算方式來產生一個分享的 Key ,以後二者將藉由相似 Blowfish 的演算方式進行同步解密的動做!
每個 sshd 都提供這兩個版本的聯機,而決定這兩種模式聯機的,就必須要在 client 端聯機時選擇聯機的模式才能確認。目前預設狀況下,會自動使用 version 2 的聯機模式喔!而因爲咱們的聯機資料中,通過了這個 Public 與 Private Key 的加密、解密動做,因此在中間的傳送過程當中,固然就比較安全的多囉!
若是直接以 ssh hostname 來鏈接進入 hostname 這個主機時,則進入 hostname 這個主機的『賬號名稱』將會是目前你所在的這個環境當中的使用者賬號!以上面爲例,由於我是以 root 的身份在執行,因此若是我執行了『 ssh host.domain.name 』時,那麼對方 host.domain.name 這部主機,就會以 root 的身份來讓我進行密碼確認的登入動做!所以,爲了不這樣的麻煩,一般我都是以簡單的 e-mail 的寫法來登入遠方的主機,例如『ssh user@hostname 』即表示,我是以 user 這個賬號去登入 hostname 這部主機的意思。固然,也可使用 -l username 這樣的形式來書寫!登入對方主機以後,其它的全部執行行爲都跟在 Linux 主機內沒有兩樣~因此,真的是很簡單吧! ^_^ 這樣就能夠達到遠程控管主機的目的了!此外,在預設的狀況下, SSH 是『容許你以 root 的身份登入』喔!呵呵!更是爽快啦!此外,請特別留意的是,當您要鏈接到對方的主機時,若是是首次鏈接,那麼 Server 會問你,你的聯機的 Key 還沒有被創建,要不要接受 Server 傳來的 Key ,並創建起聯機呢?呵呵!這個時候請『務必要輸入 yes 而不是 y 或 Y』,這樣程序纔會接受
sftp -l username hostname 或者 sftp user@hosname
進入到 sftp 以後,那就跟在通常 FTP 模式下的操做方法沒有兩樣
cd
ls dir
mkdir
rmdir
pwd
chgrp
chown
chmod
ln oldname newname
rm path
rename oldname newname
exit bye
===================================
lcd path
lls
lmkdir
lpwd
put [local] [remote]
get [remote] [local]
SSH命令使用技巧.txt
一 前言
關於 ssh 的好處, 相信不用我多說了吧?
簡而言之, 以前的 rpc command 與 telnet 都全可用 ssh 代替.
比方以下的這些常見功能:
- 遠程登陸
ssh user@remote.machine
- 遠程執行
ssh user@remote.machine 'command ...'
- 遠程複製
scp user@remote.machine:/remote/path /local/path
scp /local/path user@remote.machine:/remote/path
- X forward
ssh -X user@remote.machine
xcommand ...
- Tunnel / Portforward
ssh -L 1234:remote.machine:4321 user@remote.machine
ssh -R 1234:local.machine:4321 user@remote.machine
ssh -L 1234:other.machine:4321 user@remote.machine
二, 實做
1) 禁止 root 登陸
# vi /etc/ssh/sshd_config
PermitRootLogin no
2) 廢除密碼登陸, 強迫使用 RSA 驗證(假設 ssh 帳戶爲 user1 )
# vi /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
# service sshd restart
# su - user1
$ mkdir ~/.ssh 2>/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 端後修改之, 使其內容成爲單一一行.)
---------------------------------------------------php
回到 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 ōnerr=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 < AuthName "ssh_open" AuthUserFile /var/www/html/ssh_open/.htpasswd AuthType basic require valid-user END # htpasswd -c /var/www/html/ssh_open/.htpasswd user1 (最好還將 SSL 設起來, 或只限 https 聯機更佳, 我這裏略過 SSL 設定, 請讀者自補.) (如需控制聯機來源, 那請再補 Allow/Deny 項目, 也請讀者自補.) # cat > /var/www/html/ssh_open/ssh_open.php < //Set dir path for ip list $dir_path="."; //Set filename for ip list $ip_list="ssh_open.txt"; //Get client ip $user_ip=$_SERVER['REMOTE_ADDR']; //allow specifying ip if needed if (@$_GET['myip']) { $user_ip=$_GET['myip']; } //checking IP format if ($user_ip==long2ip(ip2long($user_ip))) { //Put client ip to a file if(@!($file = fopen("$dir_path/$ip_list","w+"))) { echo "Permission denied!! "; 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 < #!/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin list_dir=/var/www/html/ssh_open list_file=$list_dir/ssh_open.txt chain_name=ssh_rules mail_to=root # clear chain if exits, or create chain. iptables -L -n | /bin/grep -q "^Chain $chain_name" && { iptables -F $chain_name true } || { iptables -N $chain_name iptables -I INPUT -p tcp --dport 22 -j $chain_name } # clear chain when needed [ "$1" = clear ] && { iptables -F $chain_name exit 0 } # do nothing while list is empty [ -s $list_file ] || exit 1 # add rule iptables -A $chain_name -p tcp --dport 22 -s $(< $list_file) -j ACCEPT && \ echo "ssh opened to $(< $list_file) on $(date)" | mail -s "sshopen" $mail_to END # chmod +x /etc/iptables/sshopen.sh # echo -e 'sshopen\t\t1234/tcp' >> /etc/services # cat > /etc/xinetd.d/sshopen < service sshopen { disable = no socket_type = stream protocol = tcp wait = no user = root server = /etc/iptables/sshopen.sh } # iptables -I INPUT -p tcp --dport 1234 -j ACCEPT # cat > /etc/cron.d/sshopen < */5 * * * * root /etc/iptables/sshopen.sh clear END --------------------------- 轉往 client 端 在 browser URL 輸入: http://server.machine/ssh_open/ssh_open.php?myip=1.2.3.4 (若不指定 ?myip=1.2.3.4 則以 client 當時 IP 爲準, 若沒經 proxy 的話.) 如此, server 端的 ssh_open.txt 文件只有單一記錄, 每次蓋寫. 接着: $ telnet server.machine 1234 而後你有最多 5 分鐘時間用 ssh 聯機 server ! --------------------------- 此步驟的基本構思以下: 5.1) 將 sshd 的 firewall 聯機所有 block 掉. 5.2) 而後在 httpd 那設一個 directory, 可設 ssl+htpasswd+allow/deny control, 而後在目錄內寫一個 php 將 browser ip 記錄於一份 .txt 文字文件裏. 視你的轉寫能力, 你可自動抓取 browser 端的 IP, 也可以讓 browser 端傳入參數來指定. 文字文件只有單一記錄, 每次蓋寫. 5.3) 修改 /etc/services , 增長一個新項目(如 xxx), 並指定一個新 port(如 1234) 5.4) 再用 xinetd 監聽該 port , 並啓動令一隻 scrīpt, 設定 iptables , 從 step2 的清單裏取得 IP, 爲之打開 ssh 聯機. 5.5) 設 crontab 每數分中清理 iptables 關於 ssh 聯機的規則. 這並不影響既有聯機, 若逾時再連, 則重複上述. 6) 要是上一步驟沒設定, 你或許會擔憂過多的人來 try 你的 ssh 服務的話: # cat > /etc/iptables/sshblock.sh < #!/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin LOG_FILE=/var/log/secure KEY_WORD="Illegal user" KEY_WORD1="Failed password for root" PERM_LIST=/etc/firewall/bad.list.perm LIMIT=5 MAIL_TO=root IPT_SAV="$(iptables-save)" bad_list=$(egrep "$KEY_WORD" $LOG_FILE | awk '{print $NF}' | xargs) bad_list1=$(egrep "$KEY_WORD1" $LOG_FILE | awk '{print $11}' | xargs) bad_list="$bad_list $bad_list1" for i in $(echo -e "${bad_list// /\n}" | sort -u) do hit=$(echo $bad_list | egrep -o "$i" | wc -l) [ "$hit" -ge "$LIMIT" ] && { echo "$IPT_SAV" | grep -q "$i .*-j DROP" || { echo -e "\n$i was dropped on $(date)\n" | mail -s "DROP by ${0##*/}: $i" $MAIL_TO iptables -I INPUT -s $i -j DROP } egrep -q "^$i$" $PERM_LIST || echo $i >> $PERM_LIST } done END # chmod +x /etc/firewall/sshblock.sh # cat >> /etc/hosts.allow < sshd: ALL: spawn ( /etc/firewall/sshblock.sh )& : ALLOW END 這樣, 那些亂 try SSH 的傢伙, 頂多能試 5 次(LIMIT 可調整), 而後就給 BLOCK 掉了. 此外, 在 PERM_LIST 的 ip, 也可提供給 iptables 的初始 scrīpt , 來個永久性封閉: for i in $(< $PERM_LIST) do /sbin/iptables -I INPUT -s $i -j DROP done 7) 還有, 你想知道有哪些人對你作 full range port scan 的話: # iptables -I INPUT -p tcp --dport 79 -j ACCEPT cat > /etc/xinetd.d/finger < service finger { socket_type = stream wait = no user = nobody server = /usr/sbin/in.fingerd disable = no } END # cat >> /etc/hosts.allow < in.fingerd: ALL : spawn ( echo -e "\nWARNING %a was trying finger.\n$(date)" | mail -s "finger from %a" root ) & : DENY END 這裏, 我只是設爲發信給 root. 事實上, 你可修改成起動 firewall 將 %a 這個傳回值給 ban 掉也行. 不過, 對方要是有選擇性的作 port scan , 沒掃到 finger 的話, 那固然就沒用了... 本篇文章來源於 Linux公社網站(www.linuxidc.com) 原文連接:http://www.linuxidc.com/Linux/2008-02/11055p2.htm