Linux Commands Examples

A great documentation place for Linux commands


DNS lookup utility

see also : dig


host [-aCdlnrsTwv] [-c class] [-N ndots] [-R number] [-t type] [-W wait] [-m flag] [-4] [-6] {name} [server]

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!



LINUX Ping host, Display error on failure

I think awk is not necessary. Unless I'm missing something that code should do the trick:


ping -c1 $host > /dev/null 2> /dev/null
[[ $? == 0 ]] && echo "$host is up" || echo "$host is down/not reachable"

Here an example:

$ ./checkping is up
$ ./checkping is down/not reachable


How to route somehost to other host with some port on linux?

Shorewall routes. (FAQ)

You want to redirect all local connection requests to a server running in your local zone at and listening on port 333. Your local interface is eth1.

Disclaimer; I've not done exactly this with Shorewall, so modified an example Squid server setup:

  1. Add this entry to your /etc/shorewall/providers file.

    foo     1       202     -               eth1       loose
  2. In /etc/shorewall/tcrules add:

    #MARK    SOURCE              DEST        PROTO    DEST
    #                                                 PORT(S)
    202:P   tcp      80
  3. In /etc/shorewall/interfaces :

    loc     eth1         detect       routeback          
  4. On localhost, arrange for the following command to be executed after networking has come up

    iptables -t nat -A PREROUTING -i eth0 -d ! -p tcp --dport 80 -j REDIRECT --to-ports 333          

Hostname resolution of Linux machines with static IP

Yes, you can use nsupdate in your /etc/network/if-up.d/ directory.

For example

echo -e "server ns1.domain\n zone domain\n update delete test.domain\n update add test.domain 600 A\n send" | nsupdate

In long form, this is:

> server ns1.domain
> zone domain
> update delete test.domain
> update add test.domain 600 A
> send

So this saying

  1. Choose the ns1.domain name server
  2. update the "domain" zone
  3. Delete the current record
  4. Add the new record
  5. Send to name server

On the name server, you will need to allow updates, which in bind is the directive:

allow-update {; };

This would allow updates from the network


What tools exist to override an IP from a DNS lookup per browser?

A method that works with all browsers is to set up a proxy server, such as Privoxy or Squid, that has the ability to rewrite requests. When a HTTP proxy server is used, the full URL is sent to the proxy without performing any name resolution.


How to block access to a website on Lucid Lynx?

You can block the website's IP using an iptables rule. Something along the flavor provided below.

iptables -A OUTPUT -d -j DROP

Just replace the zeros with the IP of the website you wish to deny access to. This syntax also supports a wildcard. By typing an IP with a zero in it, you are effectively blocking the entire span of that field. For example, references the IP range of to

In addition to blocking by IP, you can also block by hostname using this same syntax. Just change the IP address to the domain name and it'll do the rest of the work for you.

So you can do all of what you wanted in iptables (freeing up your hosts file from needless records).


How can I tell if I am SSH'd in somwhere already?

If you know you are 'ssh'ed, but don't know from where, try


at a command-line. The output includes a FROM field which may be useful.


Locally redirecting domains (like with /etc/hosts) during certain hours of the day

Maybe something like this would solve your problem:


Adding a line into the hosts file, getting permission denied when using sudo - Mac

That's because echo is being run as root, but the shell is the one actually performing the redirection. You need to spawn a new shell for this to work:

sudo -- sh -c "echo test >> /etc/hosts"

Edit: I haven't seen the fact that the > redirect works; I can't explain that.


host(1) command only uses first nameserver?

Remove the first two nameserver entries from /etc/resolv.conf if they do not know about hosts on your LAN or about the domain used locally.

The NXDOMAIN response might be because isn't a registered domain name that can be used on the internet.

$ dig any

; <<>> DiG 9.3.6-P1-RedHat-9.3.6-4.P1.el5_5.3 <<>> any
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 7984
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;             IN      ANY

;; AUTHORITY SECTION:              1200    IN      SOA 2007041700 3600 600 604800 1200

;; Query time: 209 msec
;; WHEN: Tue Aug 30 13:29:23 2011
;; MSG SIZE  rcvd: 95

Note that the authoritative nameservers for say there is no such domain as I guess it is a convenience used by 2wire products at the LAN level only. is a Google nameserver and should not be expected to know about hosts on your private LAN and the somewhat strange configuration choices made by your 2wire router.

The name of the computer thefourthtower should appear in it's own /etc/hosts table (which you should arrange to be consulted before DNS) and really ought to be known to the DNS nameserver on your LAN (i.e. your 2wire router).

Does your /etc/nsswitch.conf contain hosts: files dns ?


Hosts not recognized?

First of all, are you running this from home? Big chance is that if you are your ISP does not allow certain connections or has a firewall installed in your router.


Check your iptables


Are asterisks in /etc/hosts supposed to have any effect?

You have answered your own question!

The /etc/hosts file is a one-to-one mapping between a hostname (not a collection of them) to a particular IP address.

Consider putting this into /etc/hosts file *.com


how to properly edit hosts, hostname and resolf.conf?

/etc/hostname should have just foo

/etc/hosts looks fine

/etc/resolv.conf should probably be for both, depending on your use case. Note that you named it /etc/resolf.conf and it should be v and not f

I don't know about domainname


Redirect a url to another url

No. /etc/hosts can only be used to map hostnames to IP addresses; you will need to set up a web server that performs the redirect to the port.


How to configure and access both local and remote versions of same website/domain?

Most OSes will resolve from their hosts file before they try finding a DNS server. If you remove (or comment out) that line from hosts, it will then query the DNS server and find that it's a remote IP.

You may also have to change the hostname, if it's set to the same, as hostname may be used to resolve IPs as well (at least by some systems in Linux).

hosts.conf man page - See more information about the order directive.


host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options.

name is the domain name that is to be looked up. It can also be a dotted-decimal IPv4 address or a colon-delimited IPv6 address, in which case host will by default perform a reverse lookup for that address. server is an optional argument which is either the name or IP address of the name server that host should query instead of the server or servers listed in /etc/resolv.conf.

The -a (all) option is equivalent to setting the -v option and asking host to make a query of type ANY.

When the -C option is used, host will attempt to display the SOA records for zone name from all the listed authoritative name servers for that zone. The list of name servers is defined by the NS records that are found for the zone.

The -c option instructs to make a DNS query of class class. This can be used to lookup Hesiod or Chaosnet class resource records. The default class is IN (Internet).

Verbose output is generated by host when the -d or -v option is used. The two options are equivalent. They have been provided for backwards compatibility. In previous versions, the -d option switched on debugging traces and -v enabled verbose output.

List mode is selected by the -l option. This makes host perform a zone transfer for zone name. Transfer the zone printing out the NS, PTR and address records (A/AAAA). If combined with -a all records will be printed.

The -i option specifies that reverse lookups of IPv6 addresses should use the IP6.INT domain as defined in RFC1886. The default is to use IP6.ARPA.

The -N option sets the number of dots that have to be in name for it to be considered absolute. The default value is that defined using the ndots statement in /etc/resolv.conf, or 1 if no ndots statement is present. Names with fewer dots are interpreted as relative names and will be searched for in the domains listed in the search or domain directive in /etc/resolv.conf.

The number of UDP retries for a lookup can be changed with the -R option. number indicates how many times host will repeat a query that does not get answered. The default number of retries is 1. If number is negative or zero, the number of retries will default to 1.

Non-recursive queries can be made via the -r option. Setting this option clears the RD — recursion desired — bit in the query which host makes. This should mean that the name server receiving the query will not attempt to resolve name. The -r option enables host to mimic the behavior of a name server by making non-recursive queries and expecting to receive answers to those queries that are usually referrals to other name servers.

By default, host uses UDP when making queries. The -T option makes it use a TCP connection when querying the name server. TCP will be automatically selected for queries that require it, such as zone transfer (AXFR) requests.

The -4 option forces host to only use IPv4 query transport. The -6 option forces host to only use IPv6 query transport.

The -t option is used to select the query type. type can be any recognized query type: CNAME, NS, SOA, SIG, KEY, AXFR, etc. When no query type is specified, host automatically selects an appropriate query type. By default, it looks for A, AAAA, and MX records, but if the -C option was given, queries will be made for SOA records, and if name is a dotted-decimal IPv4 address or colon-delimited IPv6 address, host will query for PTR records. If a query type of IXFR is chosen the starting serial number can be specified by appending an equal followed by the starting serial number (e.g. -t IXFR=12345678).

The time to wait for a reply can be controlled through the -W and -w options. The -W option makes host wait for wait seconds. If wait is less than one, the wait interval is set to one second. When the -w option is used, host will effectively wait forever for a reply. The time to wait for a response will be set to the number of seconds given by the hardware’s maximum value for an integer quantity.

The -s option tells host not to send the query to the next nameserver if any server responds with a SERVFAIL response, which is the reverse of normal stub resolver behavior.

The -m can be used to set the memory usage debugging flags record, usage and trace.


Copyright © 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
Copyright © 2000-2002 Internet Software Consortium.



idn support

If host has been built with IDN (internationalized domain name) support, it can accept and display non-ASCII domain names. host appropriately converts character encoding of domain name before sending a request to DNS server or displaying a reply from the server. If you’d like to turn off the IDN support for some reason, defines the IDN_DISABLE environment variable. The IDN support is disabled if the variable is set when host runs.

see also

dig , named.

How can this site be more helpful to YOU ?

give  feedback