Posts Tagged ‘ Linux

Taking More Control: Even More BASH Tips

This is an extension of my last BASH post, Taking Control: More BASH Tips. In that post we discussed some of the control commands that are available for clearing the screen, searching the history and moving the cursor around. This time we are going to talk about modifying the text in the current command.

CTRL+_ or CTRL+-
Let’s start out with the thing that we all need sometimes, undo. Control and underscore or dash will undo the last control command done in bash. It WILL NOT undo the last command executed on the system. For example if you cut all of the text from the cursor to the beginning of the line but really ment to cut the last word you can use undo.

#I want to change badger to fox.
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.
#I hit CTRL+u instead of CTRL+w
j2consulting:~ user1$ jumps over the lazy dog.
#CTRL+- saves me from my mistake
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.

CTRL + u, CTRL + w, CTRL + k
As I showed in the previous example, you can remove text from the current command. Using these control commands you can delete text based on the current cursor position. Control and u will delete all of the text from the point of the cursor to the beginning of the command. Control and k will delete all of the test from the point of the cursor to the end of the command. Finally, control and w will delete the previous word in the command.

#I want to change badger to fox.
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.
#I move the cursor to the space between badger and jumps and then hit CTRL+w
j2consulting:~ user1$ The quick brown jumps over the lazy dog.
#Now I can add fox.
j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.
#I don't like the end so I move the cursor to the space between fox and jumps and hit CTRL+k
j2consulting:~ user1$ The quick brown fox
#And now I change the ending.
j2consulting:~ user1$ The quick brown fox hides while the dog chases him.
#But now it's just wrong and I want to delete everything from the end to the beginning so I hit CTRL+u
j2consulting:~ user1$

CTRL + y
So you have just got done cutting all kinds of text, what is the next logical thing? Paste. Any of the text you cut using the previous 3 commands can be pasted into the line. Let’s switch the sentence around to put the dog first.

j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.
#Putting my cursor on the . and hitting CTRL+w I cut the previous word.
j2consulting:~ user1$ The quick brown fox jumps over the lazy .
#Now I put my cursor between fox and jumps and hit CTRL+y
j2consulting:~ user1$ The quick brown foxdog jumps over the lazy .
#Now I put my cursor on the d in dog and hit CTRL+w
j2consulting:~ user1$ The quick brown dog jumps over the lazy .
#Finally I put my cursor on the . again and hit CTRL+y to paste the fox
j2consulting:~ user1$ The quick brown dog jumps over the lazy fox.

CTRL + t
It is a fairly common mistake to transpose two characters while typing. Control and t can help us quickly fix this by swapping the previous two characters.

j2consulting:~ user1$ Teh
#Oops, I ment to spell The, hit CTRL+t
j2consulting:~ user1$ The
#This can also work if you want to move the cursor to the mistake.
j2consulting:~ user1$ Teh quick brown fox jumps over the lazy dog.
#By placing the cursor between the h and q and hitting CTRL+t we can fix the mistake.
j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.

By using the tricks in this and previous BASH posts you can significantly reduce the time it takes you to get things done. If you know of any other tricks I’d love to hear about them in the comments.

Taking Control: More BASH Tips

Between the numerous servers I manage via ssh and my personal machines I spend a lot of time in the BASH shell and, as with anyone and who uses a tool for a long time, I have discovered numerous shortcuts to getting things done efficiently. I wrote a couple months ago about using ! and $ to manipulate previous commands. This time I am going to talk about moving the cursor around in the current command and manipulating the window.

CTRL + l
Before we get started let’s clean up by clearing our screen. Control and l will clear the screen without modifying any of the text on your current line. It is very helpful for keeping your terminal clean or preparing it for a large amount of output that your next command may bring.

CTRL + r
This is probably one of the most frequently used control combinations. Control and r allows you to pull up a command from your history by typing in and having it match the last used command with those characters. For example, if you used a command yesterday that had very complex syntax and can’t remember what it was, you could hit ctrl+r and then start typing the part of the command that you know.

