How to set an IP alias on a Linux machine

Source: Internet
Author: User
Tags ip number
Article Title: how to set an IP alias on a Linux machine. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Command:
  
* Load the IP Alias module first (skip this step if you compile it into the core ):
  
/Sbin/insmod/lib/modules/'uname-r'/ipv4/ip_alias.o
  
* Create a loopback device and all IP addresses starting with the primary IP number on the eth0 interface.
Number:
  
/Sbin/ifconfig lo 127.0.0.1
/Sbin/ifconfig eth0 up
/Sbin/ifconfig eth0 172.16.3.1
/Sbin/ifconfig eth0: 0 172.16.3.10
/Sbin/ifconfig eth0: 1 172.16.3.100
  
172.16.3.1 is the primary IP number, while. 10 and. 100 are aliases. The strange thing is that eth0: x,
X = 0, 1, 2,... n in each different IP number. this is not required for the primary IP number.
  
* The third step is to create a delivery path. first, deliver the device to the rewinding device, and then the regional network. The last step is different.
IP number and default delivery path:
  
/Sbin/route add-net 127.0.0.0
/Sbin/route add-net 172.16.3.0 dev eth0
/Sbin/route add-host 172.16.3.1 dev eth0
/Sbin/route add-host 172.16.3.10 dev eth0: 0
/Sbin/route add-host 172.16.3.100 dev eth0: 1
/Sbin/route add default gw 172.16.3.200
  
That's it.
  
In the preceding example, we use the private IP number defined in RFC 1918.
Private IP number to replace it.
  
The preceding example only shows three IP numbers.
The maximum value is 256. There are already many 256 IP numbers on one network card :-)!
  
I use/sbin/ifconfig to look like this:
  
Lo Link encap: Local Loopback
Inet addr: 127.0.0.1 Bcast: 127.20.255 Mask: 255.0.0.0
Up broadcast loopback running mtu: 3584 Metric: 1
RX packets: 5088 errors: 0 dropped: 0 overruns: 0
TX packets: 5088 errors: 0 dropped: 0 overruns: 0
  
Eth0 Link encap: 10 Mbps Ethernet HWaddr 00: 8E: B8: 83: 19: 20
Inet addr: 172.16.3.1 Bcast: 172.16.3.255 Mask: 255.255.255.0
Up broadcast running promisc multicast mtu: 1500 Metric: 1
RX packets: 334036 errors: 0 dropped: 0 overruns: 0
TX packets: 11605 errors: 0 dropped: 0 overruns: 0
Interrupt: 7 Base address: 0x378
  
Eth0: 0 Link encap: 10 Mbps Ethernet HWaddr 00: 8E: B8: 83: 19: 20
Inet addr: 172.16.3.10 Bcast: 172.16.3.255 Mask: 255.255.255.0
Up broadcast running mtu: 1500 Metric: 1
RX packets: 0 errors: 0 dropped: 0 overruns: 0
TX packets: 0 errors: 0 dropped: 0 overruns: 0
  
Eth0: 1 Link encap: 10 Mbps Ethernet HWaddr 00: 8E: B8: 83: 19: 20
Inet addr: 172.16.3.100 Bcast: 172.16.3.255 Mask: 255.255.255.0
Up broadcast running mtu: 1500 Metric: 1
RX packets: 1 errors: 0 dropped: 0 overruns: 0
TX packets: 0 errors: 0 dropped: 0 overruns: 0
  
And/proc/net/aliases is
  
Device family address
Eth0: 0 2 172.16.3.10
Eth0: 1 2 172.16.3.100
  
And/proc/net/alias_types is
  
Type name n_attach
2 ip address 2
  
Of course, the/proc/net content is generated by the ifconfig command rather than handwritten!
  
Q: How can I keep the settings after I restart the machine?
A: Whether you use BSD or SysV (such as Red Hat) init, you can call it
Put it in/etc/rc. d/rc. local. below is my SysV init system (Red Hat 3.0.3
And 4.0:
  
* My/etc/rc. d/rc. local: (only relevant parts are displayed after modification)
  
# Setting up IP alias interfaces
Echo "Setting 172.16.3.1, 172.16.3.10, 172.16.3.100 IP Aliases ..."
/Sbin/ifconfig lo 127.0.0.1
/Sbin/ifconfig eth0 up
/Sbin/ifconfig eth0 172.16.3.1
/Sbin/ifconfig eth0: 0 172.16.3.10
/Sbin/ifconfig eth0: 1 172.16.3.100
# Setting up the routes
Echo "Setting IP routes ..."
/Sbin/route add-net 127.0.0.0
/Sbin/route add-net 172.16.3.0 dev eth0
/Sbin/route add-host 172.16.3.1 eth0
/Sbin/route add-host 172.16.3.10 eth0: 0
/Sbin/route add-host 172.16.3.100 eth0: 1
/Sbin/route add default gw 172.16.3.200
#
  
Q: How can I receive emails on different IP alias numbers on a machine with an IP alias?
Sendmail machine )?
A: * create a file (if not), for example,/etc/mynames. cw.
The name cannot be placed in the/etc directory.
  
