{"id":2917,"date":"2018-02-22T17:11:34","date_gmt":"2018-02-22T14:11:34","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-hostname-command\/"},"modified":"2018-02-22T17:11:34","modified_gmt":"2018-02-22T14:11:34","slug":"linux-hostname-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-hostname-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux hostname Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>If you are into network administration, and your work involves working on the Linux command line, there are several network-related command line tools that you should be aware of. On such tool is <strong><em>hostname<\/em><\/strong>, which you can use to display or set system&#8217;s host name. In this tutorial, we will learn more about this utility using some easy to understand examples.<\/p>\n<p>Please note that all examples here have been tested on Ubuntu 16.04 machine and cross tested on an Ubuntu server machine running 14.04 LTS version.<\/p>\n<h2 id=\"linux-hostname-command\">Linux hostname command<\/h2>\n<p>You can use hostname to show or set the system&#8217;s host name. The basic syntax of the tool is:<\/p>\n<p class=\"command\">hostname [options]<\/p>\n<p>Here&#8217;s how the man page documentation says about this utility:<\/p>\n<pre>Hostname is used to display the system's DNS name, and to display or set its hostname or NIS domain <br\/>name.<\/pre>\n<p>Following are some Q&amp;A-styled examples that will give you a good idea on how this command works.<\/p>\n<h2 id=\"q-how-hostname-works\">Q1. How hostname works?<\/h2>\n<p>By default, the hostname command produces the name of the host in output.<\/p>\n<p class=\"command\">hostname<\/p>\n<p><a class=\"fancybox\" id=\"img-hostname\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/hostname.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-hostname-command-tutorial-for-beginners-5-examples.png\" alt=\"Display the hostname\" width=\"234\" height=\"35\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-print-the-hosts-alias-name\">Q2. How to print the host&#8217;s alias name?<\/h2>\n<p>If there&#8217;s an alias name associated with the host, you can access that using the -a command line option.<\/p>\n<p class=\"command\">hostname -a<\/p>\n<h2 id=\"q-how-to-print-the-ip-address\">Q3. How to print the IP address?<\/h2>\n<p>This can be done using the -i command line option.<\/p>\n<p class=\"command\">hostname -i<\/p>\n<p>Here&#8217;s the output of the command on my local machine:<\/p>\n<p><a class=\"fancybox\" id=\"img-hostname-i\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/hostname-i.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-hostname-command-tutorial-for-beginners-5-examples-1.png\" alt=\"Show IP address of the hostname\" width=\"258\" height=\"34\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-print-the-associated-domain-name-if-any\">Q4. How to print the associated domain name (if any)?<\/h2>\n<p>You can use the -d command line option for this.<\/p>\n<p class=\"command\">hostname -d<\/p>\n<p>The above command will fetch you the domain name where the system resides.<\/p>\n<h2 id=\"q-how-to-print-short-hostname\">Q5. How to print short hostname?<\/h2>\n<p>This can be done using the -s command line option.<\/p>\n<p class=\"command\">hostname -s<\/p>\n<p><a class=\"fancybox\" id=\"img-hostname-s\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/hostname-s.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-hostname-command-tutorial-for-beginners-5-examples-2.png\" alt=\"Display short hostname\" width=\"256\" height=\"36\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-change-hostname\">Q6. How to change hostname?<\/h2>\n<p>The hostname command lets you change the name of the host as well. You can do this in the following way:<\/p>\n<p class=\"system command\">hostname [new-host-name]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">hostname howtoforge<\/p>\n<p><a class=\"fancybox\" id=\"img-hostname-set\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/hostname-set.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-hostname-command-tutorial-for-beginners-5-examples-3.png\" alt=\"Change Linux hostname\" width=\"372\" height=\"67\" title=\"\"><\/a><\/p>\n<p>Needless to say, you&#8217;ll need root privileges to carry out this operation. Also, change done through this way is temporary &#8211; for permanent change, you&#8217;ll have to make modifications in the <strong>\/etc\/hosts<\/strong> file.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>You can do a lot more with hostname, although what we&#8217;ve discussed here should be enough to at-least get you started. Once you are done with all this, head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/hostname\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to get more info on it.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you are into network administration, and your work involves working on the Linux command line, there are several network-related command line tools that you should be aware of. On such tool is hostname, which you can use to display or set system&#8217;s host name. In this tutorial, we will learn more about this utility [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2917","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2917"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2917\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}