Three more tips - use keybindings, scripts and SSH without passwords

29 April 2008

Use Readline shortcuts

At the bash prompt, you can use the default readline keybindings, these are similar to Emacs ones. Many of these are also available within other programs that use readline, such as the Python interpreter.

Here are some useful ones:

Ctrl-A Beginning of Line

Ctrl-E End of Line

Ctrl-U Kill (cut) everything left of cursor

Ctrl-K Kill (cut) everything right of cursor

Ctrl-W Kill (cut) the single word before the cursor

Ctrl-Y Yank (paste) the text back

Ctrl-L Clear Screen

Ctrl-D Exit

Ctrl-R Reverse interactive-search, (attempt to complete what is currently being typed using the history file)

SSH without Passwords

If you login to a remote machine often and you get bored of typing the password, then you can use public key cryptography instead.

The way it works is that the remote machine has a copy of your local machine's public key, it can then use that to check that your local machine is really your machine, and so let you in.

To start with, on the local machine, see if you already have a key pair:

ls ~/.ssh/id_?sa.pub

If not, then make one:

ssh-keygen -t dsa

Now you need to copy your public key to the remote host. On the local machine run:

scp ~/.ssh/id_?sa.pub remotehost:

Now we login to the remote server:

ssh remotehost

Append the public key to your authorized keys file

cat id_?sa.pub >> ~/.ssh/authorized_keys

Now you can login without passwords. Make sure the security of your machines is well thought out. Use disk encyption if possible.

Create a script directory in home directory

I often talk about random Python or bash scripts. The easy way to use them on Linux is to make a dedicated script directory for these.

mkdir ~/bin

Add it to your shell's path. Edit ~/.bashrc and add:

export PATH=$HOME/bin:$PATH

Now all the scripts that you add to ~/bin are always available. This makes things a lot more flexible and fun as you can try out various scripts by dropping them in ~/bin and then deleting them when you are bored of them.

Discuss this post - Leave a comment

1 Graham O' Malley says...

Thanks dude, I have a machine on my local network that I ssh into all the time and it never even occured to me that there was a passwordless login option - thats gonna save a lot of irritation for me :)

Posted at 11:39 p.m. on April 29, 2008


2 dbr says...

Gah, don't use passwordless SSH keys! Use ssh-agent instead - it achieves the same thing, but with one less possible-security problem.. http://upc.lbl.gov/docs/user/sshagent.html

If you don't like the emacs'ish keybindings, in bash you can run "set -o vi" and Esc toggles between regular insert mode, and vi-command mode (where 'dw' delets words, 'j' scrolls up the command-history etc). That said, the emacs'y bindings are probably more practical (modal editing in a command line isn't terribly useful)..

Posted at 9:52 a.m. on April 30, 2008


3 Steen says...

I'll just add:

Meta-b One word back Meta-f One word forward Meta-d kill word forward

...to the list of nifty keyboard shortcuts in the shell.

Posted at 7:15 p.m. on April 30, 2008


4 Ryan says...

Awesome-- never knew about the cut/paste commands within bash. That'll be a lifesaver some day. Some additional bits of awesome:

For those times you don't want to type out an argument that you just typed again, you can use !:# as a stand-in or a previous line's arguments. Once you hit enter, the command is then printed out so you can see what you did, or in zsh you can hit tab and it will autocomplete.

omg:~ keke$ mv filename1 filename2 omg:~ keke$ mv !:2 !:1 mv filename2 filename1 omg:~ keke$

And... For those times that you need to re-enter a whole line, say you forgot to sudo a command, there's !!

omg:~ keke$ port install naim Error: permission denied omg:~ keke$ sudo !! sudo port install naim Password: ---> Fetching naim

It's freaky how many weird shortcuts there are in bash, and googling finds many. For instance, there's a find-replace shortcut for the last command:

omg:~ keke$ echo "this is so horrible" this is so horrible omg:~ keke$ ^horrible^amazing echo "this is so amazing" this is so amazing

Go Bash!

Posted at 4:04 p.m. on May 1, 2008


5 Ryan says...

Ah, addendum-- you can expand the shortcuts in bash to make sure you know what you're typing by entering a space and hitting tab.

Posted at 4:07 p.m. on May 1, 2008


6 Ryan says...

Ooh, and meta, ctrl+e writes out the command too; instead of space, tab. They probably behave differently.

Reading the man page for bash now, and it's just crazy.

