azure linux 修改内网ip,Use cloud-init to set hostname for a Linux VM - Azure Linux Virtual Machines | Az...

Use cloud-init to set hostname for a Linux VM in Azure

09/03/2020

2 minutes to read

In this article

This article shows you how to use cloud-init to configure a specific hostname on a virtual machine (VM) or virtual machine scale sets (VMSS) at provisioning time in Azure. These cloud-init scripts run on first boot once the resources have been provisioned by Azure. For more information about how cloud-init works natively in Azure and the supported Linux distros, see cloud-init overview

Set the hostname with cloud-init

By default, the hostname is the same as the VM name when you create a new virtual machine in Azure. To run a cloud-init script to change this default hostname when you create a VM in Azure with az vm create, specify the cloud-init file with the --custom-data switch.

To see upgrade process in action, create a file in your current shell named cloud_init_hostname.txt and paste the following configuration. For this example, create the file on your local machine. You can use any editor you wish. Enter sensible-editor cloud_init_hostname.txt to create the file and see a list of available editors. Choose #1 to use the nano editor. Make sure that the whole cloud-init file is copied correctly, especially the first line.

#cloud-config

hostname: myhostname

Before deploying this image, you need to create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources are deployed and managed. The following example creates a resource group named myResourceGroup in the chinaeast location.

az group create --name myResourceGroup --location chinaeast

Now, create a VM with az vm create and specify the cloud-init file with --custom-data cloud_init_hostname.txt as follows:

az vm create \

--resource-group myResourceGroup \

--name centos74 \

--image OpenLogic:CentOS:7-CI:latest \

--custom-data cloud_init_hostname.txt \

--generate-ssh-keys

Once created, the Azure CLI shows information about the VM. Use the publicIpAddress to SSH to your VM. Enter your own address as follows:

ssh

To see the VM name, use the hostname command as follows:

hostname

The VM should report the hostname as that value set in the cloud-init file, as shown in the following example output:

myhostname

Next steps

For additional cloud-init examples of configuration changes, see the following:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值