{"id":1153,"date":"2018-08-20T02:33:24","date_gmt":"2018-08-19T21:03:24","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1153"},"modified":"2019-04-27T07:43:11","modified_gmt":"2019-04-27T02:13:11","slug":"how-to-change-hostname-in-centos-7","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/how-to-change-hostname-in-centos-7\/","title":{"rendered":"How to Change Hostname in CentOS 7"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<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.<\/p>\n<p>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 ( \u2013 ) as special character.<\/li>\n<li>hostnames can contains the dot special character ( . ).<\/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>&nbsp;<\/p>\n<p>In order to display a computer name in CentOS 7 and RHEL 7 systems via console, issue the following command.<\/p>\n<p>The -s flag displayed the computer short name (hostname only) and the -f flag 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 class=\"theme:dark-terminal lang:default decode:true \"># hostname\n# hostname -s\n# hostname -f<\/pre>\n<p>You can also display a Linux system hostname by inspecting the content of \/etc\/hostname file using the cat command.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \"># cat \/etc\/hostname<\/pre>\n<p>In order to change or set a CentOS 7 machine hostname, use the hostnamectl command as shown in the below command excerpt.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \"># hostnamectl set-hostname your-new-hostname<\/pre>\n<p>In addition to hostname command you can also use hostnamectl command to display a Linux machine hostname.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \"># hostnamectl<\/pre>\n<p>In order to apply the new hostname, a system reboot is required, issue one of the below commands in order to reboot a CentOS 7 machine.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true\"># init 6\n# systemctl reboot\n# shutdown -r\n<\/pre>\n<p>A second method to setup a CentOS 7 machine hostname is to manually edit the \/etc\/hostname file and type your new hostname.<\/p>\n<p>Also, a system reboot is necessary in order to apply the new machine name.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \"># vi \/etc\/hostname<\/pre>\n<p>A third method that can be used to change a CentOS 7 machine hostname is by using Linux sysctl interface.<\/p>\n<p>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 class=\"theme:dark-terminal lang:default decode:true \"># sysctl kernel.hostname\n# sysctl kernel.hostname=new-hostname\n# sysctl -w kernel.hostname=new-hostname<\/pre>\n<p>To display machine transient hostname issue the below commands.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \"># sysctl kernel.hostname\n# hostnamectl<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, the hostnamectl command can be used to achieve the following hostname setups: \u2013pretty, \u2013static, and \u2013transient.<\/p>\n<p>Although, there are other more specific ways to change a Linux machine hostname, such as issuing nmtui command or manually editing some configuration files specific to each Linux distribution (\/etc\/sysconfig\/network-scripts\/ifcfg-ethX for CentOS), the above rules are general available regardless of the used Linux distribution.<\/p>\n<p>&nbsp;<\/p>\n<p>We hope you\u2019ve found this useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1287,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1153"}],"version-history":[{"count":2,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1153\/revisions"}],"predecessor-version":[{"id":1159,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1153\/revisions\/1159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1287"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}