linux通过systemd配置ip

linux通过systemd配置ip,第1张

进来研究STM32MP157芯片的使用,手上有块米尔的开发板,通过电脑用网线直连,上电每次都需要设置eth0的IP和电脑的IP在同一网段,才可以连通。于是就想将开发板的eth0设置为静态IP,本以为修改/etc/network/interfaces文件即可,结果发现根本没这个文件。网上一搜,才明白我这个linux版本采用了systemd方式配置自启动。于是仿网上的说明先写了一个interface.sh的脚本,就一句话:

然后又在/etc/systemd/system/目录下写了一个setip.servic的文件,文件是仿网上写的,具体每个参数的含义没有深究,等有空的时候或是下次用的在研究吧。

Linux配置ip地址的两种方法,实验环境为centos7.6方法1:nmcli工具配置 (centos7以下版本不支持该方法)第一步,通过nmcli connection查看网卡名称[root@localhost ~]# nmcli connection NAME UUID TYPE DEVICEeth0  09be0948-faf1-43b6-a5a4-c19efab0bb48 ethernet eth0第二步,配置ip,网关,dns,并设置网卡开机自动启动,最后开启网卡[root@localhost ~]# nmcli connection modify eth0 ipv4.addresses "192.168.1.201/24" 说明:配置地址和掩码 [root@localhost ~]# nmcli connection modify eth0 ipv4.gateway "192.168.1.1" 说明:配置网关 [root@localhost ~]# nmcli connection modify eth0 ipv4.dns "180.76.76.76" 说明:配置dns [root@localhost ~]# nmcli connection modify eth0 ipv4.method manual 说明:地址获取的方法为手动配置而不是dhcp [root@localhost ~]# nmcli connection modify eth0 autoconnect yes 说明:开机自动打开网卡 [root@localhost ~]# nmcli connection up eth0 说明:立即打开网卡 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)第三步,查看结果(这里使用ip addr命令查看,较新版本Linux系统支持该命令)[root@localhost ~]# ip addr1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:84:23:62 brd ff:ff:ff:ff:ff:ffinet 192.168.1.201/24  brd 192.168.1.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::b7ad:e2ed:832e:99a9/64 scope link noprefixroute valid_lft forever preferred_lft forever测试通信[root@localhost ~]# ping www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=53 time=34.7 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=53 time=27.9 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=53 time=24.1 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=53 time=25.2 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=53 time=24.2 ms ^C --- www.a.shifen.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4005ms rtt min/avg/max/mdev = 24.177/27.277/34.718/3.970 ms 方法2:通过vi编辑网卡配置文件(最新版rhel8或centos8不推荐该方法,老版本rhel6及以下推荐该方法)第一步,通过vi或vim打开配置文件[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0第二步,编辑相关的参数TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 UUID=09be0948-faf1-43b6-a5a4-c19efab0bb48 DEVICE=eth0 ONBOOT=yes IPADDR=192.168.1.202 PREFIX=24 GATEWAY=192.168.1.1 DNS1=180.76.76.76 PEERDNS=no vi的编辑方法是,输入字母i,进行编辑,编辑完成后,按esc,再按:wq 保存退出。如果不保存,则是:q!退出第三步,重启网络服务[root@localhost ~]# service network restart Restarting network (via systemctl): [ OK ]第四步,查看结果并测试通信(这里用ifconfig命令来查看,各种版本Linux均支持该命令)[root@localhost ~]# ifconfig eth0 eth0: flags=4163 mtu 1500 inet 192.168.1.202 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::b7ad:e2ed:832e:99a9 prefixlen 64 scopeid 0x20 ether 00:0c:29:84:23:62 txqueuelen 1000 (Ethernet) RX packets 1117 bytes 127303 (124.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 564 bytes 69559 (67.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# ping www.baidu.com PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data. 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=53 time=28.2 ms 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=53 time=30.9 ms ^C --- www.a.shifen.com ping statistics --- 3 packets transmitted, 2 received, 33% packet loss, time 2003ms rtt min/avg/max/mdev = 28.228/29.590/30.953/1.373 ms [root@localhost ~]# 配置地址的两种方法就介绍到这里。 但是查看地址时,我们并没有看到网关和dns,那么网关和dns怎么看呢,用以下两条命令即可 [root@localhost ~]# route -n 通过查看路由表来知道网关 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0 [root@localhost ~]# cat /etc/resolv.conf 通过查看rsolv.conf文件来查看dns # Generated by NetworkManager nameserver 180.76.76.76 更多网络技术公开课,腾讯课堂搜“ 众元教育 ”,戳 免费公开课 ,可直接听课哦!

要修改IP地址只需要启动开发板并找到/etc/init.d/rcS,在这里可以找到/sbin/ifconfig eth0,修改后面的IP地址就可以了。

同时可以修改主机名:即修改/etc/sysconfig/HOSTNAME。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8470507.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-16
下一篇 2023-04-16

发表评论

登录后才能评论

评论列表(0条)

保存