Old Biboroku

Saturday, October 23, 2010

Linux Admin Cheat Sheet

Filed under: Linux — nomo17k @ 20:47
Tags: , , ,

This isn’t intended to be comprehensive at all.  I’ll just be listing commands that I sometimes but not often use to do miscellaneous administrative tasks.

Passwordless SSH

Between trusted hosts, I want to allow passwodless access via ssh. To do this, generate RSA key pair:

$ ssh-keygen

When prompted, accept the defaults unless things need to be changed.  Copy the public key just generated in ~/.ssh/id_rsa.pub on the local host.  Login to a remote host, and append the key to ~/.ssh/authorized_keys.  Next time I login to that host, I will not be prompted for password.

Linux Equivalent of open on Mac OS X

One command I like on Mac OS X is open, which open a file with an associated default application.  There is a command called xdg-open which does the same thing.  Make an alias to it so it acts like the one on Mac:

alias open='xdg-open'

Put this in ~/.bash_aliases.

Find the UUID of Devices

$ sudo blkid

Securely Erase Hard Drive

# fdisk -l                     # check hard drives file systems
# shred -vfz -n 20 /dev/sdb    # secure erase /dev/sdb

Redirect Output to Multiple Processes

Use “tee >(…)”:

$ ls -A | tee >(grep ^[.] > hidden-files) >(grep -v ^[.] > normal-files)

This example comes from this article.

Repeating Same Operation on Many Files

$ for i in *.txt ; do echo $i ; done

Recursively Do Something on Files/Directories

Find all files under a root path:

$ find /path/to/root -print

Change all directory permissions to 755:

$ find /path/to/root -type d -print -exec chmod 755 {} \;

Change all file permissions to 644:

$ find /path/to/root -type f -print -exec chmod 644 {} \;

Change all files and directories with 777 permissions to 755 permissions:

$ find /path/to/root -perm 777 -print -exec chmod 755 {} \;

Show the n-th (e.g., 9th) Line of a Text File

$ sed -n '9p' somefile

Send an Email Message

Provided the mail service is set up properly, a simple message can be sent quickly on command line.

$ echo "message body" | mail -s "subject line" your@mail.com

This is useful when I want to get an email notification when a very time-consuming job finishes.  For a time consuming job, sometimes it’s desirable to email the output from the job.  I can redirect both standard output and standard error to the email message body by:

$ someprogram 2>&1 | mail -s "job is done" your@mail.com

Keep a Process Running without Hangups

When starting a process on a remote host (e.g., over ssh), that process is killed when you logout of the session. To keep it running there, use nohup:

$ nohup somecommand &

Kill All Processes with a Given Name

$ kill -9 `pgrep someprogramname`

This will kill all the processes with the name someprogramname; see the man page for pgrep for more info.

Change Run-Level Configuration for Init Scripts

$ sudo sysv-rc-conf

This will provide an easy-to-user user interface to configure init scripts.  Useful when I want to stop KDM from running at boot, for example.

Find Out How Long the System Has Been Running

$ uptime

Search for Filename in Debian Packages

The Debian packaging system is obviously useful, but I often struggle to find to which package a particular command belongs.  I can search for by filename:

$ sudo apt-file search somefilename

To do this, the command apt-file needs to be installed if not already:

$ sudo aptitude install apt-file
$ sudo apt-file update

Clear Apt Package Cache

The /var partition may quickly fill up with downloaded .deb packages and if the partition size is small, occasional cleanup will be necessary.  If I wish to just remove .deb files that are no longer relevant, do

$ sudo aptitude autoclean

If I need to remove all .deb files, then

$ sudo aptitude clean

The latter is okay as long as I don’t mind downloading many of them again when I need to update the system via the update-upgrade sequence, for example.

View the CPU Information

$ cat /proc/cpuinfo

Bash Scripting

Get the Absolute Path to the Script’s Current Directory

DIR="`dirname $BASH_SOURCE`"
ABSDIR="`cd $DIR; pwd`"

Get the Host Name

HOST="`hostname -f`"

More to come.

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.