LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2021, 04:20 PM   #1
Ninguém
LQ Newbie
 
Registered: Feb 2011
Location: localhost
Distribution: GNU/Linux
Posts: 24
Blog Entries: 8

Rep: Reputation: Disabled
keep /etc/hosts up to date (dhcp)


I notice /etc/hosts has a line with current IP address and hostname and possible domain just after localhost.
Code:
127.0.0.1 localhost localhost.localdomain
192.168.1.22 myhost myhost.example.com
How do you keep it correct when getting its IP address from DHCP (since it changes)?
I'm using debian, by the way.
 
Old 02-25-2021, 05:11 PM   #2
zrong
Member
 
Registered: Feb 2021
Location: PT
Distribution: Debian, Slackware32-Current
Posts: 31

Rep: Reputation: 3
Did you change it and why?

Is myhost your machine name? check it with hostnamectl

Edit it, delete all and add:

127.0.0.1 localhost
127.0.1.1 myhost.lan myhost

If you using IPV6 settings on DHCP you also should have this:

::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters


You don't need to change your ip there only to test dns for locally purposes, and it's not recommended.

In other words you will have another ip via dhcp working regardless of that file and you don't need to restart services.

Last edited by zrong; 02-25-2021 at 05:16 PM.
 
Old 02-25-2021, 08:10 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,984

Rep: Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626
The DHCP server (modem/router) should have a way to make that address permanent or a never ending lease.

Otherwise just set it static.
 
1 members found this post helpful.
Old 02-25-2021, 10:25 PM   #4
zrong
Member
 
Registered: Feb 2021
Location: PT
Distribution: Debian, Slackware32-Current
Posts: 31

Rep: Reputation: 3
Sorry it seems I misunderstood the question but anyway that's not the way to make it permanent like was said is via your router or /etc/network/interfaces configuration or your network manager graphically.
 
Old 02-26-2021, 02:44 AM   #5
Ninguém
LQ Newbie
 
Registered: Feb 2011
Location: localhost
Distribution: GNU/Linux
Posts: 24

Original Poster
Blog Entries: 8

Rep: Reputation: Disabled
Quote:
Originally Posted by jefro View Post
The DHCP server (modem/router) should have a way to make that address permanent or a never ending lease.
Otherwise just set it static.
I don't necessarily have control over DHCP servers everywhere I connect my laptop to...

Quote:
Originally Posted by zrong View Post
Did you change it and why?

Is myhost your machine name? check it with hostnamectl
Yes, myhost is the hostname.
example.com is my domainname.

Quote:
Originally Posted by zrong View Post
Edit it, delete all and add:

127.0.0.1 localhost
127.0.1.1 myhost.lan myhost

If you using IPV6 settings on DHCP you also should have this:

::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters


You don't need to change your ip there only to test dns for locally purposes, and it's not recommended.

In other words you will have another ip via dhcp working regardless of that file and you don't need to restart services.
I still lag behind regarding IPv6 configurations! I'll have to look into that...

I thought of just deleting the line with the hostname (with the transient IP address) leaving just the one with "localhost", but I am aiming for correctness - I'd like to have tools like hostname and others report correct hostname, domainname, FQDN...

Next question will be the same but for /etc/networks. :-)
 
Old 02-27-2021, 12:18 AM   #6
zrong
Member
 
Registered: Feb 2021
Location: PT
Distribution: Debian, Slackware32-Current
Posts: 31

Rep: Reputation: 3
You want to use debian as server? Or just set static ip in every lan you connect? (this later not every networks support)
 
Old 02-27-2021, 03:32 AM   #7
Ninguém
LQ Newbie
 
Registered: Feb 2011
Location: localhost
Distribution: GNU/Linux
Posts: 24

Original Poster
Blog Entries: 8

Rep: Reputation: Disabled
Quote:
Originally Posted by zrong View Post
You want to use debian as server? Or just set static ip in every lan you connect? (this later not every networks support)
I was thinking more of laptops with DHCP.
They get carried around between several networks, some controlled by me, some not so. Some are friendly some are not...
I'd like to have the current (transient, correct) domain name appear on the login screen, tools like route report the correct networks...

Last edited by Ninguém; 02-27-2021 at 03:48 AM.
 
Old 02-28-2021, 02:26 PM   #8
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Having the local domain name appear on the login screen would be (almost) a magic trick since the laptop would have no communication with that network (not even the AP) until powered on and would have nothing from dns on the network until already logged in and authenticated. The connection to the AP does not AFAIK do any DNS handshaking.
 
