Select Your Language

Infrastructure and management.

  • Red Hat Enterprise Linux
  • Red Hat Satellite
  • Red Hat Subscription Management
  • Red Hat Insights
  • Red Hat Ansible Automation Platform

Cloud Computing

  • Red Hat OpenShift
  • Red Hat OpenStack Platform
  • Red Hat OpenShift Container Platform
  • Red Hat OpenShift Data Science
  • Red Hat OpenShift Dedicated
  • Red Hat Advanced Cluster Security for Kubernetes
  • Red Hat Advanced Cluster Management for Kubernetes
  • Red Hat Quay
  • OpenShift Dev Spaces
  • Red Hat OpenShift Service on AWS
  • Red Hat Gluster Storage
  • Red Hat Hyperconverged Infrastructure
  • Red Hat Ceph Storage
  • Red Hat OpenShift Data Foundation
  • Red Hat Runtimes
  • Red Hat JBoss Enterprise Application Platform
  • Red Hat Data Grid
  • Red Hat JBoss Web Server
  • Red Hat Single Sign On
  • Red Hat support for Spring Boot
  • Red Hat build of Node.js
  • Red Hat build of Quarkus

Integration and Automation

  • Red Hat Application Foundations
  • Red Hat Fuse
  • Red Hat AMQ
  • Red Hat 3scale API Management

How do I change the IP address/netmask and hostname on the command line WITHOUT rebooting the machine?

Here are some example configuration files:

/etc/sysconfig/network

/etc/sysconfig/network-scripts/ifcfg-eth0

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content

Formatting Tips

Here are the common uses of Markdown.

Request a English Translation

Generating machine translation.

Tecmint: Linux Howtos, Tutorials & Guides

How to Set or Change System Hostname in Linux

Device or system hostnames are used to easily recognize a machine within a network in a human readable format. It is not much of a surprise, but on Linux system, the hostname can be easily changed by using simple command as “ hostname “.

Read Also: How to Set Static IP Address and Configure Network in Linux

Running hostname on its own, without any parameters, will return the current hostname of your Linux system like this:

If you want to change or set hostname of your Linux system, simply run:

Of course, you will need to replace “NEW_HOSTNAME” with the actual hostname that you wish to set. This will change the hostname of your system immediately, but there is one problem – the original hostname will be restored upon next reboot.

There is another way to change the hostname of your system – permanently. You might have already figured it out that this will require change in some configuration files and you will be correct.

Set System Hostname Permanently in Linux

Newer version of different Linux distributions such as latest Ubuntu , Debian , CentOS , Fedora , RedHat , etc. comes with systemd , a system and service manager that provides a hostnamectl command to manage hostnames in Linux.

To set system hostname on SystemD based distributions, we will use hostnamectl command as shown:

For Older Linux distributions, which uses SysVinit in short init , can have their hostnames changed by simply editing the hostname file located in:

You then have to add another record for the hostname in:

For example:

You then need to run:

On RHEL/CentOS based systems that use init , the hostname is changed by modifying:

Here is a sample of that file:

To keep a permanent hostname change the value next to "HOSTNAME" to the one of your hostname.

This simple article meant to show you a simple Linux trick and I hope that you learned something new.

Previous article:

Next article:

Photo of author

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Related Posts

Extract Linux Tar Files Different or New Directory

How to Extract Tar Files to Specific or Different Directory in Linux

Debian Goodies Utilities to Manage Debian Packakges

8 Useful ‘Debian Goodies Utilities’ for Package Management

Delete User Accounts with Home Directory in Linux

How to Delete User Accounts and Their Home Directories in Linux

Find and Kill Running Processes in Linux

How to Find and Kill Running Processes in Linux

Set System Time, Date and Timezone in Linux

How to Set Time, Timezone and Synchronize System Clock in Linux

FFMPEG Command Examples in Linux

15 Must-Know FFmpeg Commands for Video, Audio & Image Conversion

5 thoughts on “How to Set or Change System Hostname in Linux”

I am using capital letters as hostname using the hostnamectl set-hostname command. But hostname is applying small cases.

Any solution for this?

Great help. Very clear. Thanks.

Thanks for the hostnamectl command

I want to delete the hostname.

Nice work, thanks

Got something to say? Join the discussion. Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Save my name, email, and website in this browser for the next time I comment.

  • Articles Automation Career Cloud Containers Kubernetes Linux Programming Security

7 ways to set your hostname in Fedora, CentOS, or Red Hat Enterprise Linux

%t min read | by Seth Kenlon (Editorial Team, Red Hat)

Lights

A hostname is a human-readable string that helps people refer to a computer by a familiar name, rather than by a number or unwieldy descriptors like, "the third one from the bottom of the second-to-last rack." Often, a hostname is set during the installation process, but there are times when it needs to be changed. On Linux, there are many ways to set a hostname, and this article aims to cover them all.

