如何使用结构编辑主机名文件 [英] how to edit hostname file using fabric

查看:90
本文介绍了如何使用结构编辑主机名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经更改了主机文件,所以如何更改hostname.my系统是ubuntu. 例如我的主机文件:

I have change my hosts file,so how to change hostname.my system is ubuntu. eg my hosts file:

192.168.0.100 host1.mydomain.com
192.168.0.101 host2.mydomain.com

我想将host1的/etc/hostname下的主机名文件命名为host1.mydomain.com,将host2的主机名文件命名为host2.mydomain.com

I wanna the hostname file under /etc/hostname of host1 to host1.mydomain.com,the hostname file of host2 to host2.mydomain.com

如何使用面料做到这一点? 我必须ssh每个主机并编辑主机名文件,fabric可以做到这一点吗?

how to do that using fabric? I have to ssh every host and edit the hostname file,does fabric can do this?

我不是要使用hostname命令,而是要编辑/etc/hostname文件. 我的意思是如何使用面料来做到这一点: 例如:

I didn't mean to use hostname command but to edit the /etc/hostname file. I mean how to use fabric to do that: such as:

def update_hostname():
  get("/etc/hosts","hosts")
  hosts_content = file("hosts")
  **hostname = ·get the hostname corespond to ip·**
  get("/etc/hostname","hostname")
  update `hostname file`
  put("hostname","/etc/hostname")

如何获得IP?因为结构会在每个主机上执行此工作,并且主机名与每个主机相对应.我需要知道作业在哪个主机上工作,然后取回IP,然后获取与IP对应的主机名,最后更新主机名文件.

how get the ip? because fabric do the job on every host, and the hostname is correspond to each host. I need to know the which host the job is working and then get the ip back,then get the hostname correspond the the ip,and final update the hostname file.

推荐答案

def hostname():

   '''
   function to change the hostname of the ubuntu server
   '''

   server_hostname = prompt ("The Hostname for the server is :")

   sed ("/etc/hostname", before='current hostname', after='%s' % (server_hostname), use_sudo=True,backup='')

   sudo ("init 6")

这将根据您的选择更改主机名.

This will change the hostname according to your choice.

这篇关于如何使用结构编辑主机名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