Bash alias and its usefulness



So as of late I've been running into some issues on servers I manage. A good example is today, I ran into a situation where mail was being stuck in the queue due to Amavis not running. While this is find and dandy, and an easy fix (had to change the system's hostname), I quickly got bored of typing out the same long string each time. So, I decided to open up my .bashrc and start cracking, and here's some helpful functions and aliases to get you started!

Postfix Management

Of course there's a web administration panel for Postfix, but that's no fun! Why not use the CLI instead? That's what I'm here for!

While some of this can be done using the postsuper, I decided to hold of on that. Instead, I created this function in .bashrc:

function mail_queue(){
        case "$1" in
                        postqueue -p
                        postqueue -f
                        echo "Searching mail queue for e-mail account $2..."
                        mailq | awk 'BEGIN { RS = "" } / $2$/ { print $1 }' | tr -d '*!'

What it does is simple. It provides a wrapper to some common Postfix administration features (flushing and showing the queue, as well as searching the queue for a specific e-mail address, showing the mail ID). How to use this, you ask? Simple! With these alias lines:

alias mq_list='mail_queue list'
alias mq_send='mail_queue flush'
alias mq_search='mail_queue search'

After you put these in, all you need to do is run source ~/.bashrc and you're set. If you want to see the queue, just type mq_list. Want to search the queue? mq_search [email protected]. Simple as pie! This can definitely be extended as well, this is just what I have on my servers.

Firewall Management

I'm a huge fan of iptables. While there are scripts out there to ease the management of iptables, I didn't want all the fluff it offered. So, I wrote my own aliases!

One of my favorites as of late (which I give edougherty from the LF IRC channel credit for as well) is listing the chains:

alias fw_list='clear && iptables --line-numbers -L -nv'

As an example, here's what my INPUT chain looks like when I run fw_list:

Chain INPUT (policy DROP 654 packets, 37174 bytes)
num  pkts bytes target    prot opt in    out    source              destination   
[... block of stuff removed ...]
13    297K  74M ACCEPT    all  --  *      *            ctstate RELATED,ESTABLISHED
14    8016  479K MYSQL      all  --  *      *       
15      0    0 DROP      icmp --  *      *            icmp type 17
16      0    0 DROP      icmp --  *      *            icmp type 13
17      11  1006 ACCEPT    icmp --  *      *            icmp type 255 limit: avg 1/sec burst 5
18    359 19594 LOG        all  --  *      *            limit: avg 2/min burst 5 LOG flags 0 level 7 prefix `INPUT [block]: '

clear is used to empty out the screen buffer. I then call iptables with the --line-numbers switch to make running iptables -D that much easier. -L is for listing the current rules. -nv is for not converting port numbers to their service name and not resolving IP address. With all of the switches used, you can also see what rules are the most used and which ones you may be able to delete (the more ruels you have the slower the firewall runs).

The next two I'll bundle into one:

alias fw_save='iptables-save > /etc/iptables.rules'
alias fw_restore='iptables-restore < /etc/iptables.rules'

Instead of having to manually type the save and restore commands all the time, why not make it a few short characters? Easy enough, right? That's because it is!

SSH Connections

The last one I'll show I used to have as a function that the alias would call, but as a quick set up I skipped writing it as a function and just set them as an alias.

alias kyo='ssh -p 4022 <user>@<the north pole>'
alias kar='ssh <server>'

Now, instead of having to remember the login for server "kyo", for example, which requires a painstaking approach, I can just type in kyo in the CLI and it'll send me to the log in.


Aliases are useful, and can make managing a computer or everyday tasks so much easier when you use the CLI. One last example, which I took from my days of using Ubuntu and then switching to Arch:

function service_call () {
    /etc/rc.d/$1 $2
alias service='service_call'

What does this do? Simple. If you've ever used Ubuntu, then you know service networking restart just restarts the adapters by calling /etc/init.d/networking restart, right? Well, same thing here! Calling service php restart would expand the alias to /etc/rc.d/php restart.
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Latest posts