j2consulting:~ user1$ echo The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
(reverse-i-search)`fox': echo The quick brown fox jumps over the lazy dog.

CTRL + a and CTRL + e
There are many times when you need to jump to the beginning of the line to fix a command and then jump back to the end to finish what you were typing. Control and a as well as Control and e take care of this for you. Say I wanted to use print instead of echo in the previous example. I could hit the up arrow, CTRL+a, change echo to printf “, CTRL+e, add \n” and be done.

j2consulting:~ user1$ echo The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
j2consulting:~ user1$ printf "The quick brown fox jumps over the lazy dog.\n"
The quick brown fox jumps over the lazy dog.

ESC, ALT, CTRL + b and ESC, ALT, CTRL + f
In addition to these there are commands to move back and forward one word at a time. I don’t frequently use these because they have mixed success depending on which terminal emulator is used. If you are on a mac on the local terminal esc and then b will move you back one word and esc then f will move you forward one word. If you are on a mac and ssh’d into a system CTRL + the left arrow will move you back one word and CTRL + the right arrow will move you forward one word. I have also read about ALT + b and ALT + f working on different systems. Experiment with this one but because of the inconsistency I tend not to depend on it.

There are more control commands available and I will be looking at those in my next post. Until then happy BASHing.

Using Screen to share your terminal.

As a consultant who works with remote clients and, before that, a telecommuting systems engineer for companies who used linux I have often encountered the problem of collaborating inside of a remote client that does not have X and who’s only access is ssh. Servers with GUI interfaces like Windows or Mac OS X have a long history of tools to share the desktop with multiple users, oddly enough both are called Remote Desktop. Servers that only have a command line interface, like most linux servers, do not have such an obvious tool for sharing the terminal with another user. While bash and ssh can be manipulated to accomplish this, screen fills this roll nicely.

Screen allows you to create virtual terminals that you can detach from and attach to at will. Detaching from a screen will leave it running in the background. Attaching to it again allows you to keep executing commands where you were or see the current state of a process you left running. If you are the user who started the screen you can attach to a screen that is already attached to, this is is how the shared screen is accomplished.

To set it up, one user logs in to the server and, using su or sudo su, changes to either the second users account or an account that all parties can access. Next, start screen with the -S option and give it a meaningful name.

me@jjtest:~# sudo su – theOtherGuy
theOtherGuy@jjtest:~# screen -S blogTest

If screen is not installed, it is available as a package on most popular linux distributions. You are now in a screen session as theOtherGuy. Now just tell theOtherGuy to log in and that your screen is called blogTest and he will simply need to run screen with the -x option and give the screen name.

theOtherGuy@jjtest:~# screen -x blogTest

At this point you are both in the same screen session and can both interact with it. This is great for showing another user what kind of problems you may be having or how to do a certain process. It can also be used to accomplish remote pair programing on the remote system. And this doesn’t have to be limited to just two users, as long as someone can get to that system as that user they can join the screen as well.

Typing exit at this point will kill the screen and kick both of you out. If one user wants to leave but not kick the other person off the user just presses Ctrl+a and then d, this will detach you from the screen. Ctrl+a is the default hot key for activating screen commands. You can read more about the variety of command available on screen at this site or read abut it fully on the man page.

Screen can be used to do so much more than I’ve talked about here but it’s ability to facilitate the sharing of a terminal between users can be a life saver when you need it. It is not normally installed by default to may linux systems so you may have to install it or ask that it be installed.

Getting the bang (!) for your buck ($) from BASH

Most of the work I do is on systems who’s primary interface is BASH. BASH is the shell that runs on most Linux distributions and Mac OS X when you open a terminal or SSH in. There are a few short cuts I have picked up along the way that are built into BASH but aren’t all that obvious or in the man ( manual ) pages. One very powerful shortcut, ! ( refered to as bang ), is used in BASH to reference the prior command(s) in varius ways and can really speed up your work in the terminal.

!$
The command that this post is named for is used to reference the last argument in the previous command. For example, let’s say you set up a new directory for your client you would need to set the ownership and permissions on it.

mkdir /dir/made/for/client1
chown client1:client1 /dir/made/for/client1
chmod 750 /dir/made/for/client1

This can be shortened by using the !$ shortcut like this.

mkdir /dir/made/for/client1
chown client1:client1 !$
chmod 750 !$

The time savings here are minor but over time can help, by referencing the first command we also reduce the number of typos.

!!
This command references the complete previous command. “Doesn’t the up arrow do that?” you ask? The up arrow pulls the previous command up, allowing you to run it again or modify it. !! allows you to insert the previous command into what you are typing. Take the previous example, if you didn’t have permission to make the directory the first time you tried you could try it again using sudo, like this.

mkdir /dir/made/for/client1 (returns an error about permission denied)
sudo mkdir /dir/made/for/client1 (works)

This can be shortened using the !! shortcut like this.

mkdir /dir/made/for/client1 (returns an error about permission denied)
sudo !! (works)

!number or !-number
BASH, in most configurations, keeps a history of your commands. You can see a list of those commands using the command history.

server01:~ j2$ history
26 command1
27 command2
28 command3
29 command4
30 command5
31 command6
32 command7
33 command8
34 command9
35 history

You can reference any of these commands using the number next to them and the ! shortcut. If I wanted to run command5 again I would do this.

!30

You can also reference commands in reverse by negating them. If I remembered that I ran command5 six commands ago I could do this.

!-6

!:number
By putting a : and a number after the ! you can reference individual arguments in a previous command. Each “word” in a command gets assigned a number, starting with 0. For example, in the command chmod 750 /dir/made/for/client1 chmod = 0, 750 = 1, and /dir/made/for/client1 = 2. So if I had to change the permissions on a few directories it would look like this.

chmod 750 /dir/made/for/client1
chmod 750 /dir/made/for/client2
chmod 750 /dir/made/for/client3

Using the shortcut it would look like this.

chmod 750 /dir/made/for/client1
!:0 !:1 /dir/made/for/client2
!:0 !:1 /dir/made/for/client3

Finally, the last shortcut and this one can be combined to reference specific parts of a command in your history list. Say I just did all those chmod commands above but forgot to chown client1′s dir. Using the combination I could do this.

chown client1:client1 !-3:2

You can now see why ! is one of my favorite tools while working in BASH. If you have any questions or know of another way to use !, please leave a comment.

Likewise, the myth debunked

This post originally written for my personal blog on June 14th 2009.

As a systems administrator in many mixed Windows and linux environments I have seen and made many of my own attempts at integrating the linux servers into the Windows Active Directory structure with mixed results. Linux registration and authentication inside of a Windows domain is akin to the bigfoot: some have claimed to see one, many have worked long hours to find them, no one can produce consistent results. Until now.

At the suggestion by a coworker I decided to give Likewise a try on my most recent attempt at bringing our linux servers into the windows domain. Skeptically I built a new CentOS 5.3 server and proceeded to follow the instructions for installing Likewise. The seemed too simple and with each step I waited for the fatal issue that would bring the test to it’s demise. Software installed, no issues. As I reviewed the instructions for adding the system to the domain and found that it only required one command and no further configuration of files I thought, “This is so going to fall on it’s face.” Command executed and I’m waiting, then it comes up on the screen: Success. Success? Really? I don’t believe it, I log onto the domain controller and there it is, right where new computers are supposed to go in the domain. I quickly flip over to the manual and look up how to authenticate for ssh, simple DOMAIN\\username@host, and give it the final test. Success. In the span of 15 minutes I was able to install the app, add my computer to the domain and authenticate against the domain. 15 more minutes and I was able to limit who could log in and give them sudo access. This is a huge win for any admin who deals with linux servers in a domain.

I’m still testing the limits of Likewise and I will say that it hasn’t been without it’s speed bumps but I plan on paying for a little support and getting the answers I need. Like may open source products, Likewise is making it’s money on support and by selling upgraded functionality. I applaud this model, allowing the flexibility of open source while still finding a way to pay for all that hard work. I will be continuing to test the limits of Likewise but as of now I am thoroughly impressed and will continue to use it.

Reblog this post [with Zemanta]