Administrator
发布于 2023-04-19 / 40 阅读
0
0

Hostname & /etc/hosts Best Practise

Linux命令参考文档:https://www.man7.org/linux/man-pages/dir_all_alphabetic.html

Best Practise

[root@VM-0-14-opencloudos ~]# hostname
VM-0-14-opencloudos


[root@VM-0-14-opencloudos etc]# cat /etc/hostname
VM-0-14-opencloudos

# 修改主机名
[root@VM-0-14-opencloudos network-scripts]# hostnamectl set-hostname weipeng.host1

# 查看主机名
[root@VM-0-14-opencloudos network-scripts]# hostnamectl 
   Static hostname: weipeng.host1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: da3451ed949b4c458cb1dddaddf35a7c
           Boot ID: a3d6cb7b2b7842fd85d184e9744472e4
    Virtualization: kvm
  Operating System: OpenCloudOS 8.6
       CPE OS Name: cpe:/o:opencloudos:opencloudos:8
            Kernel: Linux 5.4.119-20.0009.20
      Architecture: x86-64

# 通过上面的hostnamectl命令,来修改主机名,即使重启,也是会生效的。

Hostname

refer to : https://www.man7.org/linux/man-pages/man5/hostname.5.html

hostname - Local hostname configuration file

/etc/hostname

The /etc/hostname file configures the name of the local system.
Unless overridden as described in the next section, systemd(1)
will set this hostname during boot using the sethostname(2)
system call.

The file should contain a single newline-terminated hostname string.
Comments (lines starting with a “#”) are ignored. The hostname should be composed of up to 64 7-bit ASCII lower-case alphanumeric characters or hyphens forming a valid DNS domain name. It is recommended that this name contains only a single label, i.e. without any dots.

Invalid characters will be filtered out in an attempt to make the name valid, but obviously it is recommended to use a valid name and not rely on this filtering.

You may use hostnamectl(1) to change the value of this file during runtime from the command line.

hostnamectl

refer to : https://www.man7.org/linux/man-pages/man1/hostnamectl.1.html

hostnamectl - Control the system hostname

hostnamectl may be used to query and change the system hostname and related settings.

COMMANDS top
The following commands are understood:

   status
       Show system hostname and related information. 
       If no command is specified, this is the implied default.

  set-hostname NAME
       Set the system hostname to NAME. By default, this will alter the pretty, the static, and the transient hostname alike;
       Pass the empty string "" as the hostname to reset the selected hostnames to their default (usually "localhost").

       
   hostname [NAME]
       If no argument is given, print the system hostname. 
       
       If an optional argument NAME is provided then the command changes the system hostname to NAME.
       
       By default, this will alter the pretty, the static, and the transient hostname alike
      
      The static and transient hostnames must each be either a
       single DNS label (a string composed of 7-bit ASCII lower-case
       characters and no spaces or dots, limited to the format
       allowed for DNS domain name labels), or a sequence of such
       labels separated by single dots that forms a valid DNS FQDN.
       
       The hostname must be at most 64 characters, which is a Linux
       limitation (DNS allows longer names).

os-release

# 查看系统名称
[root@VM-0-14-opencloudos ~]# cat /etc/os-release
NAME="OpenCloudOS"
VERSION="8.6"
ID="opencloudos"
ID_LIKE="rhel fedora"
VERSION_ID="8.6"
PLATFORM_ID="platform:oc8"
PRETTY_NAME="OpenCloudOS 8.6"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:opencloudos:opencloudos:8"
HOME_URL="https://www.opencloudos.org/"
BUG_REPORT_URL="https://bugs.opencloudos.tech/"


# 查看架构和内核版本

[root@VM-0-14-opencloudos ~]# hostnamectl status
   Static hostname: VM-0-14-opencloudos
         Icon name: computer-vm
           Chassis: vm
        Machine ID: da3451ed949b4c458cb1dddaddf35a7c
           Boot ID: a3d6cb7b2b7842fd85d184e9744472e4
    Virtualization: kvm
  Operating System: OpenCloudOS 8.6
       CPE OS Name: cpe:/o:opencloudos:opencloudos:8
            Kernel: Linux 5.4.119-20.0009.20
      Architecture: x86-64

/etc/hosts

overview

/etc/hosts文件(域名解析文件)是一个用于储存计算机网络中各节点信息的计算机文件。

这个文件负责将主机名称映射到相应的IP地址。hosts文件通常用于补充或取代网络中DNS的功能。

hosts文件包含了ip地址和主机名之间的映射,包括主机名的别名,在没有域名服务器的情况下,系统上的所有网络程序都通过查询该文件来解析对应于某个主机名的ip地址,否则就需要使用DNS服务程序来解决。

通常可以将常用的域名和ip地址映射加入到hosts文件中,实现快速方便的访问.

优先级:dns缓存>hosts>dns服务
hosts:the static table lookup for host name(主机名查询静态表),主要用于IP地址与计算机主机名之间的转换。

format

/etc/hosts文件格式,每行表示一个IP地址,每行由三部分构成:

IP_address                                  canonical_hostname                 [aliases.......]

IP地址                                      规范的主机名 或域名                   主机名别名

(1)#注释内容

(2)host names 只能包含 数字、字母、连字符(-)、点(.);且必须以字母开始,以字母或者数字结尾。

(3)主机名别名是可选的

(4)尽管host table已被DNS取代,但是依然被广泛的使用:

系统引导:多数系统拥有较小的host table,包含本地网络中重要hosts的名字和地址信息。DNS没有启动的时候,这很有用。

[root@VM-0-14-opencloudos ~]# cat /etc/hosts
127.0.0.1 VM-0-14-opencloudos VM-0-14-opencloudos
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4

::1 VM-0-14-opencloudos VM-0-14-opencloudos
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

注:在/etc/hosts中录入ip及其主机名之后,可以使用主机名进行局域网内的远程登录或者连接,更加快捷;

修改后执行/etc/init.d/network restart使配置立即生效.

/etc/host.conf

/etc/host.conf是域名解析的配置文件,域名解析有两种办法:

(1)使用DNS系统
(2)使用/etc/hosts文件

作用
/etc/host.conf可以指定域名解析方法顺序。

一般内容为:

 # /etc/host.conf
 # We have named running, but no NIS (yet)
 order   bind hosts
 # Allow multiple addrs
 multi   on
 # Guard against spoof attempts
 nospoof on
 # Trim local domain (not really necessary).
 trim    vbrew.com.

order:指定采用哪种顺序。
hosts:采用/etc/hosts文件解析
bind: 采用DNS解析。
具体使用参考:https://www.tldp.org/LDP/nag/node82.html

/etc/resolv.conf

该文件是由域名解析器(resolver,一个根据主机名解析IP地址的库)使用的配置文件该文件是DNS域名解析的配置文件,它的格式很简单,每行以一个关键字开头,后接配置参数。

resolv.conf的关键字主要有四个,分别是:
nameserver      #定义DNS服务器的IP地址
domain          #定义本地域名
search          #定义域名的搜索列表
sortlist        #对返回的域名进行排序

nameserver:表明DNS服务器的IP地址。可以有很多行的nameserver,每一个带一个IP地址。在查询时就按nameserver在本文件中的顺序进行,且只有当第一个nameserver没有反应时才查询下面的nameserver。

domain:声明主机的域名。很多程序用到它,如邮件系统;当为没有域名的主机进行DNS查询时,也要用到。如果没有域名,主机名将被使用,删除所有在第一个点( .)前面的内容。

示例如下:

[root@VM-0-14-opencloudos network-scripts]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 183.60.83.19
nameserver 183.60.82.98


评论