Linux - change the hostname in the CLI - linux

I don't know how to search for this and that is why I asked for it (all my searches did not reveal any relevant information).
I have a Fedora 18 server that looks like this:
[root#dhcp-192-168-5-100 ~]#
I want to change that to:
[root#server1 ~]#
Currently, this machine is set to get IP through DHCP, but that is not the IP address of the machine and that is why I need to change the CLI to something more relevant.
Thank you

you can type "hostname HOSTNAME" where HOSTNAME is the new name you want. The next time you log in / connect via ssh, that's what you'll see.

Edit /etc/sysconfig/network and change/add HOSTNAME variable like so HOSTNAME=server1.domain.com After restart it should have server1.
Edit /etc/hosts and add server1 and server1.domain.com to 127.0.0.1 line, so it will look like: 127.0.0.1 localhost.localdomain localhost server1 server1.domain.com
More about network file you can read here:
https://docs.fedoraproject.org/en-US/Fedora/15/html/Deployment_Guide/ch-The_sysconfig_Directory.html#s2-sysconfig-network

Just found this and this is described pretty good.
[root#dhcp-192-168-5-100 ~]# hostname MYHOSTNAME
Change hostname in Feodora

Add "yournewhostname" into /etc/sysconfig/network in the HOSTNAME
field..
Add "yournewhostname" into /etc/hosts for 127.0.0.1
echo "yournewhostname" > /proc/sys/kernel/hostname or service hostname
IMPORTANT: logout and log back in.

Related

hostname -i vs hostname -I in linux

So I looked up on google about the difference between the output for hostname -i and hostname -I but couldn't get anything significant.
The output for hostname -i: 127.0.1.1
The output for hostname -I: 192.168.X.X.
All I could get was this:
-i: This option is used to get the IP(network) addresses. This option works only if the hostname is resolvable.
-I: This option is used to get all IP(network) addresses. The option doesn’t depend on the resolvability of the hostname.
It would be great if someone could elaborate on this for me.
From the information in your question I infer that your question is about linux hostname command. That's important as BSD flavor of that command (e.g. found in macOS system) has different argument set.
The man page for linux hostname command is actually very specific about the difference and you have posted already excerpt from it:
-i, --ip-address
Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid
using this option; use hostname --all-ip-addresses instead.
-I, --all-ip-addresses
Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces.
The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend
on name resolution. Do not make any assumptions about the order of the output.
The "address being resolved" (the -i argument) means that it is being resolved via DNS. You can get similar results by using some name resolution command as nslookup <yourhostname> or getent hosts <yourhostname>:
$ nslookup myhostname
Server: 127.0.0.1
Address: 127.0.0.1#53
Non-authoritative answer:
Name: myhostname
Address: X.X.X.X
The -I on the other hand iterates over all network interfaces of your host and sees how they are actually configured. That means it obtains the IP address via completely different method.
Assuming your hostname is set to myhost, hostname -i tries to resolve myhost. You probably have such a line in your /etc/hosts
127.0.0.1 myhost
This will resolve myhost as 127.0.0.1.
With hostname -I you get the addresses of your network interface(s), apparently without the loopback addresses. These addresses might be assigned by DHCP.
In your case with the output 192.168.X.X you only have one interface connected with only an IPv4 address.
My Ubuntu 19.10 system displays one IPv4 address and one IPv6 address when connected to WiFi only or two IPv4 and IPv6 addresses each when connected to both WiFi and Ethernet.

Can I resolve a MX record using hosts file?

To test mail server I need an MX record in DNS server, it always with a delay because DNS cache, I need to make it faster. Is there a way make an MX record locally like A record by etc/hosts file?
Thanks, this works.
echo "10.10.10.1 mail.example.com" | sudo tee --append /etc/hosts > /dev/null
echo "disable_dns_lookups = yes" | sudo tee --append /etc/postfix/main.cf > /dev/null
systemctl restart postfix
i'm managing to use postfix on a proxmox server to delivery the mail to a local mail server on a vm , with a different ip address from the dns mx record, i've found very useful this article,
http://www.readonlymaio.org/rom/2018/01/16/force-postfix-to-search-mx-records-in-etc-hosts-file/
he leds me to disable the dns lookup in postfix to force it to use the hosts A record !!
it works
in the hosts
X.X.X.X example.com
Edit the /etc/postfix/main.cf file and add this line:
disable_dns_lookups = yes
after, restart postfix

What is the /etc/hosts behavior with alias or append?

I want to discover the behaviour of the system when I define in the /etc/hosts two separe entry with the same ip:
192.168.100.1 host1
192.168.100.1 host2
instead of:
192.168.100.1 host1 host2
The behaviour when I try to access the host1 changes?
Thanks.
As man said you should not:
This manual page describes the format of the /etc/hosts file. This file is a simple text file that associates IP addresses with hostnames, one line per IP address. For each host a single line should be present with the following information:
https://unix.stackexchange.com/questions/102660/hosts-file-is-it-incorrect-to-have-the-same-ip-address-on-multiple-lines

assign command output to variable

I'm cloning lots of virtual machines, and I need to edit /etc/hostname and replace old hostname with a new one.
then I would have to run
hostname new-server-name
and log out of the ssd session and login again.
I'm trying to make my job easier by doing this
echo "new-server-name" > /etc/hostname
if there is a way to assign the value to /etc/hostname to host command? Something like
cat /etc/hostname | hostname
but it doesn't work.
You probably want this:
hostname $(cat /etc/hostname)
And don't forget to edit /etc/hosts to reflect the correct host name as well.

Change and sync hostname / ip address changes on debian linux

For changing static ip address, i set the contents of /etc/network/interfaces to
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet static
address <newaddress>
netmask <netmask>
gateway <gateway>
dns-nameservers <dns1> <dns2>
then do ifdown eth0 && ifup eth0
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
If I change the hostname and ping the new hostname, it cannot resolve to an ip
If I change the hostname and ping the old hostname, it is still available and resolved the current ip address.
I restart the machine and it still behaves the same way.
What am I missing?
Could the parent network play any role in this?
Also note that my machine thinks the hostname is updated after I change it.
I think this is not actually a problem and this behavior is explained by arp caching.
The ARP tool manipulates or displays the kernel's IPv4 network neighbour and this includes in some way your host as well.
The arp command has many switches you can work on as, for example, the following command:
arp -n
This command prints out the list of arp cache entries.
Unfortunately there is no command to flush it hence, you have 2 options.
Option 1: You wait for the cache to update (won't take long)
Option 2: You run the following very basic script and you see:
#!/bin/sh
for i in `awk -F ' ' '{ if ( $1 ~ /[0-9{1,3}].[0-9{1,3}].[0-9{1,3}].[0-9{1,3}]/ ) print $1 }' /proc/net/arp` ;
do
arp -d $i
done
The script is not mine, I have reported the only I usually run which was taken originally from this well done example:
http://www.lainoox.com/display-add-flush-arpcache-linux-arp/
I hope this helps in some way.
Changing /etc/hostname will have the affect of changing the hostname displayed on your terminal, welcome messages, in your logs and anywhere else the system uses hostname. These are all vanity changes.
Channging /etc/hosts will change your local host-name resolution (i.e. translating an ip address to a hostname and vise versa) Anything DNS does not resolv or anything you want to override locally. By default, the local files override network resolution. This is determined in /etc/nsswitch which says to use local files first then then dns for host resolution.
The important thing to keep in mind here is that these changes will not affect how othesr on a network would determine this host's ip address or name, which of course is handled through DNS or maybe their local /etc/hosts file(s).
yeah resolv.conf is for dns. as is which handles NAMES and anything that has to do with the internet. Such as ping. dns resolvc locally weather you have server or not thus the hostname / ect hosts which ALSO handle dns. your resolv.conf will update from your isp when you connect or you can change it by hand / set some thing more dynamic on a bridged interface

Resources