Top and useful ssh tips
- copy ssh key to remote host
ssh-copy-id user@remote_host
2 map the tunnel the remote host 80 port to local host port 2001
ssh -N -L2001:localhost:80 remote_host
# now we can use localhost:2001 to reach remote_host 80
- compare the local file and remote file
ssh user@host cat /path/to/remotefile | diff /path/to/localfile –
- jump point to unreachable_host
ssh -t reachable_host ssh unreachable_host
- copy file from remote_host1 to remote_host2
# please keep in mind, you can reach both remote host
ssh root@remote_host1 "cd /somedir/tocopy/ && tar -cf – ." | ssh root@remote_host2 "cd /samedir/tocopyto/ && tar -xf -"
- use screen to keep session alive even disconnected.
# make sure remote_host has screen
ssh -t remote_host screen –r
- copy db to new db server with ssh
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"
- remove the “key change warning”
#actually, this is sed tips 🙂
sed -i 8d ~/.ssh/known_hosts
- copy the public key to remote host
cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
- copy huge files to remote host, keep rsync available from both side
# local -> remote
rsync -–partial -avP -–rsh=ssh $file_source $user@$host:$destination_file
# remote -> local
rsync –-partial -avP -–rsh=ssh $user@$host:$remote_file $destination_file