Before exploring the tools related to hostnames, though, you must understand the different contexts in which the term is used. There are potentially two designators of a system’s hostname: the computer’s administrator (a laptop’s owner, or a server’s root user) and the network (depending on protocols and settings). This factor can lead to confusion, because you might look at your computer’s hostname and see one value, only to find that the same computer is referred to as something different over the network.

There’s a historical reason for this situation. Long ago, before DNS, hosts on a network had to be defined locally in the file /etc/hosts . If there were 31 hosts on a network and a new one was added, then 32 hosts had to update their /etc/hosts file to reflect the correct IP address and corresponding hostname for each of their neighbors. This process didn’t scale well for bigger networked systems such as the World Wide Web (the Internet), and so DNS was invented, and the concept of hostnames largely was abstracted away from local computers to instead be managed by the network.

Today, the important hostname is the one the network uses. The hostname value in /etc/hosts is often set to localhost by default.

With this context in mind, here are all the different ways to manipulate a hostname on Linux.

[ Free download: Advanced Linux commands cheat sheet . ]

Change all three names with hostnamectl

The hostnamectl command from systemd can manipulate three varieties of hostnames:

  • Transient : Received from network configuration.
  • Static : Provided by the kernel.
  • Pretty : Provided by the user.

A transient hostname can change as needed to avoid name collisions. For instance, if you name your computer penguin but there’s already another host with that name on the network, your network hostname becomes penguin-1 .

Static and pretty hostnames are a little like local variables: They’re used for activities occurring on the local machine, mostly as a convenience for the user, along with applications that need to know whether they’re running locally or remotely (for example, over a forwarded X session ).

When invoked without any arguments, hostnamectl returns the static and pretty names, plus some system information:

Change the static and pretty names in the GUI with GNOME Settings

If you want to set the static and pretty names in a desktop application, use GNOME Settings. To launch GNOME Settings, go to the Activities menu in the upper left corner of your GNOME desktop. Type Settings into the search field, or click the Show Applications icon on the left dock and find Settings in the application icons as shown below:

The (GNOME) Settings icon within Show Applications.

Change the static name with cockpit

Fedora, CentOS, and RHEL systems feature a web console application called Cockpit for monitoring and configuring local and remote machines. Using Cockpit, you can change the static hostname for your own machine, or any machine you administer (as long as it has Cockpit enabled).

First, install and enable Cockpit:

Logging into Cockpit.

In the web console, click the System tab in the left column. Click the entry for Host name and edit the Pretty Host Name and (static) Real Host Name :

Changing the pretty and static hostnames in Cockpit.

Change the static name in the hostname file

You can also set the static hostname manually by editing  /etc/hostname . This file contains exactly one line by default. If you have not changed the hostname yet, then that line probably reads localhost.localdomain . If you have used hostnamectl to change the hostname already, then this file reflects that change.

Editing this file manually is not necessarily recommended, because it doesn’t update all the other hostname values the way hostnamectl does. For example:

Change the static name with nmcli

Another way to change a static hostname is through the command line interface for Network Manager, nmcli :

Change the transient name with sysctl

The sysctl command allows you to configure kernel parameters while Linux is running (that is, instead of at boot time). Your computer’s transient hostname is a kernel parameter, so you can modify it with this command:

Change the transient name with hostname

The hostname command from the util-linux package is a simple tool to query and set the transient hostname.

To query your current hostname:

Be careful with  /etc/hosts

The /etc/hosts file is mostly historic, although it is used by some applications and protocols, and can be a useful method for creating shortcuts to hosts you use often. You can use it to set a hostname, but usually, you’re just creating an alias to localhost at IP address 127.0.0.1 (your computer’s network loopback address).

For example, if you change /etc/hosts from the default entries like this:

Follow the conformity convention

As you can tell from the iterations within this article, a computer’s hostname can get confusing should you use too many methods to set it, or if you change it frequently. While nothing enforces uniformity across hostname types, it’s a convention to keep everything the same on each computer.

Choose a memorable name and use it for your pretty name. Let the static and transient names be derived from that. Just as importantly, in a large network, choose meaningful names. For instance, all upper management computers may be named after a D&D monster, while all computers in the IT department may be named after a famous starship, and so on. Using a naming schema helps avoid name collisions and provides context to an otherwise dizzying list of network hosts.

Author’s photo

Seth Kenlon

Seth Kenlon is a UNIX geek and free software enthusiast. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the red hat developer program., related content.

Photo of a broken pink egg with smaller eggs spilling out

How to Change the Hostname in Linux

Want to change your Linux machine's hostname but can't figure out how? Here's a step-by-step guide to walk you through the process.

There are plenty of reasons why you may want to change the hostname of your Linux system. Unfortunately, changing your hostname is not exactly an intuitive process. Don’t worry, though, we’re going to show you how you can change your machine's hostname in less than a minute with just a few clicks and commands.

