编程小贴士

给你的编程提供小点子


SSH日常用法小例

1. 最普通登陆方法,
ssh root@192.168.1.200
ssh -l root 192.168.1.200

即:ssh username@hostIPorName 或 ssh -l username hostIPorName

2. 使用主机别名,简化登陆
修改~/.ssh/config文件(如没有该文件,创建之),添加如下内容:
host server1
hostname 192.168.1.200
user root

host server2
hostname 192.168.1.220
user root

指定了server1是192.168.1.200的别名,使用root用户;server2是192.168.1.220的别名,root用户。所以登陆操作简化为:
ssh server1 #等价于
ssh root@192.168.1.200

ssh lhzhi@server1 # 等价于
ssh lhzhi@192.168.1.200

3. ssh公私钥认证,继续简化登陆
公私钥认证是个伟大的发明,linux上使用ssh-keygen生成密钥,然后将公钥传到ssh服务器上:
ssh-keygen -f ~/.ssh/test -P”testpw” -t rsa
ssh-copy-id -i ~/.ssh/test.pub server1
ssh-copy-id -i ~/.ssh/test.pub server2

第一个命令会生成一对密钥,保存在~/.ssh/目录下,test是私钥,test.pub是公钥。解开私钥的密码是上面指定的testpw,密钥的是生成算法是rsa。
后面两个命令ssh-copy-id把公钥传到server1和server2上以后,就能够登陆server1和server2,而不需要输入密码,但是有可能需要输入解开密钥对的密码,在我的ubuntu上,只是每次开机之后第一次解开密钥输入解开密钥的密码,以后再不用输入。

4. 携带认证代理登陆
现在已经把公钥上传到server1和server2,那么从server1登陆server2依然要输入server2的密码,那有什么办法可以避免这次密码输入呢?使用ssh的-A参数即可。
过程是:
ssh -A server1 #登陆server1
ssh root@192.168.1.220 # 从server1免密码登陆server2

-A 参数登陆的时候会携带认证代理,具体是什么意思不清楚,我的理解是携带了私钥,导致不用输入密码。 这种办法很适合做跳板机登陆内部服务器。

5. 嘘!翻墙
GFW屏蔽了很多境外网站,如youtube、facebook、twitter等,大家都知道可以vpn访问。除此之外还有更简单的方法—-ssh,ssh是加密传输的,因此GFW对它无能为力。具体原理是建立一条ssh通道,然后,所有进出的数据都通过该加密通道传递。
前提是你有一台能连接境外网站的ssh服务器。实现翻墙的办法是在ssh中加入-D参数,另外需要浏览器代理软件配合。chrome下使用SwitchySharp,firefox下使用autoproxy。假如,这台境外服务器是serverproxy。
ssh -D 7070 serverproxy

-D 参数指定了本地7070为动态应用层转发端口。在翻墙过程中,不能断开该连接!
注意:在代理软件中选择 socks 5,端口选择7070。然后恭喜你,突破了长城,可以在浏览器里打开各种被屏蔽的网站,嘘,千万别说是我教你的!

6. scp传输文件
scp是最好用的数据传输办法之一,上传下载都很方便,使用ssh的22端口传递。scp比如本地有一个文本文件的目录/root/aaadir,把这个目录传到远程服务器/var目录下的方法是:
scp -rC /root/aaadir server1:/var/

参数解释:
-r 由于是传输的一个目录,所以使用-r递归传输目录下的所有文件
-C 为了加快传输速度,使用-C压缩传输
注意:如果使用scp输入密码之后报错“bash: scp: command not found”,很可能是远程服务器没有安装openssh-clients,安装之后就可以了!

7. sftp 传输文件
ftp是一个很有用的工具,但是在没有搭建ftp的环境中要使用ftp也是能办到的。sftp就是一个很好的工具,里面有类似ftp里的put和get等等很多命令,而且是基于ssh实现的!
登陆办法是:
sftp server2

然后像使用ftp一样可以进行上传,删除,下载等操作了。十分简单吧!!!

8.杂记
还有一些小问题值得注意,如果登陆过程发现速度忙或者等问题,可以使用ssh -v 来显示详细的登陆过程信息。
为了安全,有些管理人员会修改默认ssh服务器的默认端口号,避免被扫描到,这是登陆ssh服务器就要使用-p参数指定修改过的端口号!
ssh还有很多高级的用法,但以上是我日常中常用到的,有其它需求可以查看ssh命令的man手册,里面应有尽有!

115 Responses to “ SSH日常用法小例 ”

  1. suba suba说道:

    GSSPiS This very blog is definitely awesome and besides factual. I have chosen a lot of helpful advices out of this blog. I ad love to go back every once in a while. Thanks!

  2. topic. Do you know how to make your site mobile

  3. Valued Personal Traits Hello, you used to write great, but the last several posts have been kinda boring I miss your great writings. Past few posts are just a bit out of track! come on!

  4. xem tu vi 2019说道:

    Im no professional, but I imagine you just made an excellent point. You definitely comprehend what youre talking about, and I can truly get behind that. Thanks for being so upfront and so genuine.

  5. animal coloring说道:

    It’а†s actually a cool and helpful piece of info. I am satisfied that you just shared this useful information with us. Please keep us up to date like this. Thank you for sharing.

  6. Smokers Lungs说道:

    It is truly a great and useful piece of info. I am happy that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.

  7. Thanks for sharing, this is a fantastic article.Much thanks again. Fantastic.

  8. you modify it yourself? Either way stay up the nice quality writing, it is

  9. montaj krovli说道:

    Major thanks for the article post.Really thank you! Great.

  10. Click to read说道:

    It as really a great and useful piece of information. I am glad that you shared this useful information with us. Please keep us informed like this. Thank you for sharing.

  11. My Face Gifts说道:

    Thanks for sharing, this is a fantastic blog.Really thank you! Keep writing.

  12. web说道:

    I truly appreciate this post.Really looking forward to read more. Great.

  13. You completed a few fine points there. I did a search on the subject and found the majority of people will agree with your blog.

  14. When are you going to post again? You really entertain me!

  15. visit website说道:

    You ave made some really good points there. I checked on the web to find out more about the issue and found most individuals will go along with your views on this website.

  16. I truly appreciate this post. I have been looking all over for this! Thank God I found it on Google. You ave made my day! Thx again..

  17. for more info说道:

    Very neat blog article.Thanks Again. Cool.

  18. private Hire说道:

    Very interesting details you have mentioned , regards for putting up.

  19. Thank you ever so for you post.Really thank you! Want more.

  20. It is challenging to acquire knowledgeable people with this topic, nevertheless, you appear like there as extra you are referring to! Thanks

  21. Ellis Porter说道:

    It as exhausting to find educated people on this topic, but you sound like you understand what you are talking about! Thanks

  22. this website说道:

    Thanks for the post.Much thanks again. Cool.

  23. I would like to uslysht just a little more on this topic

  24. More Bonuses说道:

    Thank you ever so for you article post.Much thanks again. Awesome.

  25. Nwokolo说道:

    It as not that I want to duplicate your web page, but I really like the layout. Could you tell me which theme are you using? Or was it tailor made?

  26. klm coupon code说道:

    Very informative blog article.Thanks Again. Great.

  27. Very nice post. I just stumbled upon your blog and wished to say that I ave really enjoyed surfing around your blog posts. After all I all be subscribing to your feed and I hope you write again soon!

  28. Keep the excellent function, I study few websites on this amazing site and My partner and i conceive that your web site is actually interesting and possesses lots involving excellent info.

  29. Muchos Gracias for your blog.Thanks Again. Keep writing.

  30. You, my friend, ROCK! I found just the information I already searched all over the place and just could not locate it. What a perfect website.

  31. metal recycling说道:

    Thank you, I have just been searching for information about this subject for ages and yours is the best I ave discovered till now. But, what about the conclusion? Are you sure about the source?

  32. mp4 download说道:

    Some truly interesting info , well written and broadly user genial.

  33. whoah this blog is fantastic i love reading your posts. Keep up the great work! You know, lots of people are looking around for this information, you can aid them greatly.

  34. You made some decent points there. I did a search on the topic and found most individuals will agree with your blog.

  35. My brother recommended I might like this website. He was entirely right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  36. gambling online说道:

    you can even cross your pinkies. In about three weeks ago I

  37. visit website说道:

    I really liked your blog.Really looking forward to read more. Want more.

  38. pussy cum说道:

    You created some respectable factors there. I seemed on the net for the problem and located many people will go along with together with your internet site.

  39. I think other website proprietors should take this website as an model, very clean and magnificent user genial style and design, let alone the content. You are an expert in this topic!

  40. Tattoo Artists说道:

    Say, you got a nice blog.Really looking forward to read more. Want more.

  41. I think this is a real great blog post.Thanks Again. Really Cool.

  42. Whats up. Very nice site!! Man.. Beautiful.. Wonderful.. I all bookmark your blog and take the feeds alsoI am satisfied to locate numerous useful info here within the post. Thank you for sharing

  43. I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thx again

  44. Miracle Ndu说道:

    Regards for helping out, great information.

  45. Vs model说道:

    Thanks for the post.Really thank you! Really Cool.

  46. Pretty! This was a really wonderful post. Thank you for supplying this info.

  47. healthy说道:

    Major thanks for the blog article.Thanks Again. Awesome.

  48. Well I really enjoyed reading it. This subject provided by you is very constructive for good planning.

  49. maeng da kratom说道:

    wonderful challenges altogether, you simply gained a logo reader. What would you suggest about your publish that you just made some days ago? Any sure?

  50. This particular blog is without a doubt interesting and besides factual. I have picked up helluva interesting things out of this source. I ad love to return again soon. Thanks a lot!

  51. Im grateful for the article post.Much thanks again. Want more.

  52. Blusa Tejidas说道:

    I used to be able to find good advice from your blog articles.|

  53. Arctic Circle说道:

    Really enjoyed this blog.Really looking forward to read more. Keep writing.

  54. local说道:

    the primary way to maximize SEO for a web site.

  55. blue nike说道:

    It as hard to come by knowledgeable people in this particular topic, but you seem like you know what you are talking about! Thanks

  56. more说道:

    you made blogging look easy. The overall look of your site is fantastic, as well

  57. hampton bay说道:

    make men and women think. Also, thank you for permitting me to comment!

  58. Wow, great blog article.Really thank you! Great.

  59. School admission说道:

    Inspiring quest there. What happened after? Good luck!

  60. School news说道:

    You have remarked very interesting details ! ps nice web site. Justice is the truth in action. by Jeseph Joubert.

  61. pretty useful material, overall I think this is well worth a bookmark, thanks

  62. Really enjoyed this article post.Really thank you!

  63. This page truly has all of the info I wanted concerning this subject and didn at know who to ask.

  64. Saw your material, and hope you publish more soon.

  65. Moskito说道:

    Im thankful for the article post.Thanks Again. Will read on

  66. Interesting post , I am going to spend a lot more time learning about this subject

  67. wonderful issues altogether, you just won a new reader. What could you suggest about your publish that you made some days ago? Any positive?

  68. I think this is a real great blog post.Really looking forward to read more. Cool.

  69. We stumbled over here by a different website and thought I might as well check things out.

  70. I truly appreciate this blog post.Really thank you!

  71. Really appreciate you sharing this blog article. Cool.

  72. marketing说道:

    Thanks for sharing this great piece. Very inspiring! (as always, btw)

  73. What as up, I check your blog daily. Your story-telling style is witty, keep up the good work!

  74. free browsing说道:

    Wow, great blog.Thanks Again. Keep writing.

  75. out there right now. (from what I ave read) Is that what you are using on

  76. Thanks so much for the article.Much thanks again. Keep writing.

  77. This is really interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your magnificent post. Also, I have shared your website in my social networks!

  78. Usually I don at read post on blogs, however I wish to say that this write-up very pressured me to try and do it! Your writing style has been surprised me. Thanks, very nice article.

  79. nonetheless, you command get bought an shakiness over

  80. Usa Online Casinos view of Three Gorges | Wonder Travel Blog

  81. Thanks-a-mundo for the blog article.Really looking forward to read more. Keep writing.

  82. I truly appreciate this post. I?аАТ’аЂа†ve been looking all over for this! Thank goodness I found it on Bing. You ave made my day! Thank you again

  83. Billy graham说道:

    This is just what I ave been looking for all day long. Don at stop updating your blog.

  84. for more details说道:

    On the outside its measures are an even dozen point-five inches in diameter on six point-five toaster oven reviews centimeters heavy.

  85. write for us说道:

    Keep up the superb piece of work, I read few articles on this web site and I think that your web blog is very interesting and has got sets of great info.

  86. website a lot of times previous to I could get it to load properly.

  87. more info说道:

    Wow! This could be one particular of the most useful blogs We ave ever arrive across on this subject. Actually Excellent. I am also an expert in this topic therefore I can understand your effort.

  88. see说道:

    Say, you got a nice blog post.Really looking forward to read more. Really Great.

  89. for more info说道:

    Whats up this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if

  90. Magnificent website. Lots of helpful info here. I’а†m sending it to a few friends ans also sharing in delicious. And certainly, thanks on your sweat!

  91. Your style is so unique in comparison to other people I ave read stuff from. Many thanks for posting when you have the opportunity, Guess I all just bookmark this web site.

  92. twitter说道:

    Say, you got a nice blog post. Really Cool.

  93. click here说道:

    In fact, a systematic graphical representation is done by professional designers who have creative bent

  94. Usually I don at read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, quite nice article.

  95. post an ad说道:

    We stumbled over here from a different web address and thought I might as well check things out. I like what I see so now i am following you. Look forward to going over your web page again.

  96. take a look at说道:

    your website, how can i subscribe for a blog website? The

  97. see more说道:

    Keep up the superb piece of work, I read few posts on this website and I believe that your site is really interesting and has got circles of great info.

  98. Superb, what a web site it is! This web site gives valuable information to us, keep it up.

  99. caratulas CD说道:

    your placement in google and could damage your quality score if advertising

  100. website说道:

    I will bookmark your blog and check once more right here regularly.

  101. Thanks so much for the post. Will read on

  102. It is in reality a nice and useful piece of info. I am satisfied that you shared this helpful tidbit with us. Please keep us informed like this. Thank you for sharing.

  103. slot joker123说道:

    I will right away snatch your rss as I can not in finding your email subscription link or newsletter service. Do you have any? Please let me recognize in order that I may just subscribe. Thanks.

  104. buat akun pb说道:

    Would you be interested in trading links or maybe guest writing a blog post or vice-versa?

  105. go there说道:

    I think other site proprietors should take this site as an model, very clean and wonderful user friendly style and design, as well as the content. You are an expert in this topic!

  106. I wish people would compose much more about this while you have done. This is something which is very essential and possesses been largely overlooked through the world wide web local community

  107. Well I sincerely enjoyed studying it. This tip procured by you is very useful for good planning.

  108. va loans with bad credit How can I copyright all original content current and future on my website?

  109. What as up every one, here every one is sharing these knowledge, thus it as fastidious to read this webpage, and I used to pay a visit this blog everyday.

  110. genuine parts说道:

    It as going to be ending of mine day, except before end

  111. click here说道:

    Wow! Thank you! I permanently needed to write on my site something like that. Can I implement a part of your post to my blog?

  112. sunday说道:

    Really enjoyed this blog post.Much thanks again. Cool.

  113. Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?

  114. Thank you ever so for you article post.Really thank you! Awesome.

  115. Wow! This blog looks just like my old one! It as on a completely different subject but it has pretty much the same page layout and design. Great choice of colors!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>