Home Operation and Maintenance Linux Operation and Maintenance What are the ways to check the host name in Linux?

What are the ways to check the host name in Linux?

Jun 07, 2023 pm 06:03 PM
linux Host

linux查看主机名的方法有10种,分别是:1、使用hostnamectl;2、使用hostname;3、使用uname;4、使用nmcli;5、使用sysctl;6、使用cat /etc/hostname;7、使用cat /etc/hosts;8、通过ProcFS;9、使用nmtui;10、通过/etc/sysconfig/network查看即可。

What are the ways to check the host name in Linux?

本教程操作系统:linux5.18.14系统、Dell G3电脑。

1,查看主机名使用hostnamectl

hostnamectl 可用于查询和更改系统主机名和相关设置。运行 hostnamectl 命令以查看系统主机名,命令如下所示:

[root@localhost sharplee]# hostnamectl
或者
[root@localhost sharplee]# hostnamectl status
Copy after login

What are the ways to check the host name in Linux?

从上图可以看出当前的主机名是localhost.localdomain。

2,查看主机名使用hostname

主机名是用于设置或显示系统的当前主机、域或节点名。许多网络程序都使用这些名称来标识计算机。NIS/YP 也使用该域名,命令如下:

[root@localhost sharplee]#hostname
Copy after login

What are the ways to check the host name in Linux?

3,查看主机名使用uname

uname(含义是unix名称)是一个实用命令,它打印系统信息,如名称,版本和有关系统的其他详细信息,以及在其上运行的操作系统。命令如下:

[root@localhost sharplee]# uname -a | awk '{print $2}'
Copy after login

What are the ways to check the host name in Linux?

4,查看主机名使用nmcli

nmcli命令是主要用于网络管理控制以及网络状态报表。nmcli主要用于创建、显示、编辑、删除、激活和停用网络连接,以及控制和显示网络设备状态。命令如下:

[root@localhost sharplee]# nmcli general hostname
Copy after login

What are the ways to check the host name in Linux?

5,查看主机名使用sysctl

sysctl命令主要用于Linux运行时修改内核参数,可用的参数是 /proc/sys/ 下列出的参数。Procfs 是 Linux 中 sysctl 支持所必需的。您可以使用 sysctl 读取和写入 sysctl 数据。查看主机名命令如下所示。

[root@localhost sharplee]# sysctl kernel.hostname
Copy after login

What are the ways to check the host name in Linux?

通过上面五个命令可以查看主机名,接下来再来看一下后面的五个,后面五个主要用于补充说明以及扩展。

6,查看主机名使用cat /etc/hostname

通过查看/etc/hostname文件来查看主机名。命令如下所示。

[root@localhost sharplee]# cat /etc/hostname
Copy after login

What are the ways to check the host name in Linux?

7,查看主机名使用cat /etc/hosts

通过查看 /etc/hosts 文件来查看主机名。 /etc/hosts 主要用于配置本机的dns映射关系,一般是ip地址主机名,用于ip和主机的映射关系。命令如下所示。

[root@localhost sharplee]# cat /etc/hosts | grep 127.0.0.1
Copy after login

What are the ways to check the host name in Linux?

8,查看主机名通过ProcFS

proc 文件系统 (procfs) 是 Unix 操作系统中的一个特殊文件系统,它提供有关进程的信息和其他系统信息。它有时被称为进程信息伪文件系统。它不包含"真实"文件,而是运行时系统信息(例如系统内存,装载的设备,硬件配置等)。

[root@localhost sharplee]# cat /proc/sys/kernel/hostname
Copy after login

What are the ways to check the host name in Linux?

9,查看主机名使用nmtui

nmtui是一个基于图形化的应用程序,用于与NetworkManager进行交互。启动 nmtui 时,系统会提示用户选择要执行的活动,除非选择退出,不然的话回车进去都是选择默认的参数,进去之后可修改。

[root@localhost sharplee]#nmtui
Copy after login

What are the ways to check the host name in Linux?

10,查看主机名通过/etc/sysconfig/network

"/etc/sysconfig/network"文件指定对系统上的所有网络接口都有效的其他信息,该命令只能用于RHEL/CentOS 6 系统。Centos6以上的系统都不能使用。

[root@localhost sharplee]#$ cat /etc/sysconfig/network | grep -i hostname
HOSTNAME=localhost.localdomain
Copy after login

通过以上10种方式可以看出,查看Linux主机名有很多种方式,但是小编在这里建议大家使用前五种通过命令的方式,这几种方式几乎涵盖了Linux中的任何系统类型。

The above is the detailed content of What are the ways to check the host name in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles