How to set hostname and FQDN on CentOS 7 and RHEL 7

本文介绍如何在 CentOS 7 和 RHEL 7 系统中设置主机名与全限定域名 (FQDN)。通过编辑 /etc/hosts 和 /etc/hostname 文件来实现更改,并提供了实际操作步骤和命令示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The tutorial will explain about how to set hostname and FQDN on CentOS 7 and RHEL 7 (Red Hat Enterprise Linux 7). In our previous post “How to change hostname on CentOS and Red Hat“, we have already define the difference between hostname and FQDN. Hence suggest to read this post.

In Red Hat Enterprise Linux 7 or CentOS 7 , we have found slight changes.

You must have the ip address of system , which you can get by using the command ip addr list.

The given below is our system information, replace these values as per your system information.

IP Address : 192.168.56.101
Hostname : nix (We have selected this hostname, use hostname as per your interest)
FQDN (Fully Qualified Domain Name) : FQDN is combination of hostname+domain-name . Hence , here we are taking domain name as example.com .Therefore FQDN is nix.example.com

To change the hostname and FQDN on RHEL 7 and CentOS follow the given below steps.

Step 1: Edit /etc/hosts file

Now edit the /etc/hosts file in system. And add new line as given below

vi /etc/hosts

192.168.56.101 nix.example.com nix

This is the screenshot from our system.
Hostname CentOS 7

Step 2: Edit /etc/hostname

Edit the /etc/hostname file and give the hostname you have selected.

vi /etc/hostname

nix

The below given is reference from our system.

[root@localhost ~]# cat /etc/hostname
nix
[root@localhost ~]#

Alternatively,you can also use hostnamectl command to set the hostname. It also update the /etc/hostname file which we have edit manually in above steps.

hostname nix is used in below given command.

hostnamectl --static set-hostname nix

Step 3: Print the hostname and FQDN

Now you do not require to restart or logout-login from RHEL 7 and CentOS 7 which is the best thing we have found. Hostname and FQDN are set.
Now print the hostname and FQDN of system.

To print the hostname ,use the command –

hostname

To print the FQDN ,use the command –

hostname -f

Whereas, in case hostname and FQDN is not taking effect. Then run the below command

systemctl restart systemd-hostnamed

Below given is reference from our system

[root@nix ~]# hostname
nix
[root@nix ~]# 
[root@nix ~]# hostname -f
nix.example.com
[root@nix ~]# 
[root@nix ~]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值