野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10585|回复: 1

[经验\资料] 【imx6】linux开发板设置静态IP地址

[复制链接]
发表于 2020-1-6 10:22:55 | 显示全部楼层 |阅读模式
在开机的时候,都会默认使用DHCP设置动态IP地址,但在某些场合可能需要一个静态的IP地址,我们虽然可以通过命令 ifconfig eth0 192.168.xxx.xxx 去设置临时的静态IP地址,但每次设置就太麻烦了,需要开机自动去设置,所以就有以下的帖子:

设置静态IP
1. 编辑/etc/network/interfaces文件:
[mw_shl_code=c,true]vi /etc/network/interfaces
//可以看到如下内容# Wired or wireless interfaces
auto eth0
iface eth0 inet dhcp
iface eth1 inet dhcp
[/mw_shl_code]
从这可以看出网卡都是动态设置IP地址的,我们可以将它注释掉,以eth0为例子:
[mw_shl_code=c,true]# Wired or wireless interfaces
auto eth0
#iface eth0 inet dhcp  //注释这一行
iface eth1 inet dhcp
[/mw_shl_code]
并且在文件中添加以下内容:
[mw_shl_code=c,true]iface eth0 inet static  //设置eth0为静态的IP地址方式
address 192.168.xxx.xxx   //你的IP地址
netmask 255.255.255.0   //子网掩码
gateway 192.168.xxx.xxx     //网关
broadcast 192.168.xxx.255 //广播
[/mw_shl_code]
比如我的整个文件内容如下,我设置了IP地址为:
[mw_shl_code=c,true]# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)

# The loopback interface
auto lo
iface lo inet loopback

# Wireless interfaces
iface wlan0 inet dhcp
        wireless_mode managed
        wireless_essid any
        wpa-driver wext
        wpa-conf /etc/wpa_supplicant.conf

iface atml0 inet dhcp

# Wired or wireless interfaces
auto eth0
#iface eth0 inet dhcp
iface eth1 inet dhcp

iface eth0 inet static
address 192.168.0.100
netmask 255.255.255.0
gateway 192.168.0.1
broadcast 192.168.0.255

# Ethernet/RNDIS gadget (g_ether)
# ... or on host side, usbnet and random hwaddr
iface usb0 inet static
        address 192.168.7.2
        netmask 255.255.255.0
        network 192.168.7.0
        gateway 192.168.7.1

# Bluetooth networking
iface bnep0 inet dhcp[/mw_shl_code]
2. 重启网卡
当你设置完毕后,你可以重启网卡,此时你的IP地址就会变成你自己设置的了:
[mw_shl_code=c,true]/etc/init.d/networking restart
[/mw_shl_code]
3. 设置开机自动运行
只需要在你的脚本中写入上面的命令即可,比如我是在我自己的启动脚本 S99cpu-freq.sh 中写入/etc/init.d/networking restart
[mw_shl_code=c,true]echo "/etc/init.d/networking restart" | tee -a /etc/rc5.d/S99cpu-freq.sh[/mw_shl_code]
注意啊,这里的开机脚本是一定要按照格式命名,并且是一个可执行文件,你也可以自己写一个脚本....
然后重启就可以了。

当然还有一种更简洁的方法:

在/etc/init.d/rc下修改
在文件的最后加入以下命令就可以了:
[mw_shl_code=c,true]
ifconfig eth0 192.168.xxx.xxx netmask 255.255.255.0
route add default gw 192.168.xxx.1
[/mw_shl_code]

也可以通过以下命令追加到文件中:
[mw_shl_code=c,true]echo "ifconfig eth0 192.168.xxx.xxx netmask 255.255.255.0" | tee -a /etc/init.d/rc
echo "route add default gw 192.168.xxx.1" | tee -a /etc/init.d/rc[/mw_shl_code]

注意把xxx改为你对应的网段即可。




回复

使用道具 举报

发表于 2020-1-15 10:12:10 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-5 17:01 , Processed in 0.027413 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表