Linux

Get IT Done: Handy aliases can simplify Linux administration

See how to use the alias command to set up custom commands that can execute administrative tasks.

When my network goes down for some strange reason, my first impulse is to rush to the Linux server and run the /etc/rc.d/init.d/network restart command. I've typed this command thousands of times, and each and every time I type it, I curse myself for not creating an alias.

In Linux-land, an alias is just what you'd think it would be: a command or argument that represents another. In this Daily Feature, I'll show you how to set aliases. I'll also list some handy-dandy aliases you can copy and paste to make Linux administration a bit easier.

Setting aliases with the alias command
As you'd imagine, there's a command that will set aliases for you. The alias command allows you to set the alias by using the syntax alias ALIAS_NAME='ALIAS_COMMAND ' (where ALIAS_NAME is the alias to be used and ALIAS_COMMAND is the command used for the alias). Let's use this command to set the alias netr for the command to restart the network on the Linux server. Before you run the command, you must change to the root user with the su command. To set the alias, enter the command alias netr='/etc/rc.d/init.d/network restart'. With this alias set, you need only run the command (as root) netr to restart the network.

Setting aliases manually
As the root user, open up the ~/.bashrc file. You'll notice the aliases already set in this file:
  • alias rm='rm -i'
  • alias cp='cp -i'
  • alias mv='mv -i'

These aliases are there for obvious reasons. As the root user, you always want to verify every change you make before it's executed. In the example above, you can see that all removals (rm) are interactive, meaning the prompt will ask you if you're sure you want to delete the file. So are copies (cp) and moves (mv).

To add the netr alias to this file, simply add the alias netr='/etc/rc.d/init.d/network restart' command below the last alias listed. The revised alias section will look like this:
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias netr='/etc/rc.d/init.d/network restart'

Use your imagination
The first thing I do when I create aliases on a new Linux box is to look into the /etc/rc.d/init.d directly and see what's being used. The/etc/rc.d/init.d directory holds many key system start/stop/restart binaries. Of these binaries, the ones that are most commonly called are httpd, ipchains, iptables, network, nfs, lpd, and  smb. Depending on what you're using each Linux machine for, you'll want to create start, stop, and restart (as well as status) aliases for each of these binaries.

Here's a list of some standard user aliases for you to cut and paste. Keep in mind that each alias can be made specific to your needs.
  • alias ll='ls -l' - gives a long listing of files.
  • alias la='ls -a' - shows hidden files in a listing.
  • alias lal='ls -la' - gives a long listing and includes hidden files.
  • alias lless='ls | less' - pipes the ls command through less so that listings can be scrolled.
  • alias fetch='fetchmail -u USERNAME -p PASSWORD MAILSERVER -d 120 (where USERNAME is the mail account username, PASSWORD is the user’s mail account password, and MAILSERVER is the address of the mail server) - fetches mail from a remote system for the specified user at two-minute intervals.
  • alias ap='aterm -tr -tint -fg white -bg green +sb -e pine' - opens up Pine in a green-tinted, transparent aterm.
  • alias gr$p='grep -v '^#" /etc/syslog.conf | grep -v '^$' - prints all the lines from /etc/syslog.com that are neither commented (starting with #) nor empty (^$).
  • alias tailmes='tail -f -n 10 /var/log/messages' - follows the last 10 lines of the /var/log/messages file.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

0 comments

Editor's Picks