Archive

Archive for the ‘Linux’ Category

Vim Tip of the Week : August 17th, 2007 – Time based Undo (or Redo)

August 17th, 2007 No comments

Well these Vim tips have really been one of the more popular topics to come through this blog in a while.  I’d like to keep with tradition and keep these going each Friday.  This weeks tip is reverting or redoing changes based on time, vs simply the undo command (‘u’) and redo command (‘ctrl-r’).

From the vim :help section:

:earlier {count}    Go to older text state {count} times.

:earlier {N}s    Go to older text state {N} seconds before.

:earlier {N}m    Go to older text state {N} minutes before.

:earlier {N}h    Go to older text state {N} hours before.

Also supported is the :later command following the same syntax pattern. Easily revert to changes at previous times with the :earlier, or re-do changes forward with :later.  Of course :later won’t read your mind and create your document for you, but once you’ve gone back in time you can go back to the future.

For more info on this tip type “:help earlier” within vim.

Categories: Linux Tags:

Vim Tip of the Week – Word Find? : Aug 10, 2007

August 10th, 2007 2 comments

Vim is the greatest editor of all time. Let me tell you! Every week when I go to put together a new tip there is just so much to offer. I can keep this going for years! If you haven’t started using vim yet start!

Now vim has a really comprehensive search function using the ‘/[word]‘, but maybe you just want to find the next word matching the one your cursor is on, in the fewest keystrokes possible. Well you can do that (of course). Use the ‘*’ key to jump from whatever word your cursor is on to the next match of that word. Give it a try.

Of course using the normal search option ‘/[word]‘ followed by ‘n|N’ will jump to the next match as well. Keep these both in mind when you’re searching for words.

Categories: Linux Tags:

Configure Local and Remote System Logging : Ubuntu

August 4th, 2007 2 comments

A linux machine has a logging system which keeps track of what everything is doing.  Anytime you authorize with sudo it gets logged.  Anytime you (or someone else) connects via ssh it gets logged.  Apache logs connections, mail servers log emails sent and refused.  Pretty much everything keeps a log of what it is doing so you can later troubleshoot it or simply have a record of it.For those that are security minded it may not be a bad idea to keep duplicate of your logs by sending them not only to the local machine but to a remote machine as well.  This way, even if an attacker is able to get into the first machine his steps are logged remotely before he is able to clear them on the local machine.  This, of course, has a number of other valuable uses.  In any event, here are a few quick steps to set it up.

/etc/syslog.conf

This file is the main “what gets logged and where” file for your system.  If you take a look at it you’ll see that it takes different types of logs and writes them to the appropriate files.  Mail here.  Cron there.  etc. We can easily tell the system to send the logs elsewhere with the following:

*.*    @192.168.0.5 (replace with your log-server IP)

This will send *everything* to the remote machine to be filed in the appropriate place.

or

*.level | level.*    @192.168.0.5  (replace, again, with your log-server IP)

This will send just the appropriate log type (just mail, cron, etc) to the remote server. Follow the same syntax seen in the existing file to go this route.

/etc/default/syslogd

This file is the system logging daemon configuration.  This is where we’ll tell the remote machine to listen for and accept logs.  As you see from one of the comments you simply need to add the “-r” to the SYSLOGD=”" field.  Example:

SYSLOGD="-r"

Once you have made these changes both on the local (/etc/syslog.conf) and on the remote (/etc/default/syslogd) you’ll need to restart those services with the following command:

sudo /etc/init.d/sysklogd restart

At this point you should be able to view the logs on the remote machine (perhaps using “tail -f /var/log/[logfile]“) and see logs identified from another hostname.  Ohh, and you can send *.* from A to B and *.* from B to A and it wont cause an endless loop.  This way both machines have backups.

Categories: Linux Tags:

Turning Off The System (hardware) Beep : Linux Tutorial

July 26th, 2007 35 comments

I was reminded this week of something that I often show people but I have never actually written down in a post. This may be old news to some of you, but when has that stopped me in the past!?

Now I should preface this with a warning that my boss pointed out to me. He says “the system beeps for a reason–it’s trying to tell you something–you should leave it on.” Now that is out of the way I’ll go ahead with how to disable the PC Speaker, which removes the often annoying beeps. It should be mentioned that this should work on any distribution, and is not Ubuntu specific.

Removing the driver

The system speaker is controlled by a driver in the Linux kernel. This allows the pc speaker to beep at you for different reasons or at different events. If you remove the module which drives the speaker, the beeping goes away, as the machine no longer knows how to interface with that device.
This can be done manually with a command such as:

sudo modprobe -r pcspkr

or you can set it as a persistent change by adding the module to your system driver blacklist, available at:

/etc/modprobe.d/blacklist

simply append the line “blacklist pcspkr” for that driver to be disregarded at every boot.

If you’d like to manually re-insert the module use:

