Linux

HOW TO : Find which interface a particular IP address is configured on

There are a ton of scripts to find how many IP addresses  are configured on a system, but I could not find one, whic would show me which particular network interface an IP address was configured on. Here is a one liner, that will give you this information in Linux

/sbin/ifconfig | grep -B1 10.10.10.10 | awk '{if (NR==1) print $1}'

The same script can be changes a bit to support other operating systems too. Essentially, I am doing a grep (search) of the output of ifconfig, which shows all the network information on the system for a particular IP. At the same time, I am using the -B1 option, which will show the line above the matching line. Finally, I am piping this to awk and printing the first row in the first column.

HOW TO : Pass environment variables when using sudo

Say you need to sudo as a particular user and run a command and at the same time you need to pass an environmental variable to the command, you can do it by passing the command in doublequotes.

For example, I want to start Oracle while I am logged in as another user (vinay), I can start the database using dbstart by issues

"sudo su - oracle -c "dbstart /$ORACLE_HOME"

$ORACLE_HOME is an environmental variable listed under user oracle’s environment.

Needless to say, you need to ensure that you have sudo configured to allow your userID to su to oracle.

HOW TO : Block outbound e-mails in Postfix

I ran into a challenge at work, where we had to allow e-mail delivery for certain domains, but block all other domains. But at the same time, we had to ensure that the clients sending e-mails did not get a delivery error. We were using Postfix as the MTA running on Redhat Linux. Here’s how I resolved it

  • Edit the main.cf file (the default location is in /etc/postfix) and add “transport_maps = hash:/etc/postfix/transport” (without the quotes) to the file.
  • Create a file named “transport” in /etc/postfix, if it doesn’t exist
  • Add the following at the end of the transport file

DOMAIN1 :
DOMAIN2 :
* discard:

  • Run “postmap /etc/transport” to create a hash of the transport file
  • Run “service postfix restart” to restart the postfix service

This configuraiton will ensure that all e-mails address to DOMAIN1 and DOMAIN2 are delivered normally, but the rest of the e-mails are silently discarded.

Note : Ensure that you follow the syntax for where to place the : verbatim.

HOW TO : Force expire sudo security permissions..

Ever run into a situation when you thought you had sudo rights on a machine and tried to issue the sudo command and upon finding that you don’t have them..get your name added to the sudoers list by begging the sysadmin.. and then frusrated when sudo keeps throwing an error that you are not part of the sudoers list? Hmm.. that is a long sentance :)..

To expire any cached security permissions, so that sudo is forced to check the sudoers files, issue the following command

sudo -k

HOW TO : View HTML pages in Linux (command line)

If you are stuck in a terminal on a Linux workstation and need to view a html file.. you can use the following command

links NAME_OF_HTML_FILE

Links displays the HTML code in the page by default..If you want to just view the rendered HTML, press “\” and you can toggle between HTML and Text views.

P.S : You need to have links installed to use it :).. But most of the new distributions have it installed by default.

Tools of the trade : Postfix Configuration

Good link with information on configuring a custom transport link in Postfix. A transport link in a SMTP server is a way to define a communiction/delivery channel with particular settings. Example, you want all e-mails for a particular domain to be sent to a particular server at a particular time.

http://linuxnet.ca/postfix/dedicated_transport.html

In this particular link, the author shows the steps to configure a transport link for sending e-mails to servers/domains with high latency.