* Put the complete domain name of the alias IP number in that file. if the alias IP address does not have a domain name
You can use this IP address.
  
/Etc/mynames. cw:
----------------
#/Etc/mynames. cw-include all aliases for your machine here; # is a comment.
Domain.one.net
Domain.two.com
Domain.three.org
4.5.6.7
  
* In your sendmail. cf file, add:
  
.
.
.
##################
# Local info #
##################
.
.
# File containing names of hosts for which we receive email
Fw/etc/mynames. cw
.
.
.
  
* You can run sendmail in test mode to test the new settings:
  
Ganymede $/usr/lib/sendmail-bt
Address test mode (ruleset 3 NOT automatically invoked)
Enter <ruleset> <address>
> 0 me@4.5.6.7
Rewrite: ruleset 0 input: me @ 4. 5. 6. 7
Rewrite: ruleset 98 input: me @ 4. 5. 6. 7
Rewrite: ruleset 98 returns: me @ 4. 5. 6. 7
Rewrite: ruleset 97 input: me @ 4. 5. 6. 7
Rewrite: ruleset 3 input: me @ 4. 5. 6. 7
Rewrite: ruleset 96 input: me <@ 4. 5. 6. 7>
Rewrite: ruleset 96 returns: me <@ 4. 5. 6. 7.>
Rewrite: ruleset 3 returns: me <@ 4. 5. 6. 7.>
Rewrite: ruleset 0 input: me <@ 4. 5. 6. 7.>
Rewrite: ruleset 98 input: me <@ 4. 5. 6. 7.>
Rewrite: ruleset 98 returns: me <@ 4. 5. 6. 7.>
Rewrite: ruleset 0 returns :# # local $: me
Rewrite: ruleset 97 returns :# # local $: me
Rewrite: ruleset 0 returns :# # local $: me
> 0 me@4.5.6.8
Rewrite: ruleset 0 input: me @ 4. 5. 6. 8
Rewrite: ruleset 98 input: me @ 4. 5. 6. 8
Rewrite: ruleset 98 returns: me @ 4. 5. 6. 8
Rewrite: ruleset 97 input: me @ 4. 5. 6. 8
Rewrite: ruleset 3 input: me @ 4. 5. 6. 8
Rewrite: ruleset 96 input: me <@ 4. 5. 6. 8>
Rewrite: ruleset 96 returns: me <@ 4. 5. 6. 8>
Rewrite: ruleset 3 returns: me <@ 4. 5. 6. 8>
Rewrite: ruleset 0 input: me <@ 4. 5. 6. 8>
Rewrite: ruleset 98 input: me <@ 4. 5. 6. 8>
Rewrite: ruleset 98 returns: me <@ 4. 5. 6. 8>
Rewrite: ruleset 95 input: <> me <@ 4. 5. 6. 8>
Rewrite: ruleset 95 returns: me <@ 4. 5. 6. 8>
Rewrite: ruleset 0 returns: $ # smtp $ @ 4. 5. 6. 8 $: me <@ 4. 5. 6. 8>
Rewrite: ruleset 97 returns: $ # smtp $ @ 4. 5. 6. 8 $: me <@ 4. 5. 6. 8>
Rewrite: ruleset 0 returns: $ # smtp $ @ 4. 5. 6. 8 $: me <@ 4. 5. 6. 8>
>
  
Note that when I test the me@4.5.6.7, it sends the mail to the local machine because the me@4.5.6.8 will
Rejected by smtp mailer. this is a normal response.
  
* All settings are complete.
  
I hope this is useful to some people.
  
Thanks to those who have made great contributions to Linux and IP alias, and especially to Juan Jose
Ciarlante clarified my questions. I would also like to thank those who have found out typing errors and suggestions for me!
  
Enjoy it
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.