sudo modprobe pcspkr

Enjoy the quiet!

Categories: Linux Tags:

Vim Tip of the Week : SHIFT-K

July 20th, 2007 5 comments

Well you asked for it. Last week I did the first vim tip of the week and it looks like you all want more. I got another goodie today that you might enjoy. How many of you know what SHIFT-K does in command mode? No, don’t go look it up. Do you know right now? If not keep reading. If so, I’m impressed.

Instead of just telling you what it does I want you to give it a try for yourself (no better way to learn than to do, right?). Follow the below instructions and see if you can figure out what it does. First person to figure it out and comment with the answer wins!

Ready, Set, Go!

Open up a new file with vim:

vim file1

switch to insert mode (i,I,a,A,o,O, etc).

type a few linux commands into a new line (any command you want):

vim awk man aptitude

switch back to command mode (esc)

put your cursor on any of the commands and press SHIFT-K.

Pretty slick, particularly when trying to decipher shell scripts if you’re unfamiliar with the commands, or reading documentation that refers to related apps!

…so, what does it do? Enjoy.. and be back next week for another tip! Thanks Dax for pointing this out to me.

Categories: Linux Tags:

forget top, check out htop

July 15th, 2007 11 comments

I just realized I’ve never blogged about a really cool tool that I’m sure you’ll be impressed with.  If you’ve used top, or use top to ever try to get an idea of what is running on your machine you’ll really like htop.

From the package description:

Htop is an ncurses-based process viewer similar to top, but it allows to scroll the list vertically and horizontally to see all processes and their full command lines.  Tasks related to proccesses (killing, renicing) can be done without entering their PIDs.

Yeah, that’s right.  You can actually see the whole process including options and arguments.  You can scroll down off the current page to see what else is running.  It also looks a lot prettier, and I know how much you like your pretty applications.

Installation

Installing htop is as easy as you might guess:

sudo aptitude install htop

That’s it.  Then run htop and get a better view of what is running on your machine.  I would add a section here on how to use it, but its so simple I don’t think I need to.  All the shortcut keys are listed at the bottom.

Tomorrow I’ll show you how to bypass the F10 limitation when used with gnome-terminal.  ..you’ll see what I mean when you try to use it. :)

Categories: Linux Tags: ,

SSH Saves The Day.. 1400 Miles Away! Enable Automagic Login

July 14th, 2007 32 comments

I related the below story tonite in our Ubuntu Utah meeting and John asked me to blog the details and the howto.  This is for you buddy :)

A few months ago I was in Chicago to take the Redhat Certified Engineer exam. By day I attended class and by night I studied the book to prepare for the grueling test.  Well, one of the nights I was there (5 day course) my brother calls me in a panic begging me to come fix his computer.  I of course tell him that I’m in Chicago at the moment and not sure what I can do to help. At that I can hear his heart sink, so I ask him what the problem is.

He starts into this sob story about how he has a paper due in the morning but he can’t get logged into his computer.  Apparently his wife dropped something on the keyboard and the number row no longer works, which limits him from logging into his machine (numbers in passwords are good ideas boys and girls!).  He is, by my suggestion, using Ubuntu on his home machine so I know I can save the day.

As he continues this story I use ssh to connect to his machine…

ssh user@remote-host

..and open the /etc/gdm/gdm.conf file.

vim /etc/gdm/gdm.conf

I change two lines from:

AutomaticLoginEnable=false

AutomaticLogin=

to

AutomaticLoginEnable=true

AutomaticLogin=username

I save the file and restart the gdm (Gnome Desktop Manager)

sudo /etc/init.d/gdm restart

By this point he’s finishing his sob story about how he needs to get his paper written or he’s going to fail his class, never graduate and end up being homeless, etc, etc.

I interrupt him to tell him to look at his computer, which is now logged in as his user, never requiring a password or username for access.

The phone goes silent.  I ask if he’s still there. He is.. he’s speechless.

I remind him this is all due to the wonders of Linux and wish him good luck with his paper.. and to tell his friends about the wonders of Ubuntu!

Simple as that.  SSH connection, two commands and a very thankful brother all from 1,400 miles away… and a tutorial on how to activate automagic login on Ubuntu (which is probably not the most secure idea, but you’ve been warned.  Once he got a keyboard replacement I reverted the changes on his machine.)

Categories: Linux Tags:

Group Collaboration With Screen

May 17th, 2007 3 comments

This week I’m teaching out in Portland, OR in a Linux Fundamentals class. A small part of one of this weeks chapter is on screen. You might remember I’ve talked about screen in the past here. I still use screen on a day to day basis and really enjoy it. Today I’ve got something new for it that you may not have ever used.

Screen will allow you to collaborate within a session for multiple assigned users. It is also configurable to allow for read-only or read-write access on a per user basis. This, of course, if helpful if you don’t trust certain people within a collaboration as everyone will have input permissions on the screen.