Although this guide uses Ubuntu to demonstrate the steps required to change the hostname, the commands should work on other Linux distributions as well.

What Is a Hostname?

All computers connected to a network have an IP address and a hostname. The hostname is the name used to identify a computer on a network in “human-readable” form. Without the hostname, when you try to locate a computer on a network, all you would see are numbers, or IP addresses, like 127.0.0.1.

While IP addresses are a great way for computers to identify each other, they don’t really do much to help people identify specific computers. For example, let’s say you want to set up a network shared folder on your Ubuntu system . If someone else wants to access that folder, they will be able to find it much easier if they see your computer on the network as “larry-laptop” rather than “192.168.0.24”.

One common reason for switching hostnames is to avoid duplicate names on the same network. Ubuntu, for example, sets the hostname of all new installations to “ubuntu” by default. If you have more than one computer on your home network and want them all running Ubuntu Linux, you’re going to have to change the default hostnames to avoid problems.

How to Change the Hostname Through System Settings

The easiest way for most people to change the hostname on a Linux machine is to simply go into the system settings and click on About . Your computer’s current hostname will appear as the first item on the list, under Device Name , or similar labels on other distros.

get linux hostname from the settings

It is not immediately obvious, but you can click on the Device Name item and a dialog box will appear, allowing you to rename your device. Simply type whatever name you would like to use into the dialog and then click on Rename .

change hostname from the settings

The new hostname will be permanently set and your computer will identify itself on your network with the new name.

Change the Hostname Using the Linux Terminal

The options that appear in your system settings depend on the desktop environment you are using. The process described above will work with Ubuntu’s default GNOME desktop. If you have switched your desktop environment or window manager, however, you may not have the option to change the hostname in system settings.

If that's the case, you can change your hostname by opening a terminal window and entering one simple command.

The hostnamectl command will allow you to view and change the hostname on most other Linux distributions. Simply open a terminal window and enter the following command to view your current hostname along with a few related details:

hostnamectl command on Ubuntu terminal

To change the hostname, enter the following command, replacing new-hostname with the name you’d like to use:

To verify that the new name has been set, type hostnamectl again and you’ll see the updated information.

Again, the command-line method for changing the Linux hostname should work on nearly all modern Linux distributions, not just Ubuntu.

Learn More About How Linux Networks Work

Now that you’ve successfully set your computer’s hostname, it's time to dip your toes into networking with Linux. If you’re having other network problems with your system (or even if you’re just curious), the ss command in Linux will help you troubleshoot the errors.

How to Change HostName and IP-Address in CentOS / RedHat Linux

You can use any one of the following methods to change the hostname and/or ip-address on RedHat related distributions.

If you want to change only the hostname you can either do it from command line, or from GUI as explained below.

To change the ip-address along with the hostname, follow the steps shown below.

I. Change HostName From Command Line

1. use hostname command to change hostname.

In this example, we’ll change the hostname from dev-server to prod-server.

hostname command by default will display the current hostname as shown below:

The following will change the hostname to prod-server.

Once the hostname is changed, verify that it has changed the hostname successfully. As you see below, it has changed the hostname to prod-server

2. Modify the /etc/hosts file

If you have entries in the /etc/hosts file with the old hostname, you should modify it.

For example, the entry for 127.0.0.1 line in the /etc/hosts file will still show the old hostname. In this example, it shows as dev-server.

Modify this file, and set the new hostname here. For example, change dev-server to prod-server as shown below.

3. Modify the /etc/sysconfig/network file

The /etc/sysconfig/network file also has an entry for HOSTNAME. Change the value here as shown below.

4. Restart the Network

Restart the network service, if you want any other services that are using the hostname to pickup the changes.

If this is not a production system, you can also reboot the system to make sure the hostname is changed properly, and the system is picking it up properly during startup.

II. Change Hostname from UI

If you have desktop related utilities installed on your system, you can change the hostname from the GUI.

Execute system-config-network from the command line.

This will display the “Network Configuration” GUI. Go to the “DNS” tab and change the hostname from here as shown below.

how to change hostname ip address in linux

III. Change the IP-Address

1. change ip-address temporarily using ifconfig.

You can change the ip-address of the server using ifconfig command as we discussed earlier. For example, the following changes the ip-address of the server on eth0 interface to 192.168.1.2

2. Change ip-address Permanently

Under the /etc/sysconfig/network-scripts directory, you’ll see file for every network interface on your system. For example, if your interface is “eth0”, you’ll see ifcfg-eth0 file under this directory.

Modify the ifcfg-eth0 file and change the IPADDR field accordingly as shown below to change the ip-address.

3. Modify /etc/hosts file

If you’ve defined the ip-address in the /etc/hosts file, make sure to change those also. For example, if you have a FQDN that was pointing to the old ip-address in the /etc/hosts file, change it to the new ip-address. Depending on how you’ve configured your system, you might not have to do this step.