Old 03-01-2021, 08:39 PM   #9
zrong
Member
 
Registered: Feb 2021
Location: PT
Distribution: Debian, Slackware32-Current
Posts: 31

Rep: Reputation: 3
You need some sort of script to do that. I have never done it before, even with radius, when you login that is.
Otherwise you can add a conky file with that info or add it to conky.

FQDN you don't need it since you're not a server. But should be something like this:

hostnamectl set-hostname ninguem

You can edit hosts, and comment line 127.0.1.1 - it's debian related and no need for it.

127.0.0.1 ninguem.myhostname.org ninguem

hostaname
ninguem
hostname -f
ninguem.myhostname.org
dnsdomainname
myhostname.org

The debian wiki page about setting IP is very complete: https://wiki.debian.org/NetworkConfiguration

you can set the interfaces depending on what are you using as network manager in that wiki you have explanation

ip addr show

a simple static for interface wlp2s0 should be:

allow-hotplug wlp2s0
iface wlp2s0 inet static
#wpa-ssid xxxxx
#wpa-psk xxxxx
address 192.168.1.66
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 208.67.222.222 208.67.220.220


trouble with ipv6
- systemd
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
sysctl -p

-openrc
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/openrc/sysctl.d/sysctl.conf

-runit
edit file /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1"
GRUB_CMDLINE_LINUX="ipv6.disable=1"

and
update-grub

Last edited by zrong; 03-01-2021 at 08:49 PM.
 
Old 03-02-2021, 02:50 AM   #10
Ninguém
LQ Newbie
 
Registered: Feb 2011
Location: localhost
Distribution: GNU/Linux
Posts: 24

Original Poster
Blog Entries: 8

Rep: Reputation: Disabled
Quote:
Originally Posted by zrong View Post
You need some sort of script to do that. I have never done it before, even with radius, when you login that is.
Otherwise you can add a conky file with that info or add it to conky.
You might be right. The solution I think will have to be creating a script for post-dhcp. A script in /etc/dhcp/dhclient-exit-hooks.d/, I guess.
I'm not sure what you mean with conky. Those applets floating on the desktop? Nahhh! :-)
Quote:
Originally Posted by zrong View Post
FQDN you don't need it since you're not a server.
Why do you say this? Shouldn't all computers have its FQDN correctly set?
Quote:
Originally Posted by zrong View Post
You can edit hosts, and comment line 127.0.1.1 - it's debian related and no need for it.
Hum!...
I don't have that line. But what is it for, really?
Quote:
Originally Posted by zrong View Post
The debian wiki page about setting IP is very complete: https://wiki.debian.org/NetworkConfiguration
Thank you. I'll have a look into it. Again...

This quest aims for correctness, not a practical necessity I have right now. I'm being pedantic, I guess...
But picture this situation: You BYOD to a place to start working there (It could be a friend you want to help debugging network issues at home).
You connect to a network, but you want which network to be immediately apparent so you don't hack his neighbor network by mistake.
Makes sense?
 
Old 03-02-2021, 03:18 PM   #11
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
I have always understood that you should NEVER remove the
Code:
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
part in the /etc/hosts file since a lot of what happens locally depends on that.

I have understood that additional hostnames can be inserted with those same IPs, but that those should not be removed.

Is that information no longer valid?
 
Old 03-02-2021, 03:53 PM   #12
Ninguém
LQ Newbie
 
Registered: Feb 2011
Location: localhost
Distribution: GNU/Linux
Posts: 24

Original Poster
Blog Entries: 8

Rep: Reputation: Disabled
Quote:
Originally Posted by computersavvy View Post
I have always understood that you should NEVER remove the
Code:
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
We where talking about 127.0.1.1 , not 127.0.0.1.
Notice the third number.
I certainly still have 127.0.0.1.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Keep original mod date of a file and 'date' it back to same file? SilversleevesX Programming 4 07-16-2010 11:12 AM
Verify /etc/hosts usage and round robin in /etc/hosts MikeyCarter Linux - Software 1 06-24-2008 12:20 PM
/etc/hosts and hosts.deny question ilan1 Linux - Networking 4 03-04-2006 05:28 PM
/etc/hosts, /etc/resolve.conf and /etc/host.conf config probs below_average Linux - Networking 1 12-08-2004 10:07 PM
does the /etc/hosts file work same as the M$ hosts file? Lleb_KCir Linux - Software 6 04-03-2004 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration