Linux双向SSH免密登录

原理

双向,顾名思义,双方互通,此处的意思是多台 linux 两两免密登录。双向比单向多了些操作,单向只需把某一个linux的公钥发送给其他linux即可,而双向要实现集群中的每一台机器都保存其他所有机器的公钥。

步骤

假设,你有两台机器,ip分别为A和B:

总共分为三步:

  1. 生成公钥
  2. 将A机器的公钥拷贝至B机器
  3. 将B机器的公钥拷贝至A机器

1、生成公钥

如下命令生成公钥,默认会在~/.ssh/下生成id_rsa和id_rsa.pub。先检查一下机器是否已有公钥,如果没有再执行:

ssh-keygen -t rsa
  • -t 指定算法
  • -f 指定生成秘钥路径
  • -N 指定密码

2、拷贝公钥

cd ~/.ssh
scp id_rsa.pub root@B:/root/.ssh/authorized_keys #此命令在A机器执行,目的将公钥发送至B机器
scp id_rsa.pub root@A:/root/.ssh/authorized_keys #此命令在B机器执行,目的将公钥发送至B机器

scp: 加密的方式在本地主机和远程主机之间复制文件

参数:

  • 源文件:指定要复制的源文件。也可以是远程地址
  • 目标文件:目标文件。格式为user@host:filename(文件名为目标文件的名称)。

3、验证

ssh B #在A机器上,看是否免密登陆B
ssh A #在A机器上,看是否免密登陆B

如果发现设置免密登陆,还需要输入密码,那么检查一下/root.sshauthorized_keys目录和文件的权限。

chmod 600 authorized_keys 
chmod 700 .ssh

如果authorized_keys文件、$HOME/.ssh目录 或 $HOME目录让本用户之外的用户有写权限,那么sshd都会拒绝使用 ~/.ssh/authorized_keys 文件中的key来进行认证的。

Linux安全-使用iptables封掉所有邮件端口

说明:封掉邮件端口可以防止垃圾邮件

方法

1、直接封掉所有邮件协议端口

iptables -A INPUT -p tcp -m multiport --dport 25,110,465:587,993:995 -j DROP
iptables -A INPUT -p udp -m multiport --dport 25,110,465:587,993:995 -j DROP
iptables -A OUTPUT -p tcp -m multiport --dport 25,110,465:587,993:995 -j DROP
iptables -A OUTPUT -p udp -m multiport --dport 25,110,465:587,993:995 -j

2、保存iptables设置

iptables-save
service iptables save

Linux命令发送Http的get或post请求(curl和wget两种方法)

Http请求指的是客户端向服务器的请求消息,Http请求主要分为get或post两种,在Linux系统下可以用curl和wget命令来模拟Http的请求。下面就来介绍一下Linux系统如何模拟Http的get或post请求。

一、get请求

1、使用curl命令:

curl “http://www.baidu.com” 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地

curl -i “http://www.baidu.com” 显示全部信息

curl -l “http://www.baidu.com” 只显示头部信息

curl -v “http://www.baidu.com” 显示get请求全过程解析

2、使用wget命令:

wget “http://www.baidu.com”也可以

二、post请求

1、使用curl命令(通过-d参数,把访问参数放在里面):

curl -d “param1=value1¶m2=value2” “http://www.baidu.com”

  
2、使用wget命令:(–post-data参数来实现)

wget --post-data ‘user=foo&password=bar’ http://www.baidu.com

  
以上就是Linux模拟Http的get或post请求的方法了,这样一来Linux系统也能向远程服务器发送消息了。

示例:wget --post-data="" http://mcs-inner.99bill.com/mcs-gateway/mcs/task/clear

三、curl (可直接发送格式化请求例如json)

示例:目标url:http://fsc-inner.99bill.com/acs/deposit/{srcRef}

命令:curl -H "Content-type: application/json" -X POST -d '{"srcRef":"1002"}'http://fsc-inner.99bill.com/acs/deposit/1002

Linux:复制并创建目标目录,如果它不存在

我需要一个命令(或者可能是cp的一个选项)来创建目标目录(如果它不存在的话)。

示例:

cp -? file /path/to/copy/file/to/is/very/deep/there test -d "$d" || mkdir -p "$d" && cp file "$d" 

cp没有这个选项)。

如果以下两个都是真的:

  • 您正在使用cp的GNU版本(而不是Mac版本)和

  • 您正在复制某些现有的目录结构,您只需要重新创建
    即可

那么你可以用cp- parents标记来做到这一点。从信息页面([ http://www.gnu.org
/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation](http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html

#cp-invocation)或使用info cpman cp):

>

--parents Form the name of each destination file by appending to the target directory a slash and the specified name of the source file. The last argument given to `cp' must be the name of an existing directory. For example, the command: cp --parents a/b/c existing_dir copies the file `a/b/c' to `existing_dir/a/b/c', creating any missing intermediate directories. 

示例:

/tmp $ mkdir foo /tmp $ mkdir foo/foo /tmp $ touch foo/foo/foo.txt /tmp $ mkdir bar /tmp $ cp --parents foo/foo/foo.txt bar /tmp $ ls bar/foo/foo foo.txt 

linux定时休眠

最近公司规定晚上走人后必须关闭电脑,但是像我们这样的人,经常会忘记了关闭电脑,而且关闭电脑之后再恢复工作环境也是件挺麻烦的事情,无奈之下只能折腾一下,让linux定时休眠了。

休眠的类型

目前大概由三种类型的休眠:

  • suspend(suspend to RAM)
    指的是除了内存以外的大部分机器部件都进入断电状态。 这种休眠状态恢复速度特别快,但由于内存中的数据并没有被保存下来,因此这个状态的系统并没有进入真正意义上的休眠状态,还在持续耗电。

  • hibernate(suspend to disk)
    这种休眠会将内存中的系统状态写入交换空间内,当系统启动时就可以从交换空间内读回系统状态。 这种情况下系统可以完全断电,但由于要保存/读取系统状态到/从交换空间,因此速度会比较慢,而且需要进行一些配置(下面会说到)

  • hybrid(suspend to both)
    结合了上面两种休眠类型。它像hibernate一样将系统状态存入交换空间内,同时也像suspend一样并不关闭电源。 这种,在电源未耗尽之前,它能很快的从休眠状态恢复。而若休眠期间电源耗尽,则它可以从交换空间中恢复系统状态。

suspend 休眠

进入 suspend 特别简单,无需额外的配置,在 systemd 系统上直接执行 systemctl suspend 就行了。

systemctl suspend

它的实际动作由 systemd-suspend.service 所定义, 在 archlinux 上,它长成这样子的:

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Suspend
Documentation=man:systemd-suspend.service(8)
DefaultDependencies=no
Requires=sleep.target
After=sleep.target

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/systemd-sleep suspend

Hibernation 休眠

由于 hibernation 休眠要求将内存中的内容写入到交换空间中,因此你至少要有一个空间大于内存的交换分区或者交换文件。 (其实若交换空间不够内存大也不是一定就无法进行hibernation休眠,可以尝试运行 echo 0 |sudo tee /sys/power/image_size, 这会让系统在写入交换空间时尽可能的进行压缩,但这种方法也无法保证一定能够休眠成功)

若之前没有创建交换分区,那么可以临时创建一个交换文件来用。比如下面命令创建一个5G的交换文件

sudo dd if=/dev/zero of=/swapfile bs=10240 count=524288
sudo mkswap /swapfile
sudo chmod 0600 /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
echo "/swapfile swap swap default 0 0" |tee -a /etc/fstab
Setting up swapspace version 1, size = 5 GiB (5368705024 bytes)
no label, UUID=d0f0c682-e1fa-416f-8fe2-b554b8ca363a
/swapfile swap swap default 0 0

除此创建交换分区之外,我们还需要修改kernel的启动参数,让系统在启动时先尝试从交换空间中恢复状态。 具体操作如下:

1、如果使用交换分区来保存,则只需要为添加kernel的启动参数 resume=交换分区 即可

1)查看那块分区是交换分区

swapon
NAME      TYPE      SIZE USED PRIO
/dev/sda2 partition   8G 280K   -2
/swapfile file        5G   0B   -3

可以看出交换分区为 /dev/sda2

2)修改 /etc/default/grub, 为 GRUB_CMDLINE_LINUX_DEFAULT 行添加参数 resume=/dev/sda2