Now the first step is that you’ll need to, at least temporarily, put the suid bit on the command screen. This is done using:

sudo chmod u+s /usr/bin/screen (multiuser screen below will not work without this)

Second you’ll want to create your initial screen session. You can do that (remember my original screen cheat-sheet) using a command similar to:

screen -S screen-name (naming your screen sessions with -S makes organizing and switching simpler)

Third you now allow the collaborator to connect to your machine using something like ssh.

ssh user@remote.machine

The fourth step is to set multiuser mode on within screen itself. This is done using the screen shortcuts followed by :multiuser on. Here is an example:

ctrl-a :multiuser on

You also need to explicitly allow permissions for the collaborating user (the one that ssh’d to the machine) using something like:

ctrl-a :acladd username

At this point username can connect to the shared screen using:

screen -x username/screen-name

If you want to just try it out you can test sharing screens by just using the same user account. Start screen locally using step two above. After that is up start some command or type something at the prompt. At this point open a second terminal window and type screen -x. You should have a mirror window in both terminals.

If you have any other implementations of this I’d love to hear about them. I really like this feature and can see a number of uses for it. Perhaps collaborating on code and not needing to use pastebin or something similar. Actually watching the other user work to show you examples.

It’s like remote desktop or VNC, minus the bandwidth and overhead!

Command line collaboration brought to you by GNU. What more could you want?

More permissions on advanced permissions here.

Categories: Linux Tags:

How To pwn Your Text Files With Vim

Recently I did a lecture on the magical world of vim. Despite having used it for quite a long time I realize there is still far more that I could know about it, but there is also more that I haven’t published on this blog.  Some of you might remember some of my earlier posts on vim [here], [here].  Today I have a few more shortcut keys to tell you about that I sure hope you’ll appreciate as much as I have grown to.  First, some more advanced navigation.

Moving your cursor within vim can be done on per character, per sentence and per parapraph.  Also, remember that you can repeat commands (even movement commands) by prepending it with the number of times you would like it done.

To move your cursor within vim per character you can use the arrow keys or the h,j,k,l for left, down, up, right respectively.

To move your cursor within vim per sentence you can use the (, ) keys to move to the previous or next sentence respectively.

To move your cursor within vim per paragraph you can use the {, } keys to move to the previous or next paragraph respectively.

To move your cursor to a specific line within your text you can prefix the line number with : .  ie; to jump to line 10 you would use “:10″.

You can also jump to the beginning or end of the file with G or gg.

Next, now that you can jump from one end of your file to the other quickly you might want to open multiple windows within vim for, perhaps, comparison, etc.

You have the ability to split a window either vertically or horizontally within vim very simply.  To control, move or split windows you must prefix the command with ctrl-w.

ctrl-w n – create a new window representing a new, empty file

ctrl-w s – split the window horizontally

ctrl-w v – split the window vertically

ctrl-w arrow – move to the window in the direction of the arrow

ctrl-w q – quit the current window (also available is the default :q, :wq)

ctrl-w – / + – increase or decrease the size of the current window.  (also available is increasing or decreasing by X value: ctrl-w 5+ – increase window size by 5.)

Adding these commands to the commands that you already know and now you’re getting efficient with vim.  The only editor you’ll ever need!  …but, like I said, this still isn’t everything.  Not by a long-shot, but it’s a really good start!

Categories: Linux Tags:

Creating Shortcuts With User Aliases

March 30th, 2007 12 comments

Do you have a set of commands that you find you repeat over and over in your daily use? Things like connecting to servers over ssh, rsyncing data launching apps with a slew of argumets. You know what I mean. Well you can make things easier on yourself by creating alias commands for your account. Below are a few examples:

ssh user@server -p 22222 could be setup in an alias defined as ‘connectwork’

rsync -avz –progress daily/backup/file/ user@server:/remote/directory could be defined as ‘dailybackup’

By setting these longer commands as local aliases all you have to do is type the alias name, which is then connected to the command and you’ve saved yourself a load of typing. Yes, this appeals to us because we work hard but don’t want to work any harder than needed!

To create an alias you’ll want to edit your local .bashrc file. This file defines a number of shell options for your account but also can store any aliases you want to define. The default file has a few example aliases set, and all you have to do to create your own are append them to the list.  A sample entry would look like this:

alias aoeu=’setxkbmap us’ (this will switch keyboard layouts to qwerty)

alias asdf=’setxkxmap dvorak’ (this will switch keyboard layouts to dvorak)

alias updateme=’sudo aptitude update && sudo aptitude upgrade && sudo aptitude dist-upgrade && sudo aptitude autoclean’ (check for, download, install and cleanup after updates)

As you can tell from these few examples you’re able to create an alias for your user on the shell for whatever you like.  It can save you typing, help automate repetitive commands, and make your life easier.  Give it a try.

Categories: Linux Tags: