HowTo Archives

OpenSSH Tips and Tricks

Joe ‘Zonker’ Brockmeier has a nice write up on some pretty helpful OpenSSH tricks. Two of the tricks he lists is X forwarding with SSH, and using the SSH fuse filesystem. Both of which I use on a daily basis.

Check it out here.

Quickzi: Replace all spaces with a dot in vim

Here’s a quick tip on how to replace all spaces with a dot in vim:

:%s/[ t]/./

And here’s how to remove all trailing spaces at the end of each line:

:%s/s+$//

Have any more vim quick tips? Let’s hear them in the comments.

Quickzi: Find files older than 5 days

Here’s a quick command line tip to find files older than 5 days, and execute an action on those files:

find /home/foo/* -mtime +5 -exec echo {} > oldfiles.txt ;
&& mail -s "files older than 5 days" foo@foogazi.com < oldfiles.txt

This command searches for files that are older than 5 days, and sends the output to oldfiles.txt, then an email is sent to foo@foogazi.com with a list of the old files.

Alternatively, you could search for files older than 5 days, then delete them using this command:

find /home/foo/* -mtime +5 -exec rm {} ;

How to record your Linux shell session

A lot of people are probably not aware of the command script. Script is a quick and easy way to record everything you do in a terminal session.  I use script to record sessions of me fixing a server, or troubleshooting Linux issues, and save it for future needs, or to pass on to others as training material.  Here is what the Linux man page says:

Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

Using script to record your terminal session

It’s really quite simple to record your bash session.  All you need to do is type script -a filename to start recording your session:

laptop:~ foogazi# script -a session1_jun162010
Script started, output file is session1_jun162010
laptop:~ foogazi#

Now that the recording has started, everything you type, as well as everything that returns as output, will be saved into the filename you chose to output to.

Linux rename user command

I’ve gotten this question a whole bunch of times in the past.  You’ve got a Linux username that you want to rename, rather than recreate, so you don’t lose any significate data or permission settings.  How do you rename a Linux user?

Linux rename user command

To rename a Linux user (rename user name), you want to use the Linux command usermod.  From the man page:

The usermod command modifies the system account files to reflect the changes that are specified on the command line.

In short, here is the command to rename the Linux user:

usermod -d "/home/current_user-name" -m -l current_user_name new_user-name

And now an explanation of what each option means:

  • -d specifies the current users home directory.
  • -m specifies that you want to move the files from the current users home directory, to the new directory. You want to make sure you specify this, otherwise the users data will not transfer over to the new user account.
  • -l specifies the current user name, and the new user name you wish to change it to.

That’s it. Now you’ve renamed a Linux user.

How to configure static IP on Ubuntu

Do you use Ubuntu and wish to set a static IP for your machine?  It’s simple. Follow the steps below to find out how.

  • Right click the network manager icon at the top right of your desktop
  • Select Edit Connections
  • Select Wired
  • Click the EDIT button
  • Click the IPv4 settings tab
  • Select Manual from the method drop down list
  • Click the ADD button to add your static IP address
  • Add your DNS addresses in the DNS servers field. You can separate each DNS entry with a comma
  • Click OK.
  • Restart networking using this command: /etc/init.d/networking restart

How to reset your Linux Password

If you’ve ever forgotten your Linux password, chances are you’ve needed to either restore your Linux installation.  Hopefully this solution will save you from having to reinstall Linux in the event that you forget your password.

Reset your Linux password using Grub:

When your machine boots into Grub, you’ll want to press “e” to edit the grub boot linux.

After pressing “e”, navigate to the kernel line and add the word “single” to the end of the long string.

Press “b” to boot into single user mode.

You should then be taken directly to a root user shell, where you can then type passwd, and change your root password.

After you’ve reset your root password, type reboot at the shell to leave single user mode.

Here is a quick tip on how to display the top largest files and directories within my home directory:

# du -hs /home/adam/* | sort -nr | head

Cheers!

Here is a quick tip on how to run crontab every 5 minutes.

*/5 * * * * /home/adam/script.sh will execute script.sh every 5 minutes.

Also, here’s a quick guide to understaning the layout of cron:
# MIN HOUR DAYOFMONTH MONTH DAYOFWEEK COMMAND
5 * * * * echo 'Hello'

Also, the Crontab Man page

For further reading on Crontab check out Understand Cron Jobs in 5 Minutes

Here’s a quick tip for finding files over 100M on Linux.

# find /home/adam/ -size +100M -exec ls -lh {} \; | awk '{print $5 , $8}'

Note that if you change 100M in the command you can find files with any size.   Also, replace /home/adam with any directory you wish.