{"id":13175,"date":"2019-04-01T05:02:10","date_gmt":"2019-04-01T05:02:10","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13175"},"modified":"2019-04-01T05:02:10","modified_gmt":"2019-04-01T05:02:10","slug":"how-to-set-or-change-hostname-in-centos-7","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-set-or-change-hostname-in-centos-7\/","title":{"rendered":"How to Set or Change Hostname in CentOS 7"},"content":{"rendered":"<p>A computer hostname represents a unique name that gets assigned to a computer in a network in order to uniquely identify that computer in that specific network. A computer hostname can be set to any name you like, but you should keep in mind the following rules:<\/p>\n<ul>\n<li>hostnames can contain letters (from a to z).<\/li>\n<li>hostnames can contain digits (from 0 to 9).<\/li>\n<li>hostnames can contain only the hyphen character\u00a0<code>( \u2013 )<\/code>\u00a0as special character.<\/li>\n<li>hostnames can contains the dot special character\u00a0<code>( . )<\/code>.<\/li>\n<li>hostnames can contain a combination of all three rules but must start and end with a letter or a number.<\/li>\n<li>hostnames letters are case-insensitive.<\/li>\n<li>hostnames must contains between 2 and 63 characters long.<\/li>\n<li>hostnames should be descriptive (to ease identifying the computer purpose, location, geographical area, etc on the network).<\/li>\n<\/ul>\n<p>In order to display a computer name in\u00a0<strong>CentOS 7<\/strong>\u00a0and\u00a0<strong>RHEL 7<\/strong>\u00a0systems via console, issue the following command. The\u00a0<code>-s<\/code>\u00a0flag displayed the computer short name (hostname only) and the\u00a0<code>-f<\/code>\u00a0flag displays the computer FQDN in the network (only if the computer is a part of a domain or realm and the FQDN is set).<\/p>\n<pre># hostname\r\n# hostname -s\r\n# hostname -f\r\n<\/pre>\n<div id=\"attachment_28114\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Check-Hostname-in-CentOS-7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28114\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Check-Hostname-in-CentOS-7.png\" alt=\"Check Hostname in CentOS 7\" width=\"415\" height=\"248\" aria-describedby=\"caption-attachment-28114\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-28114\" class=\"wp-caption-text\">Check Hostname in CentOS 7<\/p>\n<\/div>\n<p>You can also display a Linux system hostname by inspecting the content of\u00a0<strong>\/etc\/hostname<\/strong>\u00a0file using the\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>.<\/p>\n<pre># cat \/etc\/hostname\r\n<\/pre>\n<div id=\"attachment_28115\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Display-CentOS-7-Hostname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28115\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Display-CentOS-7-Hostname.png\" alt=\"Display CentOS 7 Hostname\" width=\"315\" height=\"75\" aria-describedby=\"caption-attachment-28115\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-28115\" class=\"wp-caption-text\">Display CentOS 7 Hostname<\/p>\n<\/div>\n<p>In order to change or set a\u00a0<strong>CentOS 7<\/strong>\u00a0machine hostname, use the\u00a0<strong>hostnamectl<\/strong>\u00a0command as shown in the below command excerpt.<\/p>\n<pre># hostnamectl set-hostname <strong>your-new-hostname<\/strong>\r\n<\/pre>\n<p>In addition to\u00a0<strong>hostname<\/strong>\u00a0command you can also use\u00a0<strong>hostnamectl<\/strong>\u00a0command to display a Linux machine hostname.<\/p>\n<pre># hostnamectl\r\n<\/pre>\n<p>In order to apply the new hostname, a system\u00a0<strong>reboot<\/strong>\u00a0is required, issue one of the below commands in order to reboot a CentOS 7 machine.<\/p>\n<pre># init 6\r\n# systemctl reboot\r\n# shutdown -r\r\n<\/pre>\n<div id=\"attachment_28116\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Set-CentOS-7-Hostname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28116\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Set-CentOS-7-Hostname.png\" alt=\"Set CentOS 7 Hostname\" width=\"452\" height=\"306\" aria-describedby=\"caption-attachment-28116\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-28116\" class=\"wp-caption-text\">Set CentOS 7 Hostname<\/p>\n<\/div>\n<p>A second method to setup a\u00a0<strong>CentOS 7<\/strong>\u00a0machine hostname is to manually edit the\u00a0<strong>\/etc\/hostname<\/strong>\u00a0file and type your new hostname. Also, a system reboot is necessary in order to apply the new machine name.<\/p>\n<pre># vi \/etc\/hostname\r\n<\/pre>\n<p>A third method that can be used to change a\u00a0<strong>CentOS 7<\/strong>\u00a0machine hostname is by using Linux\u00a0<strong>sysctl<\/strong>\u00a0interface. However, using this method to change machine name results in setting-up the machine transient hostname.<\/p>\n<p>The transient hostname is a special hostname initialized and maintained only by the Linux kernel as an auxiliary machine name in addition to he static hostname and doesn\u2019t survive reboots.<\/p>\n<pre># sysctl kernel.hostname\r\n# sysctl kernel.hostname=new-hostname\r\n# sysctl -w kernel.hostname=new-hostname\r\n<\/pre>\n<p>To display machine transient hostname issue the below commands.<\/p>\n<pre># sysctl kernel.hostname\r\n# hostnamectl\r\n<\/pre>\n<div id=\"attachment_28117\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Change-CentOS-7-Hostname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28117\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/12\/Change-CentOS-7-Hostname.png\" alt=\"Change CentOS 7 Hostname\" width=\"448\" height=\"468\" aria-describedby=\"caption-attachment-28117\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-28117\" class=\"wp-caption-text\">Change CentOS 7 Hostname<\/p>\n<\/div>\n<p>Finally, the\u00a0<strong>hostnamectl<\/strong>\u00a0command can be used to achieve the following hostname setups:\u00a0<strong>\u2013pretty<\/strong>,\u00a0<strong>\u2013static<\/strong>, and\u00a0<strong>\u2013transient<\/strong>.<\/p>\n<p>Although, there are other more specific ways to\u00a0<a href=\"https:\/\/www.tecmint.com\/set-hostname-permanently-in-linux\/\" target=\"_blank\" rel=\"noopener\">change a Linux machine hostname<\/a>, such as issuing\u00a0<strong>nmtui command<\/strong>\u00a0or manually editing some configuration files specific to each Linux distribution (<strong>\/etc\/sysconfig\/network-scripts\/ifcfg-ethX<\/strong>\u00a0for CentOS), the above rules are general available regardless of the used Linux distribution.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/set-change-hostname-in-centos-7\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A computer hostname represents a unique name that gets assigned to a computer in a network in order to uniquely identify that computer in that specific network. A computer hostname can be set to any name you like, but you should keep in mind the following rules: hostnames can contain letters (from a to z). &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-set-or-change-hostname-in-centos-7\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Set or Change Hostname in CentOS 7&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13175","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13175"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13175\/revisions"}],"predecessor-version":[{"id":13176,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13175\/revisions\/13176"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}