Thursday, February 03, 2011

Edit Remote EC2 Text Files with TextWrangler

This post explains how to use TextWrangler on a Mac to edit text files that live on a remote Amazon EC2 instance with Ubuntu.

I assume you already know how to SSH to Amazon using an Identity File:

ssh -i ~ec2-keys/id_your-keypair ubuntu@ec2-public-dns-name.amazonaws.com

You can use a similar command to connect to your EC2 instance via SFTP:

sftp -o IdentityFile=eec2-keys/id_your-keypair ubuntu@ec2-public-dns-name.amazonaws.com

TextWrangler allows you to open a file from an SFTP server, but it doesn't allow you to configure an Identity File as you did when SSHing or SFTPing from the command line. To work around this limitation, you simply have to configure your SSH client so that it knows the Identity File you want to use. Create ~/.ssh/config if it doesn't already exist and add a line specifying your Identity File location:

echo 'IdentityFile ~ec2-keys/id_your-keypair' >> ~/.ssh/config

Now you're ready to use TextWrangler to connect to your server and browse files. Choose: File -> Open from FTP/SFTP Server... Then enter your server publisher DNS name, check the SFTP box and enter  ubuntu as the User name.  You can leave the Password: field blank.















After you hit connect,  you TextWrangler will allow you to browse the file system of your EC2 instance, choose a file, and edit it.  When you save the file, it will update directly on the remote EC2 instance.  That's all there is to it!

11 comments:

Eric Hammond said...

Ken: Life gets a lot easier if you upload your personal ssh key to EC2 and use that when starting instances.

http://alestic.com/2010/10/ec2-ssh-keys

Then you don't need to keep specifying the identity in ssh, rsync, and other commands.

-- Eric Hammond

Ken said...

Ah, yes, Eric. I've been meaning to try that. Thanks for the great tip.

Anonymous said...

Nice information and thank you for sharing.

quantum

khany said...

hi Ken,

doesn't work for me. what is the connection between TextWrangler and the config file I created? Have I missed something?

K

Ken said...

khany, the config file's purpose is to allow you to use SFTP without needing to specify the identity file every time. Text Wrangler doesn't even give you the option of specifying an identity file, which is why this is important. That said, your best option is to upload your personal ssh key to EC2 as Eric Hammond pointed out in an earlier comment.

RT said...

I got this to work (using my parameters) on my local machine

sftp -o IdentityFile=~/.ec2/ec2-keypairs ubuntu@ec2-xxx-xx-xx-xxx.compute-1.amazonaws.com

but when creating a config file with this in it

echo 'IdentityFile ~/.ec2/ec2-keypairs' >> ~/.ssh/config

textwrangler still responds the same way, by

This server operation couldn’t be completed because the connection was unexpectedly lost (application error code: 22807)

Not sure what i am doing wrong.
If you could explain step by step what ken is tallking about, that would be helpful.

Thank you for this article

Pipalayan said...

Hi Ken,
Thanks a ton for this advice!!

Anonymous said...

Dear Ken:
I very appreciate your post to make the connection to EC2 throught TextWrangler.
I tried them along what you mentioned.
But I got same error (seen above, error code 22807)
Except connecting with Text Wrangler, I succeed in using ssh in terminal.
Any idea to fix this error??
thanks.

and I welcome to give an email to jongwon.chae@gmail.com

cristi said...

It's a permission error.you are editing files as a ec2-user, and the files are in "webapp" group ....
That's why

You cana add ec2-user in webapp group then... and edit on it.

C

Anonymous said...

chmod 400 yourkey

Steve Johnson said...

FYI: I believe all of this stuff with the ssh config file is unnecessary if your standard ssh public key, the one in ~/.ssh/id_rsa.pub, is installed into the account on EC2 you're trying to connect to (in the .ssh/authorized_keys file). That's what I do...I add my standard public key to the EC2 account rather than having to do fancy configuration on the client side. We do this, in fact, as a matter of course to control who in our company has access to particular machines.

This has worked for me until about the time I upgraded to Yosemite. Now, I can connect with BBEdit via SFTP to some machines but not others, and I can't figure out why. I'm getting the same 22807 error. I've checked the configuration of sshd on each machine, and they're the same for machines that work and ones that don't.

I can, in all cases, do a "ssh @" using the same values I put in the BBEdit connection dialog. To my understanding, if this works, so should the BBEdit connection.