Finally, restart the network service, for the system to pick-up the changes.

If this is not a production system, you can also reboot the system to make sure the hostname and ip-address is changed properly, and the system is picking it up properly during startup.

If you enjoyed this article, you might also like..

Comments on this entry are closed.

Will this work for VM also? I am looking to make my VM’s IP public. Is that possible using the above mechanism.

Simple but very useful material

Please note that just changing the host name as described in 1 will not survive a reboot. That would change the host name only on that session.

The other options are valid.

Nice Article ..

Good stuff. I noticed one spelling mistake…

II. Change Hostname from UI –> I think it should be GUI. Can you please correct it?

Don’t forget that to ensure that the hostname is persistent against reboots that you will also have to change the hostname in the /etc/sysconfig/network file on RHEL/CENTOS systems.

Thanks dear, it help lot for me.

Helpful material.

system-config-network is the best way to setup the hostname permanently, this would be ideal if you are you RHEL also you do not need any script in startup to save the changes permanently.

#cat /etc/hosts (to change the hostname )

before ediitng any file we have to take the back up

#cat /etc/sysconfig/network-scripts/ifcgf-eth0

here u have to modify the ipaddress,gateway,subnetmask

#cat /etc/sysconfig/network

here we have to modify the

Next post: How Much Swap Space to Add for Oracle Database on Linux

Previous post: How to Monitor MongoDB using Nagios check_mongodb Python Plugin

Linux 101 Hacks Book

  • Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
  • Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
  • Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
  • Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well

POPULAR POSTS

  • 15 Essential Accessories for Your Nikon or Canon DSLR Camera
  • 12 Amazing and Essential Linux Books To Enrich Your Brain and Library
  • 50 UNIX / Linux Sysadmin Tutorials
  • 50 Most Frequently Used UNIX / Linux Commands (With Examples)
  • How To Be Productive and Get Things Done Using GTD
  • 30 Things To Do When you are Bored and have a Computer
  • Linux Directory Structure (File System Structure) Explained with Examples
  • Linux Crontab: 15 Awesome Cron Job Examples
  • Get a Grip on the Grep! – 15 Practical Grep Command Examples
  • Unix LS Command: 15 Practical Examples
  • 15 Examples To Master Linux Command Line History
  • Top 10 Open Source Bug Tracking System
  • Vi and Vim Macro Tutorial: How To Record and Play
  • Mommy, I found it! -- 15 Practical Linux Find Command Examples
  • 15 Awesome Gmail Tips and Tricks
  • 15 Awesome Google Search Tips and Tricks
  • RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
  • Can You Top This? 15 Practical Linux Top Command Examples
  • Top 5 Best System Monitoring Tools
  • Top 5 Best Linux OS Distributions
  • How To Monitor Remote Linux Host using Nagios 3.0
  • Awk Introduction Tutorial – 7 Awk Print Examples
  • How to Backup Linux? 15 rsync Command Examples
  • The Ultimate Wget Download Guide With 15 Awesome Examples
  • Top 5 Best Linux Text Editors
  • Packet Analyzer: 15 TCPDUMP Command Examples
  • The Ultimate Bash Array Tutorial with 15 Examples
  • 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
  • Unix Sed Tutorial: Advanced Sed Substitution Examples
  • UNIX / Linux: 10 Netstat Command Examples
  • The Ultimate Guide for Creating Strong Passwords
  • 6 Steps to Secure Your Home Wireless Network
  • Turbocharge PuTTY with 12 Powerful Add-Ons
  • Linux Tutorials
  • Sed Scripting
  • Awk Scripting
  • Bash Shell Scripting
  • Nagios Monitoring
  • IPTables Firewall
  • Apache Web Server
  • MySQL Database
  • Perl Programming
  • Google Tutorials
  • Ubuntu Tutorials
  • PostgreSQL DB
  • Hello World Examples
  • C Programming
  • C++ Programming
  • DELL Server Tutorials
  • Oracle Database
  • VMware Tutorials

About The Geek Stuff

Copyright © 2008–2023 Ramesh Natarajan. All rights reserved | Terms of Service

how to change hostname ip address in linux

Change Linux Hostname and IP Address

Sometimes you might want to change the Linux hostname and IP address assigned to a Linux machine. In this article, we will be looking at Linux hostname and IP address change.

Change Linux Hostname

Change linux ip address.

Check the current hostname on the server

To change the hostname, give new hostname in the single quotes

Verify new hostname change

On a Linux minimal install, you can assign new IP address by editing /etc/sysconfig/network-scripts/ifcfg-eth3 file

Restart the network

Related Posts

how to change hostname ip address in linux

Add paragraph text. Click “Edit Text” to customize this theme across your site. You can update and reuse text themes.

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

How to point a hostname to an ip address

