linux

linux远程开机(zz)

2010年4月23日 阅读(318)

http://www.91linux.com/html/article/network/20090120/15461.html

一,什么情况下需要远程开机?
   如果我们的服务器没有部署在本地(实际上通常都是这样的,我们会把服务器托管到IDC机房),
  而且服务器在机房中不止一台,其中一台被关闭时,则我们可以远程连接一台没有关机的服务器上,
  然后进行远程开机.
 
二,远程开机需要的软件
    它需要wakeonlan这个软件,
    从何处得到它?
    它的官方站是:http://sourceforge.net/projects/wake-on-lan/
    如果使用rpm包可以从这里下载:http://dag.wieers.com/rpm/packages/wol/
    如果使用fedora,则可以用yum命令安装:
    yum install wol
   
三,如何进行远程开机?
   先不要急着去关闭你的linux服务器,你首先要确定它是否支持远程开机?
  第一步:登录到目标服务器,用ethtool这个命令打印出网卡的信息
        [root@localhost lhd]# ethtool eth0
        Settings for eth0:
        Supported ports: [ TP MII ]
        Supported link modes:   10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
        Supports auto-negotiation: Yes
        Advertised link modes:  10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
        Advertised auto-negotiation: Yes
        Speed: 100Mb/s
        Duplex: Full
        Port: MII
        PHYAD: 32
        Transceiver: internal
        Auto-negotiation: on
        Supports Wake-on: pumbg
        Wake-on: d
        Current message level: 0x00000007 (7)
        Link detected: yes

        可以看到,ethtool把网卡的信息全部列出,我们只关心其中的这两项:
        Supports Wake-on: pumbg
        Wake-on: d
          如果 wake-on 一项值为 d,表示禁用wake on lan
                         值为 g,表示启用 wake on lan
                             
    因为此机器禁用了 wake on lan,所以用下面的命令来启用它:
    [root@localhost lhd]# ethtool -s eth0 wol g

    再用 ethtool命令进行查看,会发现:
    Wake-on: g
            
    OK,目标机器的网卡已经支持了远程开机,下面我们得到它的本地MAC地址:
    [root@localhost lhd]# ifconfig
          eth0      Link encap:Ethernet  HWaddr 00:03:0D:1D:1F:97
          inet addr:192.168.6.101  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:34470 errors:0 dropped:0 overruns:0 frame:0
          TX packets:35377 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:31559763 (30.0 MiB)  TX bytes:5340032 (5.0 MiB)
          Interrupt:5 Base address:0x2c00
    把HWaddr 00:03:0D:1D:1F:97这一项记录下来即可

    现在你可以试着把目标机器关闭

  第二步:开机

     现在我们需要登录到已安装了wakeonlan软件的机器上,在上面执行开机命令:
     
     wol 00:03:0D:1D:1F:97
     稍后就会发现,目标机器已开机可以登录了

    wakeonlan可以在这里下载:http://gsd.di.uminho.pt/jpo/software/wakeonlan/
     
     
     
四,多学一点:
    1,ethtool的 -s 参数是修改指定以太网设备的设置   
    2,wol的取值默认是d,含义是disable
           修改后的值为g,含义是Wake on MagicPacket(tm)             
      它还有几个取值,分别是:          
               p  Wake on phy activity
              u  Wake on unicast messages
              m  Wake on multicast messages
              b  Wake on broadcast messages
              a  Wake on ARP
                 
      如果有兴趣,大家可以通过 man ethtool查看
     
    3,当机器重启后,eth0的设置又会回复到Wake-on: d 状态,
      这个问题怎么解决?
      两个办法:第一个,也是我们的惯性思维;
             把/sbin/ethtool -s eth0 wol g 这条命令附加到/etc/rc.local这个文件中,
             则下次开机后会自动执行

             第二个: 编辑/etc/sysconfig/network-scripts/ifcfg-eth0
                  (eth0网卡的配置文件),添加上一行:
                     ETHTOOL_OPTS=”wol g”

     4,网络唤醒的局限性:
       它只能在局域网中应用,而不能通过互联网运行,为什么?
        因为机器关闭后,完全是靠网卡唤醒机器,此时的机器是关闭的,没有操作系统运行,
        也就谈不上支持tcp/ip协议,当然也就不能通过互联网运行了.
       
        也就是说:如果我们在某个局域网中只有一台机器,就不能使用此功能了.
        我们必须能登录到局域网中的一台机器上,在此机器上运行wake on lan 去唤醒目标机器
        前提条件就是:目标机器和我们登录的机器在同一局域网中

     5,还有一点:被远程开机的目标机器必须是插电的,没插电源的机器也能开机只有电影中才会出现

HOWTO: Set your system up for Wake On LAN (WOL)
Manual way:
————————
1. If you havent already, go to your BIOS, and turn on WakeOnLAN (it varies, look for it.) If your network card is onboard, your set for step 2, otherwise there is probably a cable that should go from your network card to your motherboard, though this is not always the case.

2. Back in ubuntu, kubuntu, xubuntu, w/e, we now need to make a script that will run every time the computer is started, because this command only lasts until the computer is turned on again once.

2a. Find out what network device you want to have the computer wake-able from, usually all, which is just one. If you have more network devices in your system, 9 chances out of 10, you already know what they are called.
You can NOT wake up a laptop or computer that is only connected via wireless with wake-on-lan, unless the bios has a method for this, this is very rare, and I do not garuntee this howto will work in such cases.
In your terminal, type:
ifconfig
You’ll get something like: (I have removed my mac address for security)
eth0 Link encap:Ethernet HWaddr 01:23:45:67:89:ab
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::215:f2ff:fe6f:3487/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:71495 errors:0 dropped:0 overruns:0 frame:0
TX packets:76190 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:23164212 (22.0 MiB) TX bytes:7625016 (7.2 MiB)
Interrupt:217 Base address:0xd400

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1290 errors:0 dropped:0 overruns:0 frame:0
TX packets:1290 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:161182 (157.4 KiB) TX bytes:161182 (157.4 KiB)

So, I want this system to be wake-able from eth0.

2b. Now we create the script.
Note: you must be an administrator on the system you are doing this to.
sudo -i
Enter your password at the prompt.
Change to the startup script directory and start editing a new file:
cd /etc/init.d/
pico wakeonlanconfig

Paste, or type this into the file, replacing eth0 with your network device, repeat the ethtool line as many times for your devices before the exit line:
#!/bin/bash
ethtool -s eth0 wol g
exit

Set the permissions of the file:
chmod a+x wakeonlanconfig

Make the script run on startup:
update-rc.d -f wakeonlanconfig defaults
You should see something like:
Adding system startup for /etc/init.d/wakeonlanconfig …
/etc/rc0.d/K20wakeonlanconfig -> ../init.d/wakeonlanconfig
/etc/rc1.d/K20wakeonlanconfig -> ../init.d/wakeonlanconfig
/etc/rc6.d/K20wakeonlanconfig -> ../init.d/wakeonlanconfig
/etc/rc2.d/S20wakeonlanconfig -> ../init.d/wakeonlanconfig
/etc/rc3.d/S20wakeonlanconfig -> ../init.d/wakeonlanconfig
/etc/rc4.d/S20wakeonlanconfig -> ../init.d/wakeonlanconfig
/etc/rc5.d/S20wakeonlanconfig -> ../init.d/wakeonlanconfig

Now we finish by running it, and making sure there are no errors.
/etc/init.d/wakeonlanconfig
This should produce no output and put you right back at the prompt you started at.

3. Use it. you’ll need something to send wake-on-lan packets with, "wakeonlan" is in the repos. And you’ll need the mac address of the system.

To get your MAC address, on the same system you just enabled WOL on, type:
ifconfig | grep HW
its the thing that looks like 01:23:45:67:89:ab , write it down.
turn off that system:
sudo halt

if your using wakeonlan from the repos, and you are on the same network as the computer your tying to wake up, replace 01:23:45:67:89:ab with your mac address and do, from another computer:
wakeonlan 01:23:45:67:89:ab
In MOST cases, you CAN SEND wake on lan packets from a wireless connected computer.
If that doesnt work, its likely the port on the system your trying to wake up isnt the default (9), try 7, or if your BIOS settings or book told you one, use that one.
wakeonlan -p 7 01:23:45:67:89:ab

If that STILL doesnt work, make sure wakeonlan is enabled in your bios and your hardware supports it.

*Note: It has been said that you need to disable -i from halt, however I have never had to do this, nor do I know how.

4. Sit on your lazy *** and have fun 🙂

Feel free to post any questions, suggestions, problems and I will tend to them ASAP.

Added notes:
* For this to work, most systems must be shut down properly, ie: with the power button or halt, or any of the ways to shut down. Unclean power-offs (like a power outage or holding the power button for 5s) seem to stop WOL from working untill the system is powered on and shut down properly. Though, there my be a few exceptions. This is a hardware issue with the BIOS. In my opinion, WOL should work regardless of how the system is powered off, but thats not the case. I suggest, if you have frequent power outages, that you have your BIOS set to Power ON after a power failure, most new systems allow this.

hvollebregt

August 25th, 2006, 03:17 PM

Many Thanks Chris, for this clear explanation. It saved me a lot of time.. I am very new to Linux, and already found how that the ethtool wol settings aren’t persisted, but I didn’t figure out yet how to make my system remember them.

The only problem I now have is: my computer wakes up within a minute after shutting down. Something wakes it up (which gives me hope that WOL *is* actually working on my system) but I want it to start up only if *I* send a magic package of course! I did set the wol setting to: g.

Do you have any idea what could be causing this? In the mean time, I am going to check my bios settings..

Cheers
Hans

You Might Also Like