linux网卡ip地址
Ⅰ 获取linux IP地址的六种方法总结
本文总结六种查看Linux IP地址的方法,方便以后的运维开发工作。
在介绍前先学习一下三个命令行筛选的主要的指令,也是频繁使用到的命令。
1、head。 head 命令可用于查看文件的开头部分的内容,有一个常用的参数 -n 用于显示行数,默认为 10。
运行head --help查看说明信息:
-q 隐藏文件名
-v 显示文件名
-c<数目> 显示的字节数。
-n<行数> 显示的行数。
2、grep。 grep 命令用于查找文件里符合条件的字符串。运行grep --help查看说明信息,参数太多主要有以下几种:
grep -r递归选择。
grep -v反选,显示不包含匹配文本的所有行。
grep -n显示符合样式的那一行之前。
grep -A显示符合范本样式的那一列之外,并显示该行之后的内容。
3、awk。 强大的文本分析工具,命令使用过于复杂(awk --help),只需要知道 awk '{print$2}'为打印第二行数据。
4、tail 。tail命令可用于查看文件的结束部分的内容,有一个常用的参数 -n 用于显示行数,默认为 10。tail --help查看主要的参数:
tail -n显示最后多少行
tail -c显示最后十个字符
tail -f 循环读取,跟踪显示最后十行
5、cut。 显示每行从开头算起的文字。
cut -b :以字节为单位进行分割。
cut -c :以字符为单位进行分割
cut -d :自定义分隔符,默认为制表符
cut -f :与-d一起使用,指定显示哪个区域
无线网卡地址:
echo wlan0=`ifconfig wlan0 | head -n2 | grep inet | awk '{print$2}'`
有线网卡地址:
echo eth0=`ifconfig eth0 | head -n2 | grep inet | awk '{print$2}'`
或者命令:
ifconfig | grep "inet " | cut -d: -f2 | awk '{print $1}' | grep -v "^127."
无线网卡地址:
ip address | grep wlan0 | awk '{print$2}'
有线网卡地址:
ip address | grep eth0 | awk '{print$2}'
或者
echo eth0=`ip address show eth0 | head -n4 | grep inet | awk '{print$2}'
echo wlan0=`ip address show wlan0 | head -n4 | grep inet | awk '{print$2}'
运行hostname -help命令查看说明信息:
Program options:
-a, --alias alias names
-A, --all-fqdns all long host names (FQDNs)
-b, --boot set default hostname if none available
-d, --domain DNS domain name
-f, --fqdn, --long long host name (FQDN)
-F, --file read host name or NIS domain name from given file
-i, --ip-address addresses for the host name
-I, --all-ip-addresses all addresses for the host
-s, --short short host name
-y, --yp, --nis NIS/YP domain name
hostname -i得到环回地址127.0.1.1, hostname -I得到具体的网卡信息192.168.31.82 。
php语言查看ip就是使用函数shell_exec来执行shell命令。
比如:
<?php
echo shell_exec("echo wlan0=`ifconfig wlan0 | head -n2 | grep inet | awk '{print$2}'`");
?>
然后执行php ip.php 。shell_exec()里面可以放置任何shell命令。这个方法的意义在于php可以通过网页对外提供服务。
#!/usr/bin/env python
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
Local_wlan0=get_ip_address("wlan0")
Local_lo=get_ip_address("lo")
#Local_eth0=get_ip_address("eth0")
print Local_wlan0
print Local_lo
#print Local_eth0
利用socket包,然后执行python ip.py 得到wlan0信息。
#!/usr/bin/env python
import os
def get_ip():
out = os.popen("echo wlan0=`ifconfig wlan0 | head -n2 | grep inet | awk '{print$2}'`").read()
print out
if __name__ == '__main__':
get_ip()
和php的shell_exec函数类似,os.popen()里面可以放置任何shell命令。注意有个函数os.system的结果只是命令执行结果的返回值,执行成功为0;os.popen()可以读出执行的内容,输出的结果比较特殊,带换行符\n 。
Ⅱ Linux配置ip地址的两种方法
Linux配置ip地址的两种方法,实验环境为centos7.6
方法1:nmcli工具配置 (centos7以下版本不支持该方法)
第一步,通过nmcli connection查看网卡名称
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
eth0 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 addr
1: 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 forever
2: 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:ff
inet 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..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..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
更多网络技术公开课,腾讯课堂搜“ 众元教育 ”,戳 免费公开课 ,可直接听课哦!
Ⅲ linux 怎么改网卡ip地址
修改debian的ip地址需要修改配置文件/etc/network/interfaces。
如果是设置成dhcp自动获取ip地址,在配置文件中加入这样的内容:
auto
eth0
iface
eth0
inet
dhcp
然后保存退出,需要重启网络(不是重启主机)使配置生效,重启网络命令:
/etc/init.d/networking
restart
重启网络后,执行dhclient
eth0命令就可以获取ip地址了。
注意:配置文件里的eth0是网卡的名字,需要你自己主机上的网卡名字(无线网卡用iwconfig可以看到,一般有线网卡用ifcongfig可以看到)。
auto
eth0这一句表示开机网卡设置就生效。
如果是设置成静态ip地址,需要写入以下内容:
auto
eth0
iface
eth0
inet
static
address
这里是静态ip
netmask
这里是子网掩码
gateway
这里是网关地址
network
这里是所在网段的网络地址
broadcast
这里是所在网段的广播地址
保存退出后也要重启网络。
Ⅳ 安装linux配置网卡IP
1.你先要检查这个网络接口是不是存在ifconfig
-a
2.你可以参考下面朋友做法,改写配置文件.
3.你也可以使用SETUP里面有个网络设置
4.
实例一:
比如我们用ifconfig
来调试
eth0网卡的地址
[root@linuxchao
~]#ifconfig
eth0
down
[root@linuxchao
~]#ifconfig
eth0
192.168.1.99
broadcast
192.168.1.255
netmask
255.255.255.0
[root@linuxchao
~]#ifconfig
eth0
up
[root@linuxchao
~]#ifconfig
eth0
Ⅳ linux下如何查看某个网卡当前使用的IP。
1、连接上相应的linux主机,进入到等待输入shell指令的linux命令行状态下。
Ⅵ linux如何查看ip地址命令(linux如何查看ip地址)
1、linux如何查看ip地址命令。
2、linux如何查看ip地址和端口。
3、linux如何查看ip地址,网关和掩码。
4、linux如何查看ip地址 python。
1."linux如何查看ip地址,为您提供linux如何查看ip地址图文信息,打开linux系统,在linux的桌面的空白处右击。
2.在弹出的下拉选项里,点击打开终端。
3.在打开的Linux服务器终端中用查询ip命令查找。
4.linux查看ip地址的命令为ifconfig-a,在命令窗口输入然后按回车。
5.按下回车后就会在终端命令窗口里显示出ip地址。
Ⅶ Linux查询IP地址
Ⅷ linux怎么查看IP地址
查看IP是ifconfig(ifconfig -a是显示全部接口)
示例结果如下:
eth0 Link encap:Ethernet HWaddr bb:bb:bb:bb:bb:bb
inet addr:192.168.1.163 Bcast:192.168.1.255 Mask:255.255.255.0
eth0 表示第一块以太网网卡, 其中 HWaddr 表示网卡的物理地址(MAC),
inet addr 用来表示网卡的IP地址,此网卡的 IP地址是 192.168.1.163,广播地址, Bcast:192.168.120.255,掩码地址Mask:255.255.255.0。
Ⅸ linux设置网卡ip
linux设置ip可以有多种 可以在图形界面配置 可以临时指定 ifconfig ens33 172.16.1.1 也可以在配置文件指定 /etc/sysconfig/network-scripts/ifcfg-eth0 具体可以看看《linux就该这么学》
Ⅹ linux怎么配置网卡的ip地址
1
在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0
2
开始编辑,填写ip地址、子网掩码、网关、DNS等。
DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为eth0
BOOTPROTO=static #设置网卡获得ip地址的方式,可能的选项为static,dhcp或bootp,分别对应静态指定的 ip地址,通过dhcp协议获得的ip地址,通过bootp协议获得的ip地址
BROADCAST=192.168.0.255 #对应的子网广播地址
HWADDR=00:07:E9:05:E8:B4 #对应的网卡物理地址
IPADDR=192.168.0.2 #如果设置网卡获得 ip地址的方式为静态指定,此字段就指定了网卡对应的ip地址
IPV6INIT=no
IPV6_AUTOCONF=no
NETMASK=255.255.255.0 #网卡对应的网络掩码
NETWORK=192.168.0.0 #网卡对应的网络地址
ONBOOT=yes #系统启动时是否设置此网络接口,设置为yes时,系统启动时激活此设备
3
编辑完后,保存退出。
4
重启网络服务。service network restart或/etc/init.d/network restart
5
ping网关,ping外网进行测试。都能ping通表示网络正常。