I have a running webserver, that can be called via http://localhost:9000/ . What I am trying to archive is, instead of calling http://localhost:9000/ , I would like to call http://repo.sweetsoft/ . I've tried to modify the hosts file as follow:

As you can see, I've added the last line but it does not work. What am I doing wrong?

zero_coding's user avatar

  • 1 You cannot add port number in /etc/hosts file; only IP address. –  FedKad Oct 13, 2019 at 10:13
  • Aha ok. Thanks a lot –  zero_coding Oct 13, 2019 at 10:16

2 Answers 2

There is a misunderstanding about hosts file here.

First of all, hosts file have precedence over DNS on most operating systems, you can define them on Linux/Unix operating systems and macOS in /etc/hosts and c:\windows\system32\drivers\etc\hosts on Windows .

So when you add a record in your hosts file like:

and try to open http://repo.sweetsoft/ in your browser, it doesn't send any DNS query to the outside world and uses this entry from your hosts file.

Keep in mind this only works for A record (resolving a name or domain address to an IPv4 address) and AAAA record (resolving a name or domain address to an IPv6 address) and you can not define TXT or MX records for example.

But port numbers are in a different network layer, hosts file only understands names (like repo.sweetsoft) and IP addresses, it's layer 3 and 4 in ISO model (Network/Transport) but port numbers are in layer 7 (application layer).

Check OSI model

enter image description here

OSI vs TCP/IP model

enter image description here

Since hosts file or DNS protocol are not aware of application layers, they have no idea about port numbers too.

Your configuration by adding 127.0.0.1:9000 to your hosts file is like adding port numbers to DNS A records.

After this clarification, you can fix this issue in multiple ways:

  • Running your application on port 80. Fixes your issue and removes any ambiguity.
  • Forward port 80 to port 9000 on your machine via iptables
  • Forward port 80 to port 9000 on your machine via socat:

If you can't change your application port, socat will be the easiest way.

For day to day usage, you can write a systemd service file to run it in the background.

  • 1 Isn't TCP port number in transport layer instead of application layer? –  Billy T Jan 12, 2023 at 19:50

You can go with a different approach:

  • 127.0.1.1 sweetsoft repo.sweetsoft in /etc/hosts
  • Direct repo.sweetsoft:80 and 127.0.0.1:9000 in your HTTP server (AFAIK, Apache can make it using VirtualHost Reference ).

However, it seem to me you are using an app development server. Using Apache to proxy may be an overkill.

user.dz's user avatar

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged networking server ..

  • The Overflow Blog
  • Exploring the inclusive tech revolution sponsored post
  • Would you trust an AI bot to find the fix for vulnerabilities in your code?
  • Featured on Meta
  • Site maintenance - Saturday, February 24th, 2024, 14:00 - 22:00 UTC (9 AM - 5...
  • Upcoming privacy updates: removal of the Activity data section and Google...
  • Notifying users about the policy on AU concerning AI-generated content
  • AI-generated content is not permitted on Ask Ubuntu

Hot Network Questions

  • Was Alexei Navalny in 2020 poisoned with Novitschok nerve agents by Russia's Federal Security Service?
  • Shimano clipless pedals and cleats
  • What is a system?
  • What exactly is transmission time?
  • Determining why my proof depends on the axiom of choice
  • Why are my new switches operating in reverse?
  • Were Mr. Darcy and Mr. Bennet formally equal in rank?
  • Is it possible for an animal to have a plant stage in its life cycle?
  • Can I walk to the Netherlands from Weeze?
  • What is the technical definition of "clone" that makes the K1810VM88 (К1810ВМ88) not a clone of the 8088?
  • Can a subshell create a subprocess?
  • Novel - Disguised alien Tock Von visits Earth, meets a girl, gets committed to a psychiatric hospital
  • The problem of philosophy?
  • Is this diesel gun concept feasible?
  • What does "rob it of a few petrified spars" mean in "Pride and Prejudice"?
  • How does this code work?
  • Lots of electrical noise at boost converter output
  • Odd use(s) of "bauen"
  • Markets in Germany with a large selection of seafood
  • Enumerate the Phat-fingered-lights-out numbers
  • Why is there a phasor in the transfer function of an ideal LPF?
  • ogr2ogr: Unable to import shapefile with '-lco RFC7946=YES' parameter to the PostGIS db
  • Compute the phat-fingered double-bit-flip distance
  • Partial derivative of the Gibbs free energy with respect to temperature at constant enthalpy

how to change hostname ip address in linux

5 ways to resolve hostname to ip address in linux

Nov 2, 2021

We all know we can use ping command to get the ip address of a hostname , but sometimes we need to do it for multiple hosts.

This post shows you how to resolve hostname to ip address in several ways , like use command ping or getent or host ,etc, so that we can do it via bash script.

Method 1: use ping command

ping command is always our friend , it’s very reliable and intuitive .

Let me explain above command a little bit:

  • -q Quiet output. Nothing is displayed except the summary lines at startup time and when finished
  • -c1 only send one ping packet
  • -t1 set TTL to 1 , if not pingable return immediately
  • tr -d '()' removed Parentheses around ip address
  • awk ... print ip address

Method 2: use getent command

The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf.

For IP v6 , you can use

For both IP v4 and v6:

Let see one example:

Here the domain has 2 IP addresses and a CNAME(kind of alias) new-fp-shed.wg1.b.yahoo.com points to one of these 2 IP addresses randomly.

So to get one of the IP address directly, below command can be used.

Method 3: use host command

host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options.

Similarly we can use below command to get one of the IP v4 address

Method 4: use dig command

Dig is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. Most DNS administrators use dig to troubleshoot DNS problems because of its flexibility, ease of use and clarity of output. Other lookup tools tend to have less functionality than dig.

For example:

Method 5: use resolveip command

The resolveip utility resolves host names to IP addresses and vice versa. This command is easy to use but seems not available by default.

Method 6 : use command nslookup

Nslookup is a program to query Internet domain name servers.

To get one of the IP v4 addresses only , you can try below command:

Bash script example

Where 2>/dev/null suppressed errors.

How to find the IP address in CMD using hostname?

command prompt to find hostname ip address

In networking, each system has its own identity denoted by a few numerical digits known as IP address, however, remembering them can be a difficult task, especially when there are a large number of systems. For example, each website running on the Internet has an IP address, so that other systems can call them whenever needed but do you think it is feasible to remember the long series of numbers for each website we visit?

The answer is “No”, hence corresponding to each IP address we can set a human-readable label. That’s the reason why when we need to visit Google we use its domain name Google.com instead of its IP address. Similarly, in our local networks, we have hostnames for each PC alongside IP addresses. This hostname allows us to connect PC if we don’t know the IP address.

However, if you don’t know the IP address corresponding to some hostname and want to find that, here in this tutorial we learn how to use the Command prompt or PowerShell to translate a hostname or Domain name to its corresponding IP address.

Step 1: Open Command Prompt

Well, as the title of this tutorial suggests we are going to use the command line on Windows to find the IP address associated with a hostname. Therefore, open the command prompt or PowerShell, click on the Windows Start button search for “ CMD ” as it appears click to run the same.

Alternatively , we can use the Windows key + R to open the Run dialog, typing “ cmd ,” and pressing Enter to access the Command prompt.

Rus as administrator command prompt

Also, learn – how to find a Wi-Fi password using the command prompt in Windows 11.

Step 2: Ping and NSLookUp

There are two common commands that we can use to find the IP address associated with a hostname, one is PING , and the other is NsLookUP , let’s see how to use them.

Ping Command to find the hostname IP

On your command prompt, and those are use PowerShell, run the “ ping ” command along with the hostname of the PC or device that IP address you want to identify. For example, our local hostname is “h2smedia” then the command to find its IP address:

ping hostname or domain

If you want to get IPv4 then add the ‘ -4 ‘ parameter in the above command, it will be like this:

Similarly, we can use any domain name to find its corresponding IP:

Using NSLookUp

Like ping, we can use the NSLookUp which is a specialized command line tool for querying Domain Name System to obtain the mapping between domain name and IP address:

nslookup hostname or domain

Step 3: View the IP Address

Well, the ping command as you run it, the system will start pinging the remote system or device to send packets to the mentioned hostname and in return will display the IP address along with information on whether the remote or local system is in reach or not. To identify the IP address just look for the line that starts with “Reply from” followed by the IP address.

Similarly, in the NSlokup command, you will directly have the Ip-address value as output, instead of confirming whether the host is reachable or not.

command prompt to find hostname ip address

Step 4: Note the IP Address

Once you have the IP address on the command prompt display, you can start using it to either connect the device available in your network or perform any troubleshooting task with the help of any third-party software such as Wireshark .

Other Articles:

  • Check if Virtualization is Enabled via Command Prompt (CMD)
  • Install PHP on Windows 10 | 11 using the Command prompt
  • How to start Firefox in safe mode with the command prompt?
  • How to create multiple folders in the Windows command prompt?

Inside Kodak TV Factory: A Glimpse into the Manufacturing Process

How to Reset Windows 11: A Step-by-Step Guide to Restore Your PC to Factory Setting

Related posts.

What is the DPC watchdog violation on Windows 11

What is the DPC watchdog violation on Windows 11? Why this is caused and the possible fixes

Reinstall Windows step by step to Factory settings

How to Check if Virtualization is Enabled via Command Prompt (CMD)

Install Virt viewer on Windows 11

Single Command to Install Virt-viewer on Windows 11 or 10

Enable nested VTXAmd-V option

How to enable Virtualbox nested VTX/Amd-V on Windows or Linux

Leave a comment cancel reply.

This site uses Akismet to reduce spam. Learn how your comment data is processed .

Reach out to us for sponsorship opportunities or suggestions

Problems are meant to be solved.

© 2024 H2S Media

IMAGES

  1. 3 Methods To Change the Hostname of Your Linux Machine

    how to change hostname ip address in linux

  2. How to Change Hostname on Ubuntu 18.04 {Without Restarting}

    how to change hostname ip address in linux

  3. Change Hostname on Ubuntu 20.04 {via GUI or Terminal}

    how to change hostname ip address in linux

  4. How To Change IP Address with Terminal on Linux

    how to change hostname ip address in linux

  5. How to Set or Change System Hostname in Linux

    how to change hostname ip address in linux

  6. How to Change Hostname in Linux

    how to change hostname ip address in linux

VIDEO

  1. How to convert/change hostname to IP address

  2. how to change hostname in cisco router @mazhinetwork786

  3. How to Change hostname In Linux server

  4. Windows Ip Address Check: Master The Art! #shorts #viral #trending #windows11 #ipconfig

  5. CARA KONFIGURASI IP ADDRESS LINUX UBUNTU SERVER 23.10 (MANTIC MINOTAUR) PADA VIRTUALBOX

  6. Change hostname and ip on linux terminal on server or machine (this case CentOS 8)

COMMENTS

  1. How to Change IP Address in Linux

    How to Change IP Address in Linux Quick Tip How to Change IP Address in Linux Learn different ways of changing the IP address in Linux. Also learn how to make the changes [ermanent. Sep 20, 2022 — Team LHB How to Change IP Address in Linux 10 Sec The Real Impact from Investing in $7M Super Bowl Ads

  2. How to change hostname on Linux

    For example: Change this: 127.0.0.1 localhost 127.0.1.1 linux-server To this: 127.0.0.1 localhost 127.0.1.1 linuxconfig Editing the hosts file That's all there is to it. But there are more command line methods to change the hostname than just this one. We cover more below. Change hostname from command line (init systems)

  3. How to Set or Change Hostname in Linux

    To change the hostname invoke the hostnamectl command with the set-hostname argument followed by the new hostname. Only the root or a user with sudo privileges can change the system hostname. For example, to change the system hostname to mail.linuxize.com, you would use the following command: sudo hostnamectl set-hostname mail.linuxize.com

  4. How To Change IP Address on Linux

    To change your IP address on Linux, use the "ifconfig" command followed by the name of your network interface and the new IP address to be changed on your computer. To assign the subnet mask, you can either add a "netmask" clause followed by the subnet mask or use the CIDR notation directly.

  5. How to configure a hostname on a Linux system

    To persistently change the hostname, use the hostnamectl command, or directly modify the default configuration file /etc/hostname. [ Learn how to manage your Linux environment for success. ] Here's an example of modifying the hostname permanently using the hostnamectl command. This shows the change: $ hostnamectl set -hostname server1.example.com

  6. Change Hostname on Ubuntu 20.04 {via GUI or Terminal}

    Type the following command: hostnamectl set-hostname new-hostname Use your own hostname choice instead of new-hostname. Step 2: Use hostnamectl to Confirm the Change Just like the hostname command, if successful, hostnamectl set-hostname does not produce any output. Therefore, use hostnamectl to check the result.

  7. How to change your hostname in Linux

    Background A quick bit of background. Before the invention of DNS, your computer's hostname was managed through the HOSTS file located at /etc/hosts. Anytime that a new computer was connected to your local network, all other computers on the network needed to add the new machine into the /etc/hosts file in order to communicate over the network.

  8. How do I change the IP address/netmask and hostname on the command line

    Learn about our open source products, services, and company. Get product support and knowledge from the open source experts. Read developer tutorials and download Red Hat software for cloud application development. Get training, subscriptions, certifications, and more for partners to build, sell, and support customer solutions.

  9. How to Set or Change System Hostname in Linux

    If you want to change or set hostname of your Linux system, simply run: $ hostname NEW_HOSTNAME Of course, you will need to replace "NEW_HOSTNAME" with the actual hostname that you wish to set. This will change the hostname of your system immediately, but there is one problem - the original hostname will be restored upon next reboot.

  10. 7 ways to set your hostname in Fedora, CentOS, or Red Hat Enterprise Linux

    Today, the important hostname is the one the network uses. The hostname value in /etc/hosts is often set to localhost by default. With this context in mind, here are all the different ways to manipulate a hostname on Linux. [ Free download: Advanced Linux commands cheat sheet. ] Change all three names with hostnamectl

  11. How to Change the Hostname in Linux

    The easiest way for most people to change the hostname on a Linux machine is to simply go into the system settings and click on About. Your computer's current hostname will appear as the first item on the list, under Device Name, or similar labels on other distros.

  12. How to Change HostName and IP-Address in CentOS / RedHat Linux

    This will display the "Network Configuration" GUI. Go to the "DNS" tab and change the hostname from here as shown below. III. Change the IP-Address. 1. Change ip-address Temporarily Using ifconfig. You can change the ip-address of the server using ifconfig command as we discussed earlier.

  13. How to Update Ubuntu IP and Hostname via Bash

    Using the hostnamectl command allows you to query or change the hostname. In this instance, the syntax is as shown below to change your current hostname to whatever you desire. sudo hostnamectl set-hostname "new-hostname"

  14. Change Linux Hostname and IP Address

    Done! Sometimes you might want to change the Linux hostname and IP address assigned to a Linux machine. In this article, we will be looking at...

  15. Linux Change Hostname

    Suse / OpenSuse: Change Hostname. Edit /etc/HOSTNAME, enter: Delete old name and add new one: server2.nixcraft.com. Save and close the file. Type the following command: hostname server2.nixcraft.com hostname. Now, proceed to step # 2.

  16. How to Change Hostname on Ubuntu 22.04

    Published on Dec 11, 2023 • 4 min read A hostname is a human-readable label assigned to a device and used to identify the device on a network. The hostname is set during the installation of the operating system or dynamically assigned to the virtual machine when it is created. Every machine on a network should have a unique hostname.

  17. How to Add an IP to a Hostname File

    2.1. Associating a Hostname With an IP Address To associate our IP address - 192.168.56.4 - with our hostname - baeldung - we can edit the hosts file: 127.0.0.1 localhost 192.168.56.4 linux 192.168.56.4 baeldung The hosts file allows duplicate entries, but it also allows us to put multiple host names on the same line:

  18. Ubuntu 22.04 change hostname

    NOTE Upon changing your hostname using the hostnamectl command no system reboot is required to apply the hostname change. Next, edit the /etc/hosts file to reflect the change by executing the command sudoedit /etc/hosts. For example, change: FROM: 127.0.0.1 localhost 127.0.1.1 ubuntu TO: 127.0.0.1 localhost 127.0.1.1 linuxconfig

  19. How to point a hostname to an ip address

    2. There is a misunderstanding about hosts file here. First of all, hosts file have precedence over DNS on most operating systems, you can define them on Linux/Unix operating systems and macOS in /etc/hosts and c:\windows\system32\drivers\etc\hosts on Windows. So when you add a record in your hosts file like:

  20. Get the Hostname From an IP Address in Linux

    2.1. Using the Command host The host command performs DNS lookups to convert names to IP addresses and vice versa. Let's try an example: $ host 8.8.8.8 8.8.8.8.in-addr.arpa domain name pointer dns.google. In this example, we've used the command host to get the hostname for the IP address 8.8.8.8 (The Google DNS). 2.2. Using the Command dig

  21. 5 ways to resolve hostname to ip address in linux

    This post shows you how to resolve hostname to ip address in several ways , like use command ping or getent or host ,etc, so that we can do it via bash script. Method 1: use ping command ping command is always our friend , it's very reliable and intuitive . ping -q -c1 -t1 yourhostname | tr -d ' ()' | awk '/^PING/ {print $3}'

  22. Change and sync hostname / ip address changes on debian linux

    For changing the hostname, i do the following actions. execute hostname <newname> set contents of /etc/hostname to <newname> replace <oldname> with <newname> in /etc/hosts. Issues. Both ip address and host name changes don't take effect. If I change the ip address and ping the hostname, it still resolves to the oldaddress

  23. How to get hostname from IP (Linux)?

    6 Answers Sorted by: 110 To find a hostname in your local network by IP address you can use nmblookup from the samba suite: nmblookup -A <ip> To find a hostname on the internet you could use the host program: host <ip> Or you can install nbtscan by running: sudo apt-get install nbtscan And use:

  24. How to Add Static Entries to the Hosts File on Linux

    To add a new entry, navigate to the end of the file and enter the IP address, followed by at least one space or tab, and then the hostname you wish to map to that IP. For example, to map the "example.com" domain to the IP address "10.0.1.69," the final version should look like this:

  25. How to change username on Linux

    In this tutorial, we saw how to change the username on a Linux system via GUI method. GUI tools only allow us to change the name of an account, without actually affecting the username. Therefore, to finish the job, we must use the usermod command in terminal. Keep in mind that changing the user's name, and not the account name, can usually ...

  26. How to find the IP address in CMD using hostname?

    Example:. nslookup google.com or nslookup h2smedia. Step 3: View the IP Address. Well, the ping command as you run it, the system will start pinging the remote system or device to send packets to the mentioned hostname and in return will display the IP address along with information on whether the remote or local system is in reach or not.

  27. Creating a k3s cluster with hostname instead of ip address

    My home network is set up with "lan" as the local domain. So if my Raspberry Pi has hostname "foo", I can connect to it via "foo.lan". I noticed that that cluster falls apart if the ip address for any of my nodes changes, so I've had to assign static ip's in my router's settings.