bash提示转义序列\ h从何处获取主机名? [英] Where does bash prompt escape sequence \h get the hostname from?

查看:168
本文介绍了bash提示转义序列\ h从何处获取主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

\h是bash提示符转义序列,扩展为主机名.它从哪里获得主机名?在我的系统上,它显示了一个我在任何地方都找不到的值,而不是在hostname -f/etc/hosts/etc/hostname/etc/sysconfig/network$HOSTNAME中.所以我想知道它从哪里得到的.我的系统是Centos 7.4.我知道有些地方存储UUID之类的东西,而且我似乎还记得过去曾遇到过类似的隐藏主机名类型问题,但我不记得详细信息了.

\h is a bash prompt escape sequence that expands to the hostname. Where does it get the hostname from? On my system it shows a value that I cannot find anywhere, not in hostname -f or /etc/hosts or /etc/hostname or /etc/sysconfig/network or $HOSTNAME. So I'm wondering where it's getting it from. My system is Centos 7.4. I know there are hidden places where things such as UUIDs are stored, and I seem to recall that I've come across a similar hidden hostname type of issue in the past, but I can't remember the details.

推荐答案

如果您查看 bash源代码,您会在shell.c中看到它调用 gethostname(2),这是从内核检索主机名的POSIX系统调用.

If you look at the bash source code you'll see in shell.c that it calls gethostname(2), a POSIX system call that retrieves the hostname from the kernel.

/* It's highly unlikely that this will change. */
if (current_host_name == 0)
  {
    /* Initialize current_host_name. */
    if (gethostname (hostname, 255) < 0)
      current_host_name = "??host??";
    else
      current_host_name = savestring (hostname);
  }

这不一定是规范字符串.内核实际上并不知道计算机的网络主机名.它只是报告返回传递给 sethostname(2)的任何内容.引用 uname(2)手册页:

This is not necessarily a canonical string. The kernel doesn't actually know the machine's network hostname. It just reports back whatever was passed to sethostname(2). To quote from the uname(2) man page:

另一方面,[主机名]是没有意义的:它在一些未定义的网络中提供了本机的名称,但是通常,机器位于多个网络中并且具有多个名称.此外,内核无法了解此类情况,因此必须告诉内核该怎么回答.

On the other hand, the [hostname] is meaningless: it gives the name of the present machine in some undefined network, but typically machines are in more than one network and have several names. Moreover, the kernel has no way of knowing about such things, so it has to be told what to answer here.

在没有gethostname(2)的非Linux系统上,bash退回到 uname(2).如果uname(2)甚至不可用,则它仅显示未知".您可以在lib/sh/oslib.c中看到该逻辑:

On non-Linux systems without gethostname(2), bash falls back to uname(2). If uname(2) isn't even available then it simply displays "unknown". You can see that logic in lib/sh/oslib.c:

#if !defined (HAVE_GETHOSTNAME)
#  if defined (HAVE_UNAME)
#    include <sys/utsname.h>
int
gethostname (name, namelen)
     char *name;
     int namelen;
{
  int i;
  struct utsname ut;

  --namelen;

  uname (&ut);
  i = strlen (ut.nodename) + 1;
  strncpy (name, ut.nodename, i < namelen ? i : namelen);
  name[namelen] = '\0';
  return (0);
}
#  else /* !HAVE_UNAME */
int
gethostname (name, namelen)
     char *name;
     int namelen;
{
  strncpy (name, "unknown", namelen);
  name[namelen] = '\0';
  return 0;
}
#  endif /* !HAVE_UNAME */
#endif /* !HAVE_GETHOSTNAME */

如果主机名更改,

\h不会更新.初始化外壳程序时,将在启动时缓存该值.

\h isn't updated if the hostname changes. The value is cached at startup when the shell is initialized.

[jkugelman@malkovich]$ hostname
malkovich
[jkugelman@malkovich]$ sudo hostname kaufman
[jkugelman@malkovich]$ hostname
kaufman
[jkugelman@malkovich]$ bash
[jkugelman@kaufman]

这篇关于bash提示转义序列\ h从何处获取主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