sudo sed -i '/GRUB_CMDLINE_LINUX_DEFAULT/ s!"$! resume=/dev/sda2"!' /etc/default/grub

2、如果是使用交换文件,则需要添加两个参数 resume=交换文件所在磁盘 以及 resume_offset=交换文件在磁盘中的偏移位置:

1)查看交换文件所在磁盘

df /swapfile
文件系统          1K-块     已用     可用 已用% 挂载点
/dev/sda3      55253696 27582224 24834972   53% /

说明磁盘为 /dev/sda3

2)查看交换文件的偏移位置

sudo filefrag -v /swapfile|head -5
Filesystem type is: ef53
File size of /swapfile is 5368709120 (1310720 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..   32767:    4653056..   4685823:  32768:            
   1:    32768..   65535:    4685824..   4718591:  32768:            

这里可以看出物理偏移位置时4653056

3)修改 /etc/default/grub, 为 GRUB_CMDLINE_LINUX_DEFAULT 行添加参数 resume=/dev/sda3 resume_offset=4653056

sudo sed -i '/GRUB_CMDLINE_LINUX_DEFAULT/ s!"$! resume=/dev/sda3 resume_offset=4653056"!' /etc/default/grub

3、重新生成 grub.cfg 文件

sudo grub-mkconfig -o /boot/grub/grub.cfg

1)配置initramfs添加 resume hook 修改 /etc/mkinitcpio.conf 文件,在 HOOKS 中添加 resume

sudo sed -i '/^HOOKS=/ s/)/ resume)/' /etc/mkinitcpio.conf 

其中由两点需要注意:
– 由于分区的label和UUID都是 udev 分配的,因此 resume 必须放在 udev 之后
– 由于 systemd hook 已经有了 resume 的功能,因此若已经有了 systemd hook,则无需再添加 udev hook

2)重新生成initramfs

sudo mkinitcpio -g /boot/initramfs-linux-lily.img
==> Starting build: 4.16.12-2-lily
  -> Running build hook: [base]
  -> Running build hook: [udev]
  -> Running build hook: [autodetect]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
  -> Running build hook: [resume]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/initramfs-linux-lily.img
==> Image generation successful

3)重启,让配置生效

经过上面复杂的配置后,hibernation 休眠才能真正成功。与 suspend 休眠类似,我们也能使用 systemctl 来进行休眠

systemctl hibernate

类似的,它的实际动作由 systemd-hibernte.service 所定义, 在 archlinux 上,它长成这样子的:

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Hibernate
Documentation=man:systemd-suspend.service(8)
DefaultDependencies=no
Requires=sleep.target
After=sleep.target

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/systemd-sleep hibernate

hybrid 休眠

在配置好 hibernate 休眠后,也就能正常进行 hybrid 休眠了,方法是执行

systemctl hybrid-sleep

类似的,它的实际动作由 systemd-hybrid-sleep.service 所定义, 在 archlinux 上,它长成这样子的:

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Hybrid Suspend+Hibernate
Documentation=man:systemd-suspend.service(8)
DefaultDependencies=no
Requires=sleep.target
After=sleep.target

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/systemd-sleep hybrid-sleep

Sleep Hooks

从上面的service文件中可以看出,不管是哪种类型的系统休眠,其内部实际调用的都是 systemd-sleep.

man systemd-sleep
SYSTEMD-SUSPEND.SERVICE(8)  systemd-suspend.service SYSTEMD-SUSPEND.SERVICE(8)

NAME
       systemd-suspend.service, systemd-hibernate.service, systemd-hybrid-
       sleep.service, systemd-sleep - System sleep state logic

SYNOPSIS
       systemd-suspend.service

       systemd-hibernate.service

       systemd-hybrid-sleep.service

       /usr/lib/systemd/system-sleep

DESCRIPTION
       systemd-suspend.service is a system service that is pulled in by
       suspend.target and is responsible for the actual system suspend.
       Similarly, systemd-hibernate.service is pulled in by hibernate.target
       to execute the actual hibernation. Finally,
       systemd-hybrid-sleep.service is pulled in by hybrid-sleep.target to
       execute hybrid hibernation with system suspend.

       Immediately before entering system suspend and/or hibernation
       systemd-suspend.service (and the other mentioned units, respectively)
       will run all executables in /usr/lib/systemd/system-sleep/ and pass two
       arguments to them. The first argument will be "pre", the second either
       "suspend", "hibernate", or "hybrid-sleep" depending on the chosen
       action. Immediately after leaving system suspend and/or hibernation the
       same executables are run, but the first argument is now "post". All
       executables in this directory are executed in parallel, and execution
       of the action is not continued until all executables have finished.

       Note that scripts or binaries dropped in /usr/lib/systemd/system-sleep/
       are intended for local use only and should be considered hacks. If
       applications want to react to system suspend/hibernation and resume,
       they should rather use the Inhibitor interface[1].

       Note that systemd-suspend.service, systemd-hibernate.service, and
       systemd-hybrid-sleep.service should never be executed directly.
       Instead, trigger system sleep states with a command such as "systemctl
       suspend" or similar.

       Internally, this service will echo a string like "mem" into
       /sys/power/state, to trigger the actual system suspend. What exactly is
       written where can be configured in the "[Sleep]" section of
       /etc/systemd/sleep.conf or a sleep.conf.d file. See systemd-
       sleep.conf(5).

OPTIONS
       systemd-sleep understands the following commands:

       -h, --help
           Print a short help text and exit.

       --version
           Print a short version string and exit.

       suspend, hibernate, hybrid-sleep
           Suspend, hibernate, or put the system to hybrid sleep.

SEE ALSO
       systemd-sleep.conf(5), systemd(1), systemctl(1), systemd.special(7),
       systemd-halt.service(8)

NOTES
        1. Inhibitor interface
           https://www.freedesktop.org/wiki/Software/systemd/inhibit

systemd 238                                         SYSTEMD-SUSPEND.SERVICE(8)

根据 systemd-sleep 的manual pages,可以看到在系统休眠之前以及从休眠状态恢复之后,都会并行地调用 /usr/lib/systemd/system-sleep 中的脚本,并传递两个参数。

第一个参数用来指定是开始休眠还是从休眠状态恢复,分别对应的字符串 “pre” 与 “post”.

第二个参数用来指明休眠的类型,分别为字符串 “suspend”, “hibernate” 以及 “hybrid-sleep”

定时执行休眠

systemd 系统中的定时任务是由timer来实现的,而每个timer都与一个service相对应。

一般情况下,timer的名称与service一致,但必要时可以通过在.timer文件中的 [Timer] 部分指定 Unit= 选项来控制一个与timer不同名的service。

下面是一个timer的例子,每天21:30分开始自动hibernate休眠

[Unit]
Description=Hibernate every 21:30:00

[Timer]
OnCalendar=*-*-* 21:30:00
Persistent=true
Unit=systemd-hibernate.service

[Install]
WantedBy=timers.target

定时唤醒休眠的linux

使用 rtcwake 可以在给定的时间唤醒处于休眠状态的电脑

其主要用法为:

sudo rtcwake -m ${mode} -t ${time_t}
# 或者
sudo rtcwake -m ${mode} -s ${seconds}

其中,参数mode为待机模式,有以下几个选项:

  • standby
    普通待机模式,为默认选项,对应 ACPI state S1

  • mmem
    suspend休眠,对应 ACPI state S3

  • disk
    hibernation 休眠,对应 ACPI state S4

  • off
    通过调用系统的关机命令来休眠,对应 ACPI state S5

参数 time_t 为从 1970-01-01, 00:00 UTC 开始到现在的秒数,可以通过 date 命令来将时间字符串转换成这个秒数,比如

sudo rtcwake -m disk -t $(date -d 08:30 +%s)

就是进行 hibernation 休眠,并于08:30分唤醒

参数 seconds 为秒数,表示从现在开始的多少秒后,系统唤醒。

Linux查杀stopped进程

在Linux系统下面,top命令可以查看查看stopped进程。但是不能查看stopped进程的详细信息。那么如何查看stopped 进程,并且杀掉这些stopped进程呢?

ps -e j | grep T 

未分类

stopped进程的STAT状态为T,一般而言,进程有下面这些状态码:

D    uninterruptible sleep (usually IO)

I    Idle kernel thread

R    running or runnable (on run queue)

S    interruptible sleep (waiting for an event to complete)

T    stopped by job control signal

t    stopped by debugger during the tracing

W    paging (not valid since the 2.6.xx kernel)

X    dead (should never be seen)

Z    defunct ("zombie") process, terminated but not reaped by

     its parent



for BSD formats and when the stat keyword is used, additional
rs may be displayed:



<    high-priority (not nice to other users)

N    low-priority (nice to other users)

L    has pages locked into memory (for real-time and custom

     IO)

s    is a session leader

l    is multi-threaded (using CLONE_THREAD, like NPTL

     pthreads do)

+    is in the foreground process group

一般较常见的是5种状态码:

  • D 不可中断 uninterruptible sleep (usually IO)
  • R 运行 runnable (on run queue)
  • S 中断 sleeping
  • T 停止 traced or stopped
  • Z 僵死 a defunct (”zombie”) process

所以,可以用下面命令ps -A -ostat,ppid,pid,cmd | grep -e ‘^[T]’ 查看stopped的进程信息。如下所示:

#  ps -A -ostat,ppid,pid,cmd | grep -e '^[T]'

T     6777  8635 more alert_pps.log
T     6777  9654 tail -60f alert_pps.log
T     6777 10724 top
# kill -9 8635
#  ps -A -ostat,ppid,pid,cmd | grep -e '^[T]'
T     6777  9654 tail -60f alert_pps.log
T     6777 10724 top
# kill -9 9654
# kill -9 10724

Linux下对文件进行md5校验命令

为解决官方发布的软件包被别人更改或者软件在传输过程中出现传输错误等问题,软件官方在提供软件包的同时,还提供一个保存MD5校验码的文件。

Linux/unix中可以使用

md5sum 文件名

方式获得文件的md5值。使用cat 命令查看一下官方软件包的md5值,就可以进行比对了。下次安装软件记得md5sum一下奥。

linux下使用supervisor监控应用程序

1、应用场景

应用程序需要24小时不间断运行。这时可使用supervisor监控应用程序的进程。当发生应用程序内部错误退出、进程被杀死等情况时,自动重启应用程序。

2、supervisor

supervisor由python写成, 简单好用。官方网站 http://supervisord.org,上面有详细的指南文档。

3、安装supervisor

1)安装 setuptools

$sudo apt-get install python-setuptools

2) 使用easy_install安装 supervisor

$sudo easy_install supervisor

安装完成后出现:

/usr/bin/supervisord --supervisor服务守护进程
/usr/bin/supervisorctl--supervisor服务控制程序

4、配置文件

以下命令可以将配置文件示例打印到控制台:

$ echo_supervisord_conf

若要生成配置文件,使用命令:

$ sudo echo_supervisord_conf > /etc/supervisord.conf

或者在当前目录生成:

$ echo_supervisord_conf > supervisord.conf

Notice: 如何停止子进程

场景:如果supervisord.conf中配置的command是执行一个bash,而bash里执行java,那么当使用supervisorctl stop [programname]停止程序时,只有上层进程被停止,而java进程没有被停止。

解决办法:

在配置文件中设置:

stopasgroup=true               
killasgroup=true 

5、配置要运行的程序

在上面生成的配置文件后,追加:

[program:helloworld]
command=./helloworld              ;执行命令
process_name=%(program_name)s
autostart=true                   ; 程序是否随supervisor启动而启动
autorestart=true                 ;程序停止时,是否自动重启
startsecs=10

6、启动supervisor

$ supervisord -c supervisord.conf

7、使用supervisorctl管理程序

1)开启/停止某个程序

# supervisorctl [start | stop] [program名称]      //在supervisord.conf中定义的

2)查看进程状态

$supervisorctl status

8、通过web方式查看状态

配置文件中配置:

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)

那么可以从浏览器通过访问 localhost:9001来查看进程状态

9、添加supervisord为Linux系统服务,开机自动启动

1)将supervisord.conf拷贝到 /etc 目录下

2)启动脚本 supervisord.sh (centos/redhat)

#!/bin/sh
#
# /etc/rc.d/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord

# Source init functions
. /etc/rc.d/init.d/functions

prog="supervisord"

prog_bin="/usr/local/bin/supervisord"
PIDFILE="/tmp/supervisord.pid"

start()
{
        echo -n $"Starting $prog: "

# Source init functions
. /etc/rc.d/init.d/functions

prog="supervisord"

prog_bin="/usr/local/bin/supervisord"
PIDFILE="/tmp/supervisord.pid"start()
{
        echo -n $"Starting $prog: "
        daemon $prog_bin --pidfile $PIDFILE
        [ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
        echo
}

stop()
{
        echo -n $"Shutting down $prog: "
        [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
        echo
}

case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  status)
        status $prog
  ;;
  restart)
    stop
    start
  ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
  ;;

esac

3)添加为系统服务

# mv supervisord.sh  /etc/init.d/supervisord
# chkconfig --add  supervisord
# chkconfig --level 345 supervisord on

4) 开启/停止服务

# service supervisord [start | stop]

CentOS Linux解决Device eth0 does not seem to be present异常

在VMware里克隆出来的CentOS Linux。。

ifconfig…没有看到eth0.。然后重启网卡又报下面错误。

故障现象:

Shutting down loopback insterface:                                                                                                     [  OK  ]
Bringing up loopback insterface:                                                                                                          [  OK  ]
Bringing up interface eth0:  Device eth0 does not seem to be present,delaying initialization.                    [FAILED]
undefined

未分类

如上图所示

解决办法:

首先,打开/etc/udev/rules.d/70-persistent-net.rules内容如下面例子所示:

# vi /etc/udev/rules.d/70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
# PCI device 0x1022:0x2000 (pcnet32)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:8f:89:9
7", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x1022:0x2000 (pcnet32)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:50:bd:1
7", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

记录下,eth1网卡的mac地址00:0c:29:50:bd:17

接下来,打开/etc/sysconfig/network-scripts/ifcfg-eth0

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

将 DEVICE=”eth0″ 改成 DEVICE=”eth1″ ,将 HWADDR=”00:0c:29:8f:89:97″ 改成上面的mac地址 HWADDR=”00:0c:29:50:bd:17″

最后,重启网络

# service network restart或者

# /etc/init.d/network restart

正常了。