如何在Linux中设置静态IP和配置网络

如何在Linux中设置静态IP和配置网络



包括Centos系列和Ubuntu系列,命令修改和配置文件修改:
https://www.cyberciti.biz/faq/howto-linux-configuring-default-route-with-ipcommand/

1、使用ifconfig的一次性修改

这个在Linux系统上比较统一,基本目前接触到的Linux都支持ifconfig命令来修改ip。

(1)、修改静态ip

命令如下(来自man ifconfig):

ifconfig [-v] [-a] [-s] [interface]
ifconfig [-v] interface [aftype] options | address ...

-a     display all interfaces which are currently available, even if down

-s     display a short list (like netstat -i)

-v     be more verbose for some error conditions

interface
	The name of the interface.  This is usually a driver name followed by a  unit number, for example eth0 for the first Ethernet interface. If your kernel supports alias interfaces, you can specify them with syntax  like eth0:0 for the first alias of eth0. You can use them to assign more addresses. To delete an alias interface use ifconfig eth0:0down. Note: for every scope (i.e. same net with address/netmask combination) all aliases are deleted, if you delete the first (primary).
	
...		

ifconfig --help:

Usage:
  ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
  [add <address>[/<prefixlen>]]
  [del <address>[/<prefixlen>]]
  [[-]broadcast [<address>]]  [[-]pointopoint [<address>]]
  [netmask <address>]  [dstaddr <address>]  [tunnel <address>]
  [outfill <NN>] [keepalive <NN>]
  [hw <HW> <address>]  [mtu <NN>]
  [[-]trailers]  [[-]arp]  [[-]allmulti]
  [multicast]  [[-]promisc]
  [mem_start <NN>]  [io_addr <NN>]  [irq <NN>]  [media <type>]
  [txqueuelen <NN>]
  [[-]dynamic]
  [up|down] ...

示例(修改ip和子网掩码):

ifconfig eth0 40.40.40.200 netmask 255.255.255.0
(2)、修改网关

使用route进行修改:

$ route --help
Usage: route [-nNvee] [-FC] [<AF>]           List kernel routing tables
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

       route {-h|--help} [<AF>]              Detailed usage syntax for specified AF.
       route {-V|--version}                  Display version/author and exit.

        -v, --verbose            be verbose
        -n, --numeric            don't resolve names
        -e, --extend             display other/more information
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

  <AF>=Use -4, -6, '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
    x25 (CCITT X.25)

man route:

NAME
       route - show / manipulate the IP routing table

SYNOPSIS
       route [-CFvnNee] [-A family |-4|-6]

       route  [-v]  [-A family |-4|-6] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] [mss M] [window W] [irtt I] [reject]  [mod]  [dyn]  [reinstate] [[dev] If]

       route  [-v]  [-A family |-4|-6] del [-net|-host] target [gw Gw] [netmask Nm] [metric M] [[dev] If]

       route  [-V] [--version] [-h] [--help]

示例(修改网关地址):

route add default gw 40.40.40.1

2、修改配置文件的永久性修改

这个由于过于偏应用层,所以基于Linux内核的不同应用系统对于网络的配置不太统一,一般来说包括Ubuntu/Debian类和Redhat/CentOS类,还有一些嵌入式Linux就更奇怪了,只能特殊性兼容。

(1)、Ubuntu相关系统

对于Ubuntu,这里有一个在旧系统中编辑/etc/networking/*的替代品。整个系统现在使用/etc/netplan下的YAML配置文件,然后netplan命令将这些配置应用到系统。基本上嵌入式Ubuntu都还是遵循旧系统,在network/interfaces文件中修改。

//使用vi/vim打开配置文件进行修改,一般如下:
$sudo vim /etc/network/interfaces
auto eth0
iface eth0 inet static
address 192.168.0.200
gateway 192.168.0.1
netmask 255.255.255.0

修改完后进行网络重启即时生效:

sudo /etc/init.d/networking restart
或
sudo service networking restart
(2)、CentOS相关系统

根据网卡接口修改对应的网络配置文件:

$sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0c:29:17:c4:09
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
UUID="xxxxxx-xxxx..."
IPADDR=192.168.0.200
NETMASK=255.255.255.0
GATWAY=192.168.0.1

修改完后重启即时生效:

sudo /etc/init.d/networking restart
或
sudo service networking restart

3、使用ifconfig模拟永久性修改的方案

可以将ifconfg修改ip的命令写入开机脚本或定时执行来模拟永久性生效,但是不推荐这种方式,通过修改配置文件的方法更加统一,避免出现多个程序修改配置文件后由于该种方式影响的情况。但是在某些hacking的场景下进行脚本一键化处理的时候比较有用,修改ip便于处理某些操作,之后再通过命令还原。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昵称系统有问题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值