RPDevelopment
Posts: 6
Joined: Thu Apr 02, 2020 3:51 pm

Rename "hostname" for RP Client

Thu Apr 02, 2020 4:04 pm

I read somewhere that a specific hostname may be assigned to a IP address using /etc/hosts, but I only see the ability to change the raspberry hostname. For example, I have a hostname "unknownxxx" appearing in the Admin/Query log that is one of my devices, so, I like to rename it to the name of that device.

User avatar
DougieLawson
Posts: 38882
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Rename "hostname" for RP Client

Thu Apr 02, 2020 4:42 pm

The GUI preferences dialog has an option to do this. sudo raspi-config has an option.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

W. H. Heydt
Posts: 12430
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Rename "hostname" for RP Client

Thu Apr 02, 2020 4:44 pm

RPDevelopment wrote:
Thu Apr 02, 2020 4:04 pm
I read somewhere that a specific hostname may be assigned to a IP address using /etc/hosts, but I only see the ability to change the raspberry hostname. For example, I have a hostname "unknownxxx" appearing in the Admin/Query log that is one of my devices, so, I like to rename it to the name of that device.
Just how would you suggest going about renaming someone else's system? Changing the hostname is done for the system you are doing the changing on.

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Rename "hostname" for RP Client

Thu Apr 02, 2020 5:05 pm

W. H. Heydt wrote:
Thu Apr 02, 2020 4:44 pm
Just how would you suggest going about renaming someone else's system?
You can put anything you like in your own /etc/hosts.
For example you could put in this entry in

Code: Select all

8.8.8.8 pingy.pingy.thing
and you would get

Code: Select all

pi@raspberrypi:~ $ ping -c 1 pingy.pingy.thing
PING pingy.pingy.thing (8.8.8.8) 56(84) bytes of data.
64 bytes from pingy.pingy.thing (8.8.8.8): icmp_seq=1 ttl=54 time=18.1 ms

--- pingy.pingy.thing ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 18.067/18.067/18.067/0.000 ms
Unreadable squiggle

RPDevelopment
Posts: 6
Joined: Thu Apr 02, 2020 3:51 pm

Re: Rename "hostname" for RP Client

Fri Apr 03, 2020 6:36 pm

(1) I tried the raspi-config and it only provides changing the RP hostname.
(2) I don't understand how to use the editing of Hosts, as suggested.
(3) The hostname I'm attempting to change is a "unknownxxxx" ip hostname. It should be named "abcdevice", since I confirmed it's ip/mac address

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Rename "hostname" for RP Client

Fri Apr 03, 2020 6:57 pm

Well, then you put an entry in /etc/hosts on the Pi to say

Code: Select all

1.2.3.4 abcdevice
Replace 1.2.3.4 with the actual IP address of that host. Then whenever you try to access 'abcdevice' on that Pi, it will connect to the correct ip address.

/etc/hosts is a very simple local look-up table. It is only used by the system it is stored on*

*Except when you are running a DNSMasq DNS server on that system, where the entries in that file are propagated to other systems on the same network - but don't worry about that, it's a special case and shouldn't apply here.
Unreadable squiggle

RPDevelopment
Posts: 6
Joined: Thu Apr 02, 2020 3:51 pm

Re: Rename "hostname" for RP Client

Fri Apr 03, 2020 7:54 pm

This looks like the solution. As a novice, would you please write the entire code line. Is it sudo /etc/hosts?

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Rename "hostname" for RP Client

Fri Apr 03, 2020 8:23 pm

sudo nano /etc/hosts
Just add the new line at the end.

sudo means "run with administrator (root) privileges"
nano is a simple editor
/etc/hosts is the name of the file to edit. You need administrator powers to edit it.
Unreadable squiggle

RPDevelopment
Posts: 6
Joined: Thu Apr 02, 2020 3:51 pm

Re: Rename "hostname" for RP Client

Fri Apr 03, 2020 8:39 pm

OK. I'm stupid. "Sudo nano /etc/hosts" yields just the local host listing. Ending the command without the "/" yields a new directory request.

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5590
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Rename "hostname" for RP Client

Fri Apr 03, 2020 9:55 pm

RPDevelopment wrote:
Fri Apr 03, 2020 8:39 pm
OK. I'm stupid. "Sudo nano /etc/hosts" yields just the local host listing. Ending the command without the "/" yields a new directory request.

Code: Select all

sudo nano /etc/hosts
within a terminal should display something like this:
SudoNanoEtcHosts.png
SudoNanoEtcHosts.png (35.1 KiB) Viewed 316 times
The two "lines" at the bottom starting with

Code: Select all

^G Get Help
are nano's keyboard shortcuts (keys + control) and their brief help. It's a very basic, simple editor.
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Rename "hostname" for RP Client

Fri Apr 03, 2020 10:00 pm

RPDevelopment wrote:
Fri Apr 03, 2020 8:39 pm
OK. I'm stupid. "Sudo nano /etc/hosts" yields just the local host listing. Ending the command without the "/" yields a new directory request.
Yes. Add the line you need on the end of that file.
Unreadable squiggle

RPDevelopment
Posts: 6
Joined: Thu Apr 02, 2020 3:51 pm

Re: Rename "hostname" for RP Client

Sat Apr 04, 2020 1:33 am

OK. Entering sudo nano /etc/hosts does yield the local host as your insert. However, when you say "add a line", do you mean "sudo nano /etc/hosts/1.2.3.4 newipname" or "sudo nano etc/hosts/1.2.3.4 newipname", with spaces between the IP address and hostname, but no space after the hosts/? I assume that etc is a directory and hosts is a subdirectory

In my earlier progamming days the coding is like "Device name/directory/subdirectory" (c:/photos/family/dogs) then all files in the photos directory/dogs subdirectory would list the file names of all the dog photos. No spaces between directory/subdirectory entries. I assume that we're dealing with a similar format, with all IP addresses and corresponding hostnames are listed, like all IP addresses are listed in the hosts/clients directory. So I would assume sudo nano /etc/hosts IP Address (or HostName), with no space after the "hosts" directory entry.

Then, when finding the desired IP address in the "hosts" directory, I'd edit that IP record to change the current "unknown1212121" with "newipname". I assume the the image would be as you included in your insert, with the 1.2.3.4 IP and "unknown1212121" hostname.

I've tried several iterations, but obviously not correct, sometimes getting "etc" does not exist, or "hosts" does not exist. It's probably a space that's needed, but not entered, or a "/" that's needed, but not entered. I got the "sudo nano /etc/hosts" fine. It's the placement and whether to include spaces or "/" for "added line" that I'm missing. in order to locate the desired IP address for editing. I'm assuming it's a single line command.

Sorry for being such a pain.

User avatar
TracyLBaker
Posts: 41
Joined: Sat Feb 18, 2017 3:44 pm
Location: Phoenix, AZ

Re: Rename "hostname" for RP Client

Sat Apr 04, 2020 2:29 am

If your pi account allows it without sudo (my pi account doesn't need sudo):

Code: Select all

echo "1.2.3.4 name" >> /etc/hosts

If you're pi account requires sudo:

Code: Select all

echo "1.2.3.4 name" | sudo tee -a /etc/hosts

1.2.3.4 will be IP address of the other system

name will be what you want to call the other system -- this creates an alias for it (it does not change its hostname).

In the first example, >> is a necessity, in the second example, -a is: they both tell the command append to the file instead of overwriting it.

Neither will change an existing entry -- but that's OK. In this scenario, both aliases would work -- you can have (almost) as many as you want per IP.
RHCE, Red Hat Academy Professor - Estrella Mountain Community College
2 Pi 4B 4Gb media servers with 48Tb 5 Pi 3 Model B+s: 1 headless media/NFS/SMB server - Raspbian Jessie, 1 Kali Linux w/fruitywifi, 1 headless CentOS 7 webserver, 2 backups

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Rename "hostname" for RP Client

Sat Apr 04, 2020 6:02 am

As we have said many times, the command to start a simple editor to edit the file that contains your host/ip entries is

Code: Select all

sudo nano /etc/hosts
Typed exactly like that.

That will then show you the file as Trev posted.

You use the arrow keys to move down the screen to after the last line shown (the one that starts with 127.0.1.1).

You may need to press Enter to get a new blank line at the end.

Then you type in the IP address you want to use, then one or more spaces or a tab, then the hostname you want to give that IP address.

Then you press the "o" key while holding the Ctrl key to tell the editor to save the update. It will prompt you with the file name. Press Enter to accept the existing name.

Finally, press "x" while holding down Ctrl to close the editor.

It is that simple.
Unreadable squiggle

RPDevelopment
Posts: 6
Joined: Thu Apr 02, 2020 3:51 pm

Re: Rename "hostname" for RP Client

Sat Apr 04, 2020 5:46 pm

Got it! I thought that the adding a line related to extending the sudo command, not in the text editor. Duh! My bad.

I've saved the 8 lines appearing in the text editor after entering the relevant IP Address and proposed hostname change. Now I hope that the PIHole Admin Console shows that changed host name in the query page.

You've been very patient and helpful.

Many thanks.

Return to “General discussion”