Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 19/20 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #16  
Old 9th March 2012, 07:13 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,548
linuxfedorafirefox
Re: Set hostname according to /etc/hosts FC16

Tho' I don't entirely agree w/ RandomUser's approach - the appoach I suggested is decidely ad hoc, and not the way I would roll out 99 systems.

The first thing to realize is that a hostname (not network name) is highly desirable for every system - as ID even when not connected to a network. The schema above can only workk when the interface "em1" or whatever comes up and configures successfully. Even when that is not the case we still want a hostname.

So as a matter of course I would argue that setting
/etc/sysconfig/network:HOSTNAME=thissystemname
is a wise course of action, This and not the network name can be used as a unique system ID.

The next issue - if I had 99 systems on a subnet I'd wager it's worth the cost to install a DHCP/DNS server that can manage all of the IP assignments, rather than using an /etc/hosts file for this. There are tricks for assigning the 'DHCP_HOSTNAME in the ifcfg files, to eliminate that issue.

There may be good reasons why a DNS/DHCP server (like a $60 router running DD-WRT) won't wok in this specific case, but 99 systems seems like a school or business where this would make sense.

---------

Setting the hostname fronm a network dependency can work, but it's not ideal. It's the cart before the horse.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #17  
Old 9th March 2012, 07:19 PM
ablen Offline
Registered User
 
Join Date: Jan 2012
Location: USA
Posts: 51
windows_7ie
Re: Set hostname according to /etc/hosts FC16

Hello all,

PabloTwo, I tried out your revised code (with the "inet 188") and it works like a charm. Thank you. A small number of my machines do indeed have different architectures so this will save me having to re-edit rc.local on those machines. Thank you!

randomuser, thanks for your notes. I tried hostname $HOSTNAME and it will set the hostname (and my prompt) according to the current value of hostname (which starts out as localhost on my machine). But that's different than the name specified in my /etc/hosts file. Too bad that doesn't work because it would be a wonderfully elegant solution if so!

Regarding your other ideas given, thank you for those as well. I may keep these in mind for a future time. For now, the solution brainstormed by steveea and PabloTwo is perfect for my needs.

Once we can figure out how to get rc.local running on bootup, that is. Any more ideas on that, anyone? PabloTwo, you tried this on your machine I think. You said you have the file here: /etc/rc.d/rc.local, right? We both have rc-local.service enabled. Do you have FC16 as I do (if that matters)?

Last edited by ablen; 9th March 2012 at 07:23 PM.
Reply With Quote
  #18  
Old 9th March 2012, 07:21 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,548
linuxfedorafirefox
Re: Set hostname according to /etc/hosts FC16

Quote:
Ok, here's my real recommendation. This will be a lot more work off the bat, but you'll thank yourself later. Linux system administrators are lazy by nature, and look for the least amount of work in the long term.

1. Run a DHCP server. Configure a host directive for every host, probably in groups.
2. Run a nameserver. Configure a zone for your network, and use dyndns to update named with DHCP.
3. Learn how to use cobbler. You can automate mass installs over the network, assigning roles, packages, and configurations via a kickstart file specific to each machine.
4. Forget about the host file, all of that information is now centralized.
1. Agreed.
2. a DNS server yes, but 'dyndns' has nothing to do with it. Maybe you mean 'dnsmasq' or else the 'bind' service. dnsmasq is a great simple solution for a few hundred nodes, and manages dns, dns forwarding and dhcp oin one package. This is what runs inside a DD-WRT router. The advantage of the DD-WRT router approach is that it runs 24x7 on 5 watts and you don't have to dedicate a server to it. If you have DHCP & DNS services elsewhere - use those.
3. I've never used cobbler, but seeems like a rgeat approach.
4. YES ! I never touch /etc/hosts anymore, even at home. It's a losing way to configure networks.

So maybe we mostly agree.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #19  
Old 9th March 2012, 07:30 PM
ablen Offline
Registered User
 
Join Date: Jan 2012
Location: USA
Posts: 51
windows_7ie
Re: Set hostname according to /etc/hosts FC16

I've been given a lot to think about. I think there's a lot of wisdom here. For today, though, any more ideas re: why my rc.local is not (or seems not) to be executing on bootup?

I wanted to thank all of you again for being so helpful. I really appreciate everyone's efforts if I've not mentioned that yet.
Reply With Quote
  #20  
Old 9th March 2012, 07:34 PM
randomuser Offline
Registered User
 
Join Date: Mar 2012
Location: Montana, US
Posts: 83
Yes, I mean dyndns, as defined in the dhcpd manpages with the ddns-update-style and related directives. When I have more free time later today, I'll throw together a guide on doing this for the wiki and reference it here.
Reply With Quote
  #21  
Old 9th March 2012, 07:55 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,548
linuxfedorafirefox
Re: Set hostname according to /etc/hosts FC16

Quote:
Originally Posted by randomuser View Post
Yes, I mean dyndns, as defined in the dhcpd manpages with the ddns-update-style and related directives. When I have more free time later today, I'll throw together a guide on doing this for the wiki and reference it here.
You mean DDNS aka "dynamic DNS". dyndns means dyndns.org to me and is a service allowing home users with dynamically assigned public IPs to refer to their homes by name.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #22  
Old 9th March 2012, 07:57 PM
ablen Offline
Registered User
 
Join Date: Jan 2012
Location: USA
Posts: 51
windows_7ie
Re: Set hostname according to /etc/hosts FC16

Okay, so I have been working through some ideas as presented on this site for getting rc.local to run at startup:

https://bugzilla.redhat.com/show_bug.cgi?id=734268

When I run one of the suggested commands, I get the following worrisome result:

[root@localhost ~]# systemctl list-unit-files | grep rc-local.service
rc-local.service disabled
Reply With Quote
  #23  
Old 9th March 2012, 08:07 PM
randomuser Offline
Registered User
 
Join Date: Mar 2012
Location: Montana, US
Posts: 83
Quote:
randomuser, thanks for your notes. I tried hostname $HOSTNAME and it will set the hostname (and my prompt) according to the current value of hostname (which starts out as localhost on my machine). But that's different than the name specified in my /etc/hosts file. Too bad that doesn't work...
Let me clarify myself, I missed this statement.

$HOSTNAME is an environment variable. When prepended with a $, bash will substitute the value of the variable, in this case the system's hostname. You were intended to personally substitute your desired hostname into the command, typing "hostname linux99webserver" instead of typing "Control-V."

I understand my suggestions seem daunting, and it is much easier to cut and paste the script created for you. It's really up to you how to handle your situation, but I want to give you a word of caution.

If you don't take the time to learn some basics, and do things properly the first time, your life will be infinitely more difficult. There is no level of support this or any forum can provide that can substitute the actual knowledge required for what it sounds like you are trying to do. Save yourself the trouble of unlearning bad habits and manually configuring dozens of machines, and take the time to read and learn about the tools designed for these tasks.
Reply With Quote
  #24  
Old 9th March 2012, 08:19 PM
ablen Offline
Registered User
 
Join Date: Jan 2012
Location: USA
Posts: 51
windows_7ie
Re: Set hostname according to /etc/hosts FC16

Hello randomuser,

Quote:
Originally Posted by randomuser View Post
$HOSTNAME is an environment variable. When prepended with a $, bash will substitute the value of the variable, in this case the system's hostname. You were intended to personally substitute your desired hostname into the command, typing "hostname linux99webserver" instead of typing "Control-V."
Ah, I see. To do this I would need to log into all the machines one at a time (which I am trying to avoid).

Regarding your mention that I am currently manually configuring dozens of machines - my setup does not require that. Once I have a single image in place I can roll it out to all 99 of my machines in a controlled fashion.

Please understand that I respect the idea you've presented. I'll be reading the guide that you are putting together with great interest since setting up a DNS server should be worthwhile I think.

But I also believe that there must be a way to run a line of code upon bootup. This is one of the basics I would like to learn from you. Could you please provide some guidance on this as well if the solution is known to you? In short, is there a way to run a script at startup under FC16 any longer? I assume rc.local is the way to go - at least according to all I've read.

Last edited by ablen; 9th March 2012 at 08:37 PM.
Reply With Quote
  #25  
Old 9th March 2012, 08:37 PM
randomuser Offline
Registered User
 
Join Date: Mar 2012
Location: Montana, US
Posts: 83
Hey ablen,

I'll be able to give you more detailed guidance, if needed, later on. In the meantime, I suggest you start reading about cobbler, kickstart, and fedora in general at docs.fedoraproject.org.

rc.local generally is the place for startup scripts. To avoid confusion, lets talk about DNS here, and you should start a new thread for this seperate issue. Please also describe the task you are trying to accomplish, the desired function of the script, and perhaps the script itself. As this thread shows, its better to share WHAT you want to do than just ask how to do it. There might be a better way.
Reply With Quote
  #26  
Old 9th March 2012, 09:01 PM
ablen Offline
Registered User
 
Join Date: Jan 2012
Location: USA
Posts: 51
windows_7ie
Re: Set hostname according to /etc/hosts FC16

Okay, before we go much farther, here are the specifics re: what I want to accomplish. My goal is to set my command prompt so that it matches the information in /etc/hosts (I want to be able to log into a machine and immediately know which one I am on by looking at the command prompt). Currently I use /etc/hosts so that an IP address can be resolved into a name, especially while using SSH. This works great so I'd like to keep that as is.

Now, the hostname on each machine is set to localhost during the initial setup of the OS. I *could* set it more explicitly if desired during the initial setup (linux23 for example) but that would require me to set up each individual machine one at a time. So I want to avoid that. Instead I want to set that name afterwards - automatically. All the information to do this is in the /etc/hosts file. Thus my request (the title of the thread): "Set hostname according to /etc/hosts FC16."

To be honest, I originally thought that if I set these values in /etc/hosts that it WOULD automatically set all the hostnames for me. The reason I believed this is because that's how it functions under some prior versions of FC.

So to close, I now have a way to do this thanks to the help received from stevea and PabloTwo (thank you!).

If I can find the reason why rc.local is not running then that solves the problem.

I hope I've explained sufficiently what I am after. As simple a solution as possible to what I perceive to be a simple need.

Last edited by ablen; 9th March 2012 at 10:21 PM.
Reply With Quote
  #27  
Old 9th March 2012, 09:42 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,658
linuxfirefox
Re: Set hostname according to /etc/hosts FC16

rc.local likely is running... just not when you expect it to be.

All of the solutions you have been shown assume that the network has been initialized.... and that is not necessarily the case.

What MAY work is enabling NetworkManager-wait-online.service (assuming you are using NetworkManager...)

If you aren't using network manager.... I don't know.

You would be better off by creating a single file containing: hostname, MAC address pairs for all your systems. You can always get the system MAC address from an "ifconfig -a", then try to match that against the file. The line that matches also has your hostname.

This way you can set the host name without depending on anything that must be initialized first.

Systemd sucks at handling things like rc.local correctly.

If you are willing to skip the use of the network identification, you can also check out the use of dmidecode -
In the output is a section on system information - there should be a motherboard serial number present that you can extract for use as a key to lookup an associated host name. That should not change unless you also change the motherboard. (same problem when using MAC addresses - they change if you replace the interface). The problem with using IP numbers is that you still have to get the name from somewhere - such as DHCP... so even then, the IP number could change. It would still be unique, but then you could just use the host name assigned by DHCP in the first place.

Last edited by jpollard; 9th March 2012 at 10:15 PM. Reason: typo
Reply With Quote
  #28  
Old 9th March 2012, 10:04 PM
PabloTwo Offline
"Registered User" T-Shirt Winner
 
Join Date: Mar 2007
Location: Seville, FL
Posts: 5,852
linuxchrome
Re: Set hostname according to /etc/hosts FC16

I have one command in my /etc/rc.d/rc.local file and it gets executed on every boot, no problem. The one extra step I took, but don't think is really necessary, was to create a symlink in /etc/ to /etc/rc.d/rc.local. Granted, this one is not tangled up with any "the network isn't up yet" timing issues.
Code:
BASH:~/-> cat /etc/rc.d/rc.local
#!/bin/bash
# Add commands to run at system startup

/sbin/hdparm -S 36 /dev/sdb /dev/sdc
Code:
BASH:~/-> hddtemp SATA:/dev/sdb SATA:/dev/sdc
/dev/sdb: ST3160812AS: drive is sleeping
/dev/sdc: WDC WD1001FALS-00U9B0: drive is sleeping


---------- Post added at 05:04 PM ---------- Previous post was at 04:50 PM ----------

jpollards MAC address scenario has a lot of merit.
Code:
BASH:~/-> MACADDR=$(ifconfig | grep HWaddr | awk '{print $5}'); echo $MACADDR
00:25:22:A2:52:BA

Last edited by PabloTwo; 9th March 2012 at 09:57 PM.
Reply With Quote
  #29  
Old 9th March 2012, 10:42 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,658
linuxfirefox
Re: Set hostname according to /etc/hosts FC16

You, fortunately, are not doing anything that needs to be initialized first... rc.local runs, but at a random time after boot. Anything that need prior initialization is not necessarily ready when it runs.

BTW, I usually do it the simple (at least to me) way:
Code:
MACADDR=`ifconfig | grep HWaddr | awk '{print $5}'`
Though either can give you multiple MAC addresses if you have multiple interfaces. One can always enter all the addresses in the table, or just check all the addresses until you find a match... (I have 5, 2 physical networks, 3 virtual for use with VMs)

Makes for a relative simple (assuming file has "MACADDR name" lines in it):
Code:
for m in ${MACADDR}; do
    name=`awk  "/${m}/ {print $2}" file`
    if [ name != "" ]; then
        hostname ${name}
        exit 0
    fi
done
echo "hostname not found"
exit 1

Last edited by jpollard; 9th March 2012 at 10:54 PM.
Reply With Quote
  #30  
Old 9th March 2012, 11:57 PM
PabloTwo Offline
"Registered User" T-Shirt Winner
 
Join Date: Mar 2007
Location: Seville, FL
Posts: 5,852
linuxchrome
Re: Set hostname according to /etc/hosts FC16

I got to thinking and realized the MAC address might not always be in awks $5 field. I'd amend my code to use $NF instead of $5, since I think the MAC address would always be the last field.
Reply With Quote
Reply

Tags
fc16, hostname, or etc or hosts, set

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
gethostbyname>hostname problem: check /etc/hosts or /etc/resolv.conf hamidian Using Fedora 1 30th May 2010 11:25 PM
gethostbyname>hostname problem: check /etc/hosts or /etc/resolv.conf hamidian Servers & Networking 0 30th May 2010 06:18 AM
HOSTNAME & hosts P4rD0nM3 Using Fedora 0 15th January 2008 09:20 AM
hosts puzzle ->host hostname -> host hostname not found alphonsebrown Servers & Networking 2 30th March 2005 09:26 AM
Should my hostname be in the 'hosts' file? Fusion Servers & Networking 3 24th February 2005 03:53 PM


Current GMT-time: 18:27 (Thursday, 17-04-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat