Can we set easy-to-remember host names for EC2 instances?

I am running several standard instances of Fedora on EC2. I feel that the public hostnames of instances assigned by Amazon are too weird and hard to remember. I would like to change them to something short (e.g. red / blue / green / etc.).

Is there a flip side to this? And how to configure it so that it persists after a reboot?

Thank.

+46
amazon-ec2 hostname
Mar 02 '09 at 18:02
source share
11 answers

Before you start, try running hostname and hostname --fqdn and pay attention to the answers.

You can edit /etc/hostname and set the host name that will be inserted after reboot. You can force the hostname -F /etc/hostname be reloaded using hostname -F /etc/hostname to read this value in the hostname. The bash request will change after logging out and logging in.

warning / note :
Yes, it’s good if the host name in the bash prompt is set to something more useful than ip-123-123-123-123 , but I decided to leave mine (at least for now), because it seems that many things really count on the hostname on ec2 instances being set in a standard way. After editing /etc/hostname and changing the hostname on webserver many services seem to fail because the hostname will not be resolved and apache will not start. Then I edited /etc/hosts and added to

 127.0.0.1 webserver 

like the second line. Apache then started, but complained that it could not find the fully qualified domain name. I confirmed that running hostname --fqdn no longer works.

Next, I consulted with man hostname and found out that although you can set hostname , it seems that the FQDN is what is returned using DNS lookups.

FQDN

You cannot change the fully qualified domain name (as returned by the hostname --fqdn) or the DNS domain name (as returned by dnsdomainname) with this command. The fully qualified domain name of the system is the name that the resolver (3) returns for the host name.

Technically: FQDN is the name getaddrinfo (3) returns the host name returned by gethostname (2). The DNS domain is the part after the first dot.

Therefore it depends on the configuration (usually in /etc/host.conf) how you can change it. Usually (if the hosts file is parsed before DNS or NIS), you can change it in / etc / hosts.

I think that it would be possible to install the system / fool system in FQDN, something like ip-123-123-123-123.ec2.internal , although the host name is webserver , but at the moment it began to seem more unpleasant than it cost , and for me, a more pleasant bash hint might cause software and configuration problems in the future, and so I decided to give up.

I also found out that many instances of Amazon ec2 use something called cloud-init :

cloud-init is a Ubuntu package that handles the early initialization of a cloud instance. It is installed in Ubuntu cloud images, as well as in official Ubuntu images available on EC2.

Some of the settings that he configures are:

  • default locale setting
  • host name setting
  • generate ssh private keys
  • adding ssh keys to user .ssh / authorized_keys so that they can log in
  • setting ephemeral mount points.

cloud-init behavior can be customized using custom data. User data can be provided by the user at the time the instance is started. This is done using the -user-data or -user-data-file argument to ec2-run instances

I also found this , which talks about how the hostname is configured using cloud-init:

On EBS instances, shutdown and subsequent start end with a different IP address.

In the case when the user has not changed / etc / hostname from its original value (seeded with metadata "local-hostname"), cloud-init will again set the host name and update / etc / hostname.

In the event that the user changes the name / etc / hostname, he will remain user-managed.

Also, if /etc/cloud/cloud.cfg contains the value "preserve_hostname" set to True, then the name / etc / hostname will never be affected.

An interesting conclusion is that if you do not change the host name, the cloud-init package will keep it relevant for you.

If someone has a workaround or can solve some of the problems mentioned, and help assure that nothing breaks on the ec2 instances due to a change in the host name, I would be glad to hear it.

+82
Oct 15
source share

Another way is to simply edit ~/.bashrc and prepend PS1 with a machine alias.

Edit: perhaps more correctly throughout the machine, for example. on AWS Linux AMI (example) (paste this into the console or add a custom .sh installation):

 cat << EOF | sudo tee /etc/profile.d/ps1.sh if [ "$PS1" ]; then PS1="[\u@myinst1:\l \t \! \W]\\$ " fi EOF 
+13
Nov 20
source share

Change /etc/sysconfig/network as root.

Replace

 HOSTNAME=localhost.localdomain 

from

 HOSTNAME=hostname.DOMAIN_NAME 

Then reboot or run /etc/init.d/network restart Then the server should report its name as a fully qualified domain name.