Posted at 4:19 p.m. on May 1, 2008


7 Jalada says...

I agree with DBR, never ever use passwordless public keys. Yes they're convinient, but there's a much easier way (sshagent as DBR mentions). In fact, in Ubuntu and Mac OS, there are graphical SSH Agents that automatically pop up when you have a key with a passphrase, asking if you want the OS to remember it (basically, doing what sshagent does, but graphically). For the sake of typing a passphrase ONCE, you have secure keys that - if stolen - still cannot be used.

Useful tips though! :) (I have subscribed to your RSS)

http://jalada.havennetworks.com

Posted at 1:31 p.m. on May 18, 2008


What do you have to say?

Show Editing Help

Europython

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

picsus

January 5, 2009
Monique, a Leaf fan, originate this plumb persistent to believe. Now, let me regarding out that this was in no way an try to articulate one cooperate is more wisely ...
This week in the world of the Command Line; The Friday Round up!

QuickSilver

January 5, 2009
Nice! Is there anyway to implement a ServerAliveInterval for long processes? This is because my our firewall keeps closing the connection based on inactive connections. Thanks,
SFTP in Python: Really Simple SSH

Tun

January 5, 2009
Hi, Do You know, haw can i get start date for tasks evolution? If exists the similar way to your example: i.get_due() ? I would like to have sth like ...
Three Useful Python Bindings - ClamAV, Apt and Evolution

MurreiM

January 5, 2009
This is great! http://www.youtube.com/MurreiM Buy Alli Orlistat online cheap
Filing cabinets 101 - An introduction to disk partitions

sarah

January 5, 2009
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed ...
This Week: Freedom not Time-Bombs

jnfrlast

January 4, 2009
Hi! http://www.youtube.com/jnfrlast buy cheap viagra online
Filing cabinets 101 - An introduction to disk partitions

Samuel Huckins

January 4, 2009
Great tips! I have had occasion to do a lot of MySQL instance migrations lately, so here is an improvement for Trick 1: mysqldump <DATABASE_NAME> [mysqldump_options] | gzip -c | ...
Five useful command one liners

George Glass

December 31, 2008
I don't really see the point in trying to make linux user-friendly or take over the desktop. We rule the servers the most important element of the entire game. Let ...
Give Linux a chance

bug

December 31, 2008
@Zeth: The hidden field does block some. Not perfect, but it does release some weight from the filtering system, as those are 100% false comments. Acctually, if you would have ...
On Comment Spam

Zeth

December 31, 2008
Hi Eion, Yes that is an interesting approach also. It is the only approach given by default in the stock Django comments module, though it does not stop all comment ...
On Comment Spam

Bug

December 30, 2008
Well... Sadly, and I guess you hate me for it, I use captcha. But at least it's not an image, so even if you visit using w3m [yey!] you can ...
On Comment Spam

Eion

December 30, 2008
Other than server-side processing of comments, I like to add additional <input>'s and hide them in external css. Most of the time the fields are populated by spam-bots, and if ...
On Comment Spam

Nostoc

December 27, 2008
... Mate possible because of the dull Kg8
Ruy Lopez, Berlin defence, open variation

Nostoc

December 27, 2008
My bad, I meant the picture beneath 15, after close inspection my suggestion would be on 18. Instead of 18 : Qe2, I would have taken that knight with my ...
Ruy Lopez, Berlin defence, open variation

Zeth

December 27, 2008
Nostoc, white takes the rook on 15, the rook is a better kill than a knight.
Ruy Lopez, Berlin defence, open variation

Nostoc

December 26, 2008
I'm not that good at chess, but I have a question. At 15, why doesn't white simply take black's knight in C6 with the bishop? It's an easy kill, since ...
Ruy Lopez, Berlin defence, open variation

Zeth

December 26, 2008
CorkyAgain, good question, I don't have a FreeBSD box available at the moment so I can't comment. On Linux at least watch does as I have described.
Five useful command one liners

CorkyAgain

December 25, 2008
Is the watch command you're describing a Linuxism? On my FreeBSD box, "man watch" seems to be describing something completely different.
Five useful command one liners

Binny V A

December 25, 2008
I have actually setup a site to store just short commands... http://txt.binnyva.com/
Five useful command one liners

Bassam essa

December 25, 2008
i try this line command elinks -source "http://www.e51g.com/" > resulthtml.txt its work done :) thx
Command the Web - an ELinks tutorial