如何在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便于处理某些操作,之后再通过命令还原。