+12
Feb 28 '11 at 8:49
source share

From this site :

Change host name on a running system

On any Linux system, you can change the host name using the hostname command (surprised?) ... Here are some quick ways to use the command line host name:

 $> hostname 

without any parameters, it will display the current hostname of the system.

 $> hostname --fqd 

it will display the fully qualified domain name (or FQDN) of the system.

 $> hostname NEW_NAME 

sets the hostname of the system to NEW_NAME .

You can also edit /etc/hostname (at least in Ubuntu).

To ensure that it remains after rebooting in AWS, either add the command to /etc/rc.local so that it starts when the machine starts.

There is also a way to dynamically set the host name via USER_DATA :

 USER_DATA='/usr/bin/curl -s http://169.254.169.254/latest/user-data' HOSTNAME='echo $USER_DATA' IPV4='/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4' hostname $HOSTNAME echo $HOSTNAME > /etc/hostname 
+6
May 22 '12 at 8:56
source share

To change the system hostname to a public DNS name

Follow this procedure if you already have a public DNS name.

  1. Open the /etc/sysconfig/network configuration file in your favorite text editor and change the HOSTNAME entry to reflect the fully qualified domain name (for example, webserver.mydomain.com).

     HOSTNAME=webserver.mydomain.com 
  2. Reload the instance to select a new host name.

     [ec2-user ~]$ sudo reboot 
  3. Log in to your instance and verify that the hostname is updated. Your invitation should indicate the new host name (before the first "."), And the hostname command must specify the fully qualified domain name.

     [ec2-user@webserver ~]$ hostname webserver.mydomain.com 

To change the system host name without a public DNS name

  1. Open the configuration file /etc/sysconfig/network in your favorite text editor and change the HOSTNAME entry to reflect the desired system host name (for example, a web server).

     HOSTNAME=webserver.localdomain 
  2. Open the /etc/hosts in your favorite text editor and add an entry starting with 127.0.1.1 (on DHCP systems) or an eth0 address (on systems with a static IP) to match the example below, substituting your own hostname. (127.0.0.1 should be left as the localhost string.)

     127.0.0.1 localhost localhost.localdomain 127.0.1.1 webserver.example.com webserver 
  3. Reload the instance to select a new host name.

     [ec2-user ~]$ sudo reboot 
  4. Log in to your instance and verify that the hostname is updated. Your invitation should indicate the new host name (before the first "."), And the hostname command must specify the fully qualified domain name.

     [ec2-user@webserver ~]$ hostname webserver.localdomain 

Note. You can also change the shell prompt without affecting the host name. Refer to this AWS documentation .

+1
Apr 01 '14 at 2:59
source share

Of course, you can do this if you have your own domain (specify CNAME to indicate the hostname of Amazon). Otherwise, you are pretty much stuck with the one they give you (or Elastic IP if you install one of them).

0
Mar 02 '09 at 18:41
source share

The solution / etc / rc.local worked for me for the primary host name, but does not give me the fully qualified domain name.

0
Nov 12 2018-10-12T00:
source share

In my Linux AMI (snapshot of another instance). None of the above formulas worked. Then I just changed the HOSTNAME field in the file: /etc/init.d/modifyhostname and performed a normal reboot.

0
Jan 07 '15 at 13:09 on
source share

You will need to do a few things to set the host name:

  1. hostname newname - sets the host name, but is mutable
  2. edit / etc / hostname - sets the host name for the next reboot
  3. edit / etc / hosts - so that sudo doesn't complain

I put them together in a script and downloaded as a histogram: https://gist.github.com/mnebuerquo/5443532036af8b48995547e2817dba85

0
Mar 17 '18 at 22:26
source share

If you do not want to contact your DNS provider, you can use aliases for your instances. I was very surprised that Amazon did not provide a way to give an alias for the EC2 instance when they came out with the AWS Management Console , but at the same time you can use the free RightScale if you want to use the alias function - much easier than remembering those instances Amazon instances.

-one
Mar 02 '09 at 19:15
source share
 sudo hostname *yourdesiredhostnamehere* sudo /etc/init.d/networking restart 

then the host name will be changed. All other services, such as apache and postfix, are running on my server. Ubuntu 12.04 LTS Server

-one
Mar 29 '13 at 17:26
source share



All Articles