Linux Commands Examples

A great documentation place for Linux commands

hostname

show or set the system’s host name domainname - show or set the system’s NIS/YP domain name ypdomainname - show or set the system’s NIS/YP domain name nisdomainname - show or set the system’s NIS/YP domain name dnsdomainname - show the system’s DNS domain name

Synopsis

hostname [-v] [-a|--alias] [-d|--domain] [-f|--fqdn|--long] [-A|--all-fqdns] [-i|--ip-address] [-I|--all-ip-addresses] [-s|--short] [-y|--yp|--nis]
hostname
[-v] [-b|--boot] [-F|--file filename] [hostname]
hostname
[-v] [-h|--help] [-V|--version]

domainname [nisdomain] [-F file]
ypdomainname
[nisdomain] [-F file]
nisdomainname
[nisdomain] [-F file]

dnsdomainname [-v]


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!

examples

0
source

Hostname does not have a FQDN?? - Linux/Ubuntu

edit /etc/hosts to add your FQDN

Information on syntax located here: http://www.faqs.org/docs/securing/chap9sec95.html

update: reading over your question again it almost sounds like you either don't have your path set right, or there is something wrong with the hostname program.

do 'which hostname'

it should return with the path '/bin/hostname'

if that works try the command again like,

'/bin/hostname --fqdn'

0
source

Finding IP or hostname of origin machine (ssh)

Id I understood correctly you are connection directly from origin machine to destination with proxycommand hops. And so here are three hacks for you (third added after comment on usage scenario). First) Use remote port forwarding to allow you to get back to you origin machine with -R remotemachineport:dstonlocalnet:dstportonlocalnet

ssh -R 2222:localhost:22 user@target
# on "target" you can now do this to get back to origin host
ssh user2@localhost -p 2222 

Second) Abuse AcceptEnv LC_* on target. It's not nice but it's quite common to allow LOCALE variables even whan AcceptUserEnviconment is not available. So now you can do:

export LC_SOURCEHOST=my.ip.addr.or:something
ssh user@target
# on tathet:
echo $LC_SOURCEHOST

Third) Use ssh remote forward to identify host or host type.

# Here is an example what you can use on target machine. 
# This will modify your PS1 variable (prompt) based on source host (port forwarding)
# Add this to .bash_profile

function checkHost {
  RET=""
  ## loop over test port numbers 17891-17895 in my example
  for x in $(seq 1 5); do 
    # if netstat is not available test port some other way like with nc or something
    ## && RET= will set RET = 1-5
    /bin/netstat -lnt|/bin/grep -q 127.0.0.1:1789$x && RET=$x;
  done
  # return RET
  # please note that if you have multiple open connections with different port numbers
  # this method cannot not distinguish between them 
  echo $RET
}

# get 1-5 number from function above
VAL=$(checkHost)
# do something like set PS1 var or map vim file or something
export PS1='\[\033k\033\\\]\u@\h: \w\$ '$VAL

Now connect with port forwarding:

### this will still enable ssh forwarding back. Change 22 to something else like 
### 24 to disable it (if nothing is listening on your source machines 24 port)
ssh -R 17891:localhost:22 user@target
0
source

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 10.1.1.188\n send" | nsupdate

In long form, this is:

nsupdate
> server ns1.domain
> zone domain
> update delete test.domain
> update add test.domain 600 A 10.1.1.188
> 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 { 10.10.10.0/24; };

This would allow updates from the 10.10.10.0/24 network

0
source

Address VMWare Fusion Linux guest by hostname?

If you are using NAT, it may try to register with DNS servers, however to computers other than the host, it's IP will be the same as your machines and can cause a few problems.

I would recommend that you switch to bridged networking as this will give it its own IP address and to any machine, it will look and feel like any other machine on the network. If you set up all the IP settings correctly (or use DHCP), it should automatically register and you should not have a problem doing anything on any port via IP or hostname.

0
source

RedHat Enterprise Linux 6 - Edit Hostname

You have to do a little bit more than using hostname. The following link below should solve your problem. Change your Hostname without Rebooting in RedHat Linux

Make sure you are logged in as root and move to /etc/sysconfig and open the network file in vi.

cd /etc/sysconfig
vi network

Look for the HOSTNAME line and replace it with the new hostname you want to use. In this example I want to replace localhost with redhat9.

HOSTNAME=redhat9

When you are done, save your changes and exit vi. Next we will edit the /etc/hosts file and set the new hostname.

vi /etc/hosts

In hosts, edit the line that has the old hostname and replace it with your new one.

192.168.1.110     redhat9

Save your changes and exit vi. The changes to /etc/hosts and /etc/sysconfig/network are necessary to make your changes persistent (in the event of an unscheduled reboot).

Now we use the hostname program to change the hostname that is currently set.

hostname redhat9

And run it again without any parameters to see if the hostname changed.

hostname

Finally we will restart the network to apply the changes we made to /etc/hosts and /etc/sysconfig/network.

service network restart
0
source

Putty can't connect over SSH to hostname, IP connection works

Probably you are not using the correct name. I can imagine you are using something like an URL (http://host_name.com/). Try tou use only the name host_name.com. We need more info about the name you are using.

0
source

Linux change environmental variables without rebooting for all shells

You can't do anything that would push out a environmental variable to already running shells.

So you options are to either start new shells, or do something in each shell that is already running to take the new environmental variable.

The easiest way would be to export the new variable in each shell:

export HOSTNAME=mynewhostname

Replacing 'mynewhostname' with your new hostname.

0
source

Detect host's name

You can change the hostname to whatever you want as long you alter other configuration files along with the change.

As far as I know, there is no direct way of fetching the hostname of the computer unless there is a DNS entry pointing to the IP address with that hostname. However, some servers might have an API method or a procedure call which makes them get and print/send the hostname.

0
source

Configure Ubuntu hostnames

In /etc/hostname you have to put the unqualified name for the host; in your example you need to put

one

In /etc/hosts, which is a DNS resolving file, you need to specify which IP address is host one. Let's suppose one has IP address 192.162.1.2, then you have to add this line to your /etc/hosts:

192.168.1.2 one.example.com one

(as stated on man hosts: *IP_address canonical_hostname [aliases...]*).

Hope this helps

EDIT: I forgot: for Ubuntu AFAIK you have to run /etc/init.d/hostname.sh as root after modifying the hostname file. I'm not sure, but that's how it's done in Debian.

0
source

How to remotely find out a linux hostname without the use of DNS?

In the absence of a DNS server, this might work:

traceroute 1.2.3.4

Alternatively, if the server in question is exporting Samba shares, you could do this:

smbclient -L 1.2.3.4

This depends on whether you have a working DNS server set up on your LAN that has the necessary information. If it does, this command should do what you need:

nslookup 1.2.3.4

or

host 1.2.3.4

Finally, another useful command is

arp -a
0
source

Is it possible to connect to a device using its hostname?

On some networks – yes. But on your everyday TCP/IP network, the hostname must be translatable to an IP address, otherwise clients will not know what to connect to.

There are several existing protocols for local name lookup, though:

  • Windows uses NetBIOS name services for resolving hostnames of other computers in the same LAN, using UDP broadcasts. Your server will need to run the nmbd component of Samba to be discoverable using NetBIOS.

  • OS X uses mDNS ("multicast DNS"), sometimes known as Bonjour, for the same purposes. (Note that mDNS generally does not resolve bare hostnames like NetBIOS would; instead, it is limited to the .local domain.) For mDNS, your server will need avahi-daemon.

  • (Also, Windows can resolve mDNS names if Bonjour is installed (typically as part of iTunes), and OS X should support NetBIOS if "Windows file sharing" or a similar service is enabled.)

  • Desktop-oriented Linux systems often come with both NetBIOS and mDNS resolvers preconfigured. (That is, they run both nmbd and avahi-daemon, plus the relevant glibc NSS modules in /etc/nsswitch.conf.)

  • Finally, most "home routers" maintain a local DNS domain (.lan, .home, &c.) according to hostnames they receive from DHCP requests sent by computers in the LAN. Depending on what your server uses, dhcpcd needs the "hostname" option, while dhclient uses "send host-name = gethostname()".

(The new protocol used by Windows 7 – LLMNR – is omitted because it is inferior to mDNS, it is specific to IPv6, and it apparently does not have a decent Linux implementation anyway.)

0
source

How do I find out my computer network name (Linux)?

On a computer that is using the University's DNS servers, you can try ping -a x.x.x.x (Windows) or host x.x.x.x (Linux).

If your University's DNS servers are not set up for dynamic DNS entries, you will not receive a name for the host. I'm suprised your University allows SSH connections (or allocates you public addresses) for that matter. You may want to set up your own name using DynDNS.

0
source

Only root can resolve DNS hostnames on Debian

I would first check the permissions on /etc/resolv.conf and /etc/nsswitch.conf. Both of these should have read permissions for all. Test if you can read these files with cat or less. If you can't, then it is unlikely the resolver can.

You can run the resolver on its own with the command getent hosts google.com. Substitute google.com with whatever domain you want to look up.

0
source

How can I access a VMware Fusion guest running Ubuntu via its .local (Bonjour/Zeroconf/mdns) address?

how about adding a line like

172.16.90.128   ubuntu.local

to your /etc/hosts file:

description

Hostname is used to display the system’s DNS name, and to display or set its hostname or NIS domain name.

GET NAME
When called without any arguments, the program displays the current names:

hostname will print the name of the system as returned by the gethostname(2) function.

domainname will print the NIS domainname of the system. domainname uses the gethostname(2) function, while ypdomainname and nisdomainname use the yp_get_default_domain(3).

dnsdomainname will print the domain part of the FQDN (Fully Qualified Domain Name). The complete FQDN of the system is returned with hostname --fqdn (but see the warnings in section THE FQDN below).

SET NAME
When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname uses the sethostname(2) function, while all of the three domainname, ypdomainname and nisdomainname use setdomainname(2). Note, that this is effective only until the next reboot. Edit /etc/hostname for permanent change.

Note, that only the super-user can change the names.

It is not possible to set the FQDN or the DNS domain name with the dnsdomainname command (see THE FQDN below).

The host name is usually set once at system startup in /etc/init.d/hostname.sh (normally by reading the contents of a file which contains the host name, e.g. /etc/hostname).

THE FQDN
The FQDN (Fully Qualified Domain Name) of the system is the name that the resolver(3) returns for the host name, such as, ursula.example.com. It is usually the hostname followed by the DNS domain name (the part after the first dot). You can check the FQDN using hostname --fqdn or the domain name using dnsdomainname.

You cannot change the FQDN with hostname or dnsdomainname.

The recommended method of setting the FQDN is to make the hostname be an alias for the fully qualified name using /etc/hosts, DNS, or NIS. For example, if the hostname was "ursula", one might have a line in /etc/hosts which reads

127.0.1.1 ursula.example.com ursula

Technically: The FQDN is the name getaddrinfo(3) returns for the host name returned by gethostname(2). The DNS domain name is the part after the first dot.

Therefore it depends on the configuration of the resolver (usually in /etc/host.conf) how you can change it. Usually the hosts file is parsed before DNS or NIS, so it is most common to change the FQDN in /etc/hosts.

If a machine has multiple network interfaces/addresses or is used in a mobile environment, then it may either have multiple FQDNs/domain names or none at all. Therefore avoid using hostname --fqdn, hostname --domain and dnsdomainname. hostname --ip-address is subject to the same limitations so it should be avoided as well.

options

-a, --alias

Display the alias name of the host (if used). This option is deprecated and should not be used anymore.

-A, --all-fqdns

Displays all FQDNs of the machine. This option enumerates all configured network addresses on all configured network interfaces, and translates them to DNS domain names. Addresses that cannot be translated (i.e. because they do not have an appropriate reverse DNS entry) are skipped. Note that different addresses may resolve to the same name, therefore the output may contain duplicate entries. Do not make any assumptions about the order of the output.

-b, --boot

Always set a hostname; this allows the file specified by -F to be non-existant or empty, in which case the default hostname localhost will be used if none is yet set.

-d, --domain

Display the name of the DNS domain. Don’t use the command domainname to get the DNS domain name because it will show the NIS domain name and not the DNS domain name. Use dnsdomainname instead. See the warnings in section THE FQDN above, and avoid using this option.

-f, --fqdn, --long

Display the FQDN (Fully Qualified Domain Name). A FQDN consists of a short host name and the DNS domain name. Unless you are using bind or NIS for host lookups you can change the FQDN and the DNS domain name (which is part of the FQDN) in the /etc/hosts file. See the warnings in section THE FQDN above, and avoid using this option; use hostname --all-fqdns instead.

-F, --file filename

Read the host name from the specified file. Comments (lines starting with a ’#’) are ignored.

-i, --ip-address

Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip-addresses instead.

-I, --all-ip-addresses

Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.

-s, --short

Display the short host name. This is the host name cut at the first dot.

-v, --verbose

Be verbose and tell what’s going on.

-V, --version

Print version information on standard output and exit successfully.

-y, --yp, --nis

Display the NIS domain name. If a parameter is given (or --file name ) then root can also set a new NIS domain.

-h, --help

Print a usage message and exit.

files

/etc/hostname Historically this file was supposed to only contain the hostname and not the full canonical FQDN. Nowadays most software is able to cope with a full FQDN here. This file is read at boot time by the system initialization scripts to set the hostname.

/etc/hosts Usually, this is where one sets the domain name by aliasing the host name to the FQDN.

notes

The address families hostname tries when looking up the FQDN, aliases and network addresses of the host are determined by the configuration of your resolver. For instance, on GNU Libc systems, the resolver can be instructed to try IPv6 lookups first by using the inet6 option in /etc/resolv.conf.


authors

Peter Tobias, <tobias@et-inf.fho-emden.de>
Bernd Eckenfels, <net-tools[:at:]lina.inka[:dot:]de> (NIS and manpage).
Michael Meskes, <meskes[:at:]debian[:dot:]org>

How can this site be more helpful to YOU ?


give  feedback