Chinaunix首页 | 论坛 | 博客
  • 博客访问: 589753
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类: LINUX

2009-04-25 07:37:45

With the vast majority of web developers deploying their own code, and ssh playing a central role in the toolbox, I felt that tweaking this part of my work saves me some time, and if not, definitely some frustration over mistyping passwords and host names.

So, for myself as much as everyone out there, here is my collected useful knowledge that has helped me ease the pain of remote work.

1. Execute commands remotely as one-liners

This is what some deployment tools such as use, but for a quick look at something, it cuts out valuable seconds you could look at web comics instead.

Let's try a simple example:


matthias:~ matthias$ ssh remoteuser@myserver.pretendco.com ls -l
remoteuser@myserver.pretendco.com's password: 
total 804
lrwxrwxrwx   1 remoteuser remoteuser      31 Nov 10  2007 access-logs -> /links/to/my/access-logs
drwxr-xr-x   2 remoteuser remoteuser    2048 Apr  6  2008 cgi-bin
-rw-r--r--   1 remoteuser remoteuser  808417 Jul 17  2007 error_log_dump
drwxr-x---   3 remoteuser mail     2048 Oct  1  2007 etc
drwxrwx---   7 remoteuser remoteuser    2048 Apr  4 02:33 mail
drwxr-x---   3 remoteuser remoteuser    2048 May 23  2008 public_ftp
drwxr-x---  30 remoteuser nobody   2048 Dec  4 06:33 public_html
drwxr-xr-x   7 remoteuser remoteuser    2048 Sep 24  2008 tmp
lrwxrwxrwx   1 remoteuser remoteuser      11 Apr  1  2006 www -> public_html
matthias:~ matthias$
  

You can even pipe through to a remote command. Here's an example taken from Look Here First, where the content of a local file is concatenated to a remote file.


matthias:~ matthias$ cat localfile.txt | ssh remoteuser@myserver.pretendco.com "cat - >> remotefile.txt"
  

2. Add your public key to your remote server

Next let's see how we can get around typing our password all the time. One way of doing this is to .


# first, upload public key from client to server
client$ scp ~/.ssh/id_rsa.pub remoteuser@myserver.pretendco.com:~

# next, setup the public key on server
server$ mkdir ~/.ssh
server$ chmod 700 ~/.ssh
server$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
server$ chmod 600 ~/.ssh/authorized_keys
server$ rm ~/id_rsa.pub
  

Of course, we can combine with the previous example and run it like this:


matthias:~ matthias$ cat ~/.ssh/id_dsa.pub | ssh remoteuser@myserver.pretendco.com "cat - >> ~/.ssh/authorized_keys2"
  

Windows users fear not, , with PuTTY and PuTTYgen.

3. Let the ControlMaster handle your sessions

Sadly this might not always possible, maybe your key management on your server is different, or you don't have permissions to the file.

Luckily you can get around this by letting your first ssh connection act as ControlMaster. By adding the following to ~/.ssh/config, you only need to enter your password once, and all parallel sessions won't ask for a password


Host *
    ControlMaster auto
    ControlPath ~/.ssh/master-%r@%h:%p
  

4. Set your user and hostname for your host

Finally, more often than not your username won't be the same on your local machine and on whatever server you'd like to connect to, or your server might not use the default ssh port. Or maybe you're just tired of typing that superlong domain name, and look for a less challenging typing task. ~/.ssh/config to the rescue.

As final example:


Host myserver
    User remoteuser
    Port 22022
    HostName myserver.pretendco.com
  

Most likely you won't need the Port instruction in there, because your server runs ssh on the default port 22, so just drop the line.

All this allows us something as compact as:


matthias:~ matthias$ ssh myserver mycommand

Have fun, and let's hope this got you into your weekend early!

阅读(869) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~