0

After adding a hosting file using etc/hosts on Mac OS X machines manually we found out we needed to remove the current hosts and add a new one. I'm wondering if it's possible to add a hosting file for Mac OS X using Bash.

This is the current state. How do we change the final line or add one to it?

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1             localhost Computer_Name
255.255.255.0     broadcasthost
::1                        localhost
10.24.82.5           server_name
midori
  • 4,807
  • 5
  • 34
  • 62
MyloTut
  • 31
  • 1
  • 2
  • 2
    Please add sample input and your desired output for that sample input to your question. – Cyrus Oct 06 '15 at 02:53
  • Below you'll find an example of what we have. The final line in the Host Database is where we would look to make the change. Say we want to change the IP from 10.24.82.5 using a bash script. Would that work? – MyloTut Oct 06 '15 at 04:31
  • possible duplicate of http://stackoverflow.com/questions/28457543/sed-replace-ip-in-hosts-file-using-hostname-as-pattern – tripleee Oct 06 '15 at 05:17

2 Answers2

2

I guess you are asking how to edit the file from the command line, then

$ sed -i.OLD 's@10\.24\.82\.5 @8.8.8.8 @' /etc/hosts

The -i option allows sed to edit in-place. You may need sudo to have write permission to that file.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
1

You can simply use sed to change it, you just need to provide new ip address:

sed 's/^.*\(server_name\)/new_ip_address \1/' file
midori
  • 4,807
  • 5
  • 34
  • 62