顯示具有 ssh 標籤的文章。 顯示所有文章
顯示具有 ssh 標籤的文章。 顯示所有文章

2011年5月8日 星期日

25 Best SSH Commands / Tricks

http://blog.urfix.com/25-ssh-commands-tricks/



25 Best SSH Commands / Tricks
Friday, November 19, 2010

OpenSSH is a FREE version of the SSH connectivity tools that technical users of the Internet rely on. Users of telnet, rlogin, and ftp may not realize that their password is transmitted across the Internet unencrypted, but it is. OpenSSH encrypts all traffic (including passwords) to effectively eliminate eavesdropping, connection hijacking, and other attacks. Additionally, OpenSSH provides secure tunneling capabilities and several authentication methods, and supports all SSH protocol versions.

SSH is an awesome powerful tool, there are unlimited possibility when it comes to SSH, heres the top Voted SSH commands


1) Copy ssh keys to user@host to enable password-less ssh logins.

ssh-copy-id user@host
To generate the keys use the command ssh-keygen

2) Start a tunnel from some machine’s port 80 to your local post 2001

ssh -N -L2001:localhost:80 somemachine
Now you can acces the website by going to http://localhost:2001/

3) Output your microphone to a remote computer’s speaker

dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp
This will output the sound from your microphone port to the ssh target computer’s speaker port. The sound quality is very bad, so you will hear a lot of hissing.

4) Compare a remote file with a local file

ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
Useful for checking if there are differences between local and remote files.

5) Mount folder/filesystem through SSH

sshfs name@server:/path/to/folder /path/to/mount/point
Install SSHFS from http://fuse.sourceforge.net/sshfs.html
Will allow you to mount a folder security over a network.

6) SSH connection through host in the middle

ssh -t reachable_host ssh unreachable_host
Unreachable_host is unavailable from local network, but it’s available from reachable_host’s network. This command creates a connection to unreachable_host through “hidden” connection to reachable_host.

7) Copy from host1 to host2, through your host

ssh root@host1 “cd /somedir/tocopy/ && tar -cf – .” | ssh root@host2 “cd /samedir/tocopyto/ && tar -xf -”
Good if only you have access to host1 and host2, but they have no access to your host (so ncat won’t work) and they have no direct access to each other.

8) Run any GUI program remotely


ssh -fX @
The SSH server configuration requires:
X11Forwarding yes # this is default in Debian
And it’s convenient too:
Compression delayed

9) Create a persistent connection to a machine

ssh -MNf @
Create a persistent SSH connection to the host in the background. Combine this with settings in your ~/.ssh/config:
Host host
ControlPath ~/.ssh/master-%r@%h:%p
ControlMaster no
All the SSH connections to the machine will then go through the persisten SSH socket. This is very useful if you are using SSH to synchronize files (using rsync/sftp/cvs/svn) on a regular basis because it won’t create a new socket each time to open an ssh connection.

10) Attach screen over ssh

ssh -t remote_host screen -r
Directly attach a remote screen session (saves a useless parent bash process)

11) Port Knocking!

knock 3000 4000 5000 && ssh -p user@host && knock 5000 4000 3000
Knock on ports to open a port to a service (ssh for example) and knock again to close the port. You have to install knockd.
See example config file below.
[options]
logfile = /var/log/knockd.log
[openSSH]
sequence = 3000,4000,5000
seq_timeout = 5
command = /sbin/iptables -A INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPT
tcpflags = syn
[closeSSH]
sequence = 5000,4000,3000
seq_timeout = 5
command = /sbin/iptables -D INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPT
tcpflags = syn

12) Remove a line in a text file. Useful to fix

ssh-keygen -R
In this case it’s better do to use the dedicated tool

13) Run complex remote shell cmds over ssh, without escaping quotes

ssh host -l user $(<cmd.txt) 
Much simpler method. More portable version: ssh host -l user “`cat cmd.txt`”

14) Copy a MySQL Database to a new Server via SSH with one command

mysqldump –add-drop-table –extended-insert –force –log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost “mysql -uUSER -pPASS NEW_DB_NAME”
Dumps a MySQL database over a compressed SSH tunnel and uses it as input to mysql – i think that is the fastest and best way to migrate a DB to a new server!

15) Remove a line in a text file. Useful to fix “ssh host key change” warnings

sed -i 8d ~/.ssh/known_hosts

16) Copy your ssh public key to a server from a machine that doesn’t have ssh-copy-id

cat ~/.ssh/id_rsa.pub | ssh user@machine “mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys”
If you use Mac OS X or some other *nix variant that doesn’t come with ssh-copy-id, this one-liner will allow you to add your public key to a remote machine so you can subsequently ssh to that machine without a password.

17) Live ssh network throughput test

yes | pv | ssh $host “cat > /dev/null”
connects to host via ssh and displays the live transfer speed, directing all transferred data to /dev/null
needs pv installed
Debian: ‘apt-get install pv’
Fedora: ‘yum install pv’ (may need the ‘extras’ repository enabled)

18) How to establish a remote Gnu screen session that you can re-connect to

ssh -t user@some.domain.com /usr/bin/screen -xRR
Long before tabbed terminals existed, people have been using Gnu screen to open many shells in a single text terminal. Combined with ssh, it gives you the ability to have many open shells with a single remote connection using the above options. If you detach with “Ctrl-a d” or if the ssh session is accidentally terminated, all processes running in your remote shells remain undisturbed, ready for you to reconnect. Other useful screen commands are “Ctrl-a c” (open new shell) and “Ctrl-a a” (alternate between shells). Read this quick reference for more screen commands: http://aperiodic.net/screen/quick_reference

19) Resume scp of a big file

rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file
It can resume a failed secure copy ( usefull when you transfer big files like db dumps through vpn ) using rsync.
It requires rsync installed in both hosts.
rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file local -> remote
or
rsync –partial –progress –rsh=ssh $user@$host:$remote_file $destination_file remote -> local

20) Analyze traffic remotely over ssh w/ wireshark

ssh root@server.com ‘tshark -f “port !22″ -w -’ | wireshark -k -i -
This captures traffic on a remote machine with tshark, sends the raw pcap data over the ssh link, and displays it in wireshark. Hitting ctrl+C will stop the capture and unfortunately close your wireshark window. This can be worked-around by passing -c # to tshark to only capture a certain # of packets, or redirecting the data through a named pipe rather than piping directly from ssh to wireshark. I recommend filtering as much as you can in the tshark command to conserve bandwidth. tshark can be replaced with tcpdump thusly:
ssh root@example.com tcpdump -w – ‘port !22′ | wireshark -k -i -

21) Have an ssh session open forever

autossh -M50000 -t server.example.com ‘screen -raAd mysession’
Open a ssh session opened forever, great on laptops losing Internet connectivity when switching WIFI spots.

22) Harder, Faster, Stronger SSH clients

ssh -4 -C -c blowfish-cbc
We force IPv4, compress the stream, specify the cypher stream to be Blowfish. I suppose you could use aes256-ctr as well for cypher spec. I’m of course leaving out things like master control sessions and such as that may not be available on your shell although that would speed things up as well.

23) Throttle bandwidth with cstream

tar -cj /backup | cstream -t 777k | ssh host ‘tar -xj -C /backup’
this bzips a folder and transfers it over the network to “host” at 777k bit/s.
cstream can do a lot more, have a look http://www.cons.org/cracauer/cstream.html#usage
for example:
echo w00t, i’m 733+ | cstream -b1 -t2

24) Transfer SSH public key to another machine in one step

ssh-keygen; ssh-copy-id user@host; ssh user@host
This command sequence allows simple setup of (gasp!) password-less SSH logins. Be careful, as if you already have an SSH keypair in your ~/.ssh directory on the local machine, there is a possibility ssh-keygen may overwrite them. ssh-copy-id copies the public key to the remote host and appends it to the remote account’s ~/.ssh/authorized_keys file. When trying ssh, if you used no passphrase for your key, the remote shell appears soon after invoking ssh user@host.

25) Copy stdin to your X11 buffer

ssh user@host cat /path/to/some/file | xclip
Have you ever had to scp a file to your work machine in order to copy its contents to a mail? xclip can help you with that. It copies its stdin to the X11 buffer, so all you have to do is middle-click to paste the content of that looong file :)


Have Fun

Please comment if you have any other good SSH Commands OR Tricks.

2011年2月10日 星期四

Omnitty 大量管理Linux主機

Omnitty簡化大量管理Linux主機與多台主機指令同步執行測試 By Keynes


http://keynes0918.wordpress.com/2010/09/12/omnitty%E7%B0%A1%E5%8C%96%E5%A4%A7%E9%87%8F%E7%AE%A1%E7%90%86linux%E4%B8%BB%E6%A9%9F%E8%88%87%E5%A4%9A%E5%8F%B0%E4%B8%BB%E6%A9%9F%E6%8C%87%E4%BB%A4%E5%90%8C%E6%AD%A5%E5%9F%B7%E8%A1%8C%E6%B8%AC/

批量管理多台linux的软件 Omnitty
http://myhat.blog.51cto.com/391263/453090

Installation Instructions
Installing Omnitty from source code involves the standard steps you are used to:

$ tar -zxf /path/to/rote-X.Y.Z.tar.gz
$ cd rote-X.Y.Z
$ ./configure
$ make
$ su -c "make install"
$ cd ..
$ tar -zxf /path/to/omnitty-X.Y.Z.tar.gz
$ cd omnitty-X.Y.Z
$ ./configure
$ make
$ su -c "make install"


If you do it this way, omnitty will get installed in /usr/local/bin, which you should put in your PATH if it is not already there. You can then run the program by typing omnitty at the prompt.

2010年9月1日 星期三

待練 shell,ssh

SSH 小技巧
http://cha.homeip.net/blog/2010/08/2623.html

最牛B的 Linux Shell 命令 系列连载
http://wowubuntu.com/linux_shell_1.html
http://wowubuntu.com/linux_shell_2.html
http://wowubuntu.com/linux_shell_3.html
http://wowubuntu.com/linux_shell_4.html

test ,練個script測 檔案權限...
http://hi.baidu.com/zheng918/blog/item/892bf9f28636cd1bb07ec5fc.html/cmtid/477efffa7aaa1ad3b58f3116
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#test

2009年12月10日 星期四

[盜貼]用 rsync 做資料同步,並利用 SSH 做安全加密





資料參考來源:




使用 rsync 做資料同步,並利用 SSH 做安全加密
 ( 適用RHEL4、Fedora Core 3、4 及 5 )
rsync 為 Linux 平台上做異地備援時,不可缺少的好工具,但在傳送時是以明碼傳送,一旦封包被攔劫,有被解讀的風險存在,因為我們要利用 SSH 幫我們在傳遞封包時,將資料加密,以避免被人解讀。


一、語法:
   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
   rsync [OPTION]... [USER@]HOST:SRC DEST
   rsync [OPTION]... SRC [SRC]... DEST
   rsync [OPTION]... [USER@]HOST::SRC [DEST]
   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

二、使用說明:
 1. 當來源端或目的端的路徑都未包含 : 這個分隔符號時,表示複製本地端的檔案。
2. 當目的端路徑包含單個 : 這個分隔符號時,表示複製本地端檔案到遠端機器 ( 透過 ssh 或 rsh )。
3. 當來源端路徑包含 : 這個分隔符號時,表示複製遠端檔案到本地端。
4. 當來源端路徑包含 :: 或 rsync:// 這個符號時,表示從遠端 rsync 伺服器複製檔案到本地端。
5. 當目的端路徑包含 :: 或 rsync:// 這個符號時,表示從本地端複製檔案到 rsync 伺服器。
6. 當來源端路徑包含 :: 和加上 --rsh=COMMANS ( -e COMMAND ) 參數,表示使用遠端 shell 程式 (remote shell),並透過遠端的 rsync 伺服器,複製遠端檔案到本地端。
7. 當目的端路徑抱含 :: 和加上 --rsh=COMMAND 參數,表示使用遠端 shell 程式並透過遠端的 rsync 伺服器,複製本地端檔案到遠端機器。
8. 列出遠端機器的檔案,跟 rsync 傳輸相同,只不過把本地端的目的地位置留空。

 注意: 在所有狀況下(列出遠端機器檔案除外),來源端及目的端的路徑最少要有一個是本地端路徑.
三、使用方式如下:
 # rsync -avlR --delete -e ssh 來源host_IP:/etc --exclude X11 --exclude httpd 目的位置
遠端主機下的 /etc 目錄,除了 X11 及 httpd 目錄外,其餘的與本地端做資料同步,並用 ssh 作為加密。
a : archive
v : verbose-msg
l : softlink
R : Relative Pathname

 --delete : 來源的資料如果沒有,則目的的資料一併刪除
 -e : command
 --exclude : 排除以外的目錄,需以相對路徑設定

 # rsync -t *.c 目的host_ip:/src
 將目前路徑下所有符合 *.c 的檔案,與目的主機的 /src 目錄做同步,並保留時間記錄。

 # rsync -avz 來源host_ip:/src/bar /data/tmp
將來源主機的 /src/bar 目錄,與本機 /data/tmp 目錄做同步。
-a 表示使用 archive 模式,保持原有的檔案屬性與權限。
-z 傳輸時壓縮資料。

 # rsync -avlr /var/lib/mysql/ /backup
 將本機 /var/lib/mysql 目錄下所有檔案(含子資料夾),同步一份到 /backup 下做為備份。

 在來源端後面加上一個 / 可避免在目的端的資料夾下額外建立一個資料夾。
例如:rsync -av /src/foo /dest 會在 /dest 下產生一個 foo 的資料夾,表示複製這個資料夾。
rsync -av /src/foo/ /dest 則不會在 /dest 下產生一個 foo 的資料夾,表示複製這個資料夾下的內容。

四、參數:
 -h , --help     顯示rsync求助資訊.

 --version      顯示rsync版本.
 -v , --verbose    複雜的輸出訊息.
 -q , --quiet      安靜模式,幾乎沒有訊息產生.常用在以cron執行rsync.
 -I, --ignore-times   通常rsync為了加快速度會忽略同樣檔案大小且同樣存取時間點的檔案.
可以透過此參數關閉此快速檢查.
--size-only     rsync只檢查檔案大小是否改變,不管時間存取點是否改變.
通常用在mirror,且對方時間不太正確時.

 -c, --checksum    在傳送之前透過檢查碼來檢查所有要傳送的檔案.(會拖慢速度.)
 -a, --archive    archive mode 權限保存模式,相當於 -rlptgoD 參數.
保存幾乎所有的權限設定,除了硬式連結(透過-H設定).

 -r, --recursive    複製所有下層的資料(遞迴)
 -R, --relative      使用相對路徑.
如 : rsync foo/bar/foo.c remote:/tmp/ 在遠端產生/tmp/foo.c檔案
rsync -R foo/bar/foo.c remote:/tmp/  在遠端產生/tmp/foo/bar/foo.c 檔案

 -b, --backup    目的地端先前已經存在的檔案在傳輸或刪除前會被備份.
  --backup-dir=DIR 設定備份的資料夾.
  --suffix=SUFFIX 指定備份的檔案名稱字尾形式(預設為~).

 -K, --keep-dirlinks  接收方將連結到資料夾的檔案視為資料夾處理
 -l, --links       複製所有的連結
 -H, --hard-links    保留硬式連結
 -p, --perms     保留檔案權限
 -o, --owner     保留檔案擁有者(root only)
 -g, --group      保留檔案群組
 -D, --devices     保留device資訊(root only)
 -t, --times     保留時間點
 -n, --dry-run    不實際執行傳送,只顯示將會有的傳輸動作
 -S, --sparse    嘗試去處理稀疏的檔案,讓這些檔案在目的端佔去較少的磁碟空間.
 -W, --whole-file     複製所有的檔案,不額外作檢查.
   --no-whole-file  關閉 --whole-file 參數

 -x, --one-file-system 不要跨越檔案系統分界(只在一個檔案系統處理)
 -B, --block-size=SIZE 強制透過rsync程式去比對修復block-sizeforce
 -e  --rsh=COMMAND 定義所使用的remote shell
 --rsync-path=PATH 定義rsync在遠端機器存放資料的路徑
--existing     只比對更新目的端已經存在的檔案
--ignore-existing    忽略目的端已經存在的檔案(也就是不更新)
--delete        刪除傳送端已經不存在,而目的端存在的檔案
--delete-excluded  除了把傳送端已經不存在,而目的端存在的檔案刪除之外,
也刪除 --exclude 參數所包含的檔案.
--delete-after    rsync預設會在檔案傳送前進行相關刪除動作確保接收端有足夠的檔案空間,
但可以透過 --delete-after 讓刪除動作在檔案傳送後再行刪除.
--ignore-errors    忽略任何錯誤既使是I/O error 也進行 --delete 刪除動作.
--max-delete=NUM 定義rsync不要刪除超過 NUM 個檔案.
--partial      rsync若遇到傳輸過程中斷時,會把那些已經傳輸的檔案刪除.
在某種狀況下保留那些部分傳送的檔案是令人高興的.
你可以透過 --partial 參數達到這個目的.
--partial-dir=DIR  在 --partial 參數啟動時,你還可以定義rsync把那些部分傳送的檔案寫入定義的資料夾,
  而非直接寫入目的端.需要注意的是,此資料夾不應該被其他使用者可以寫入.(如:/tmp)
--force       當目的端資料夾被傳送端非資料夾名稱覆蓋時,強制rsync刪除資料夾,即使該資料夾不是空的.
--numeric-ids      不將傳送端檔案的uid及gid值,與目的端的使用者/群組進行配對.
若傳送端並沒有uid及gid的對應名稱(如:原帳號群組被刪除的遺留檔案),
或目的端沒有相對應的帳號/群組,保留數字型態的uid/gid
--timeout=TIMEOUT 設定 I/O 逾時的時間(秒). 超過這個秒數而沒有資料傳送,rsync將會結束.
預設為0,也就是沒有定義逾時時間.

 -T, --temp-dir=DIR   定義rsync在接收端產生暫時性的複製檔案時使用資料夾暫存.
預設是直接在接收端資料夾直接產生暫存檔案.
--compare-dest=DIR 定義rsync在目的端建立資料夾來比對傳送過來的檔案.
--link-dest=DIR   與 --compare-dest 相同,但同時會針對無法改變的檔案建立硬式連結.

 -z, --compress       壓縮模式,當資料在傳送到目的端進行檔案壓縮.
 -P           -P參數和 --partial --progress 相同.只是為了把參數簡單化.
 -C, --cvs-exclude      排除那些通常不希望傳送的檔案.定義的方式與CVS傳送相同:
RCS SCCS CVS  CVS.adm  RCSLOG  cvslog.*  tags  TAGS  .make.state
.nse_depinfo  *~ #* .#* ,* _$* *$ *.old *.bak *.BAK *.orig *.rej
.del-* *.a *.olb *.o *.obj *.so *.exe *.Z *.elc *.ln core .svn/
符合以上條件的都會被忽略而不傳送.
--exclude=PATTER   符合PATTERN(規則表示式)樣式的檔案不進行傳送
--exclude-from=FILE 和--exclude參數相同,不過是把不進行傳送的檔案事先寫入某一檔案.
  執行時,透過此參數讓rsync讀取.(#開頭的行列或空白行會被rsync忽略)
--include=PATTERN 定義rsync不要排除符合pattern樣式的檔案.
--include-from=FILE  和--include參數相同,只不過把要包含的檔案寫到某一檔案.
--files-from=FILE     把要傳送的檔案名稱都精確的寫入某一檔案,讓rsync讀取.
   如: rsync -a --files-from=/tmp/foo /usr remote:/backup

 -0,--from0       定義檔案所要讀取的檔案是null字元結尾.
--version      顯示版本訊息.
--daemon       定義rsync以daemon型態執行.
--no-detach       當以daemon型態執行時,不要進行分裂且變成背景程序.
--address=ADDRESS 定義所要連結(bind)的ip位址或是host名稱(daemon限定)
--config=FILE    定義所要讀取的設定檔rsyncd.conf位置(daemon限定)
預設值為 /usr/local/etc/rsyncd.conf
--port=PORT     定義rsyncd(daemon)要執行的port(預設為tcp 873)                 
--blocking-io     使用blocking I/O連結遠端的shell,如rsh , remsh
--no-blocking-io     使用non-blocking連結遠端的shell,如ssh (預設值)
--stats       顯示檔案傳送時的資訊狀態
--progress     顯示傳送的進度.(給檔案傳送時,怕無聊的人用的..)
--log-format=FORMAT 定義log的格式(在rsyncd.conf設定)
--password-file=FILE 從檔案讀取與遠端rsync伺服器連結的密碼
--bwlimit=KBPS    定義傳輸頻寬的大小(KBytes/秒)
--write-batch=FILE  把紀錄資料寫入一個檔案(給其他相同環境且相同需求的機器使用)
--read-batch=FILE  透過讀取紀錄檔案來進行傳輸.(檔案由 --write-batch 參數產生)
--checksum-seed=NUM 定義檔案 checksum-seed 的大小(byte)

 -4, --ipv4       使用IPv4協定
 -6, --ipv6       使用IPv6協定


BloggerAds