用法:ifconfig | grep <network_interface> | cut -d ':' -f 1
ubuntu
root@ubuntu:~# ifconfig | grep enp| cut -d ':' -f 1
enp2s0
enp3s0
centos
[root@centos ~]# ifconfig | grep ens| cut -d ':' -f 1
ens192
注意:ubuntu下有线网卡基本以
enp
开头,centos以ens
开头,无线网卡下ubuntu以wlp
开头
用法:ip a | grep <network_interface> | cut -d ':' -f 2 | awk '{print $1}' | grep <network_interface>
ubuntu
root@ubuntu:~# ip a | grep enp | cut -d ':' -f 2 | awk '{print $1}' | grep enp
enp2s0
enp3s0
centos
[root@centos ~]# ip a | grep ens | cut -d ':' -f 2 | awk '{print $1}' | grep ens
ens192
用法:ls /sys/class/net | grep <network_interface>
root@ubuntu:~# ls /sys/class/net | grep enp
enp2s0
enp3s0
用法:cat /sys/class/net/<network_interface>/address
root@ubuntu:~# cat /sys/class/net/enp2s0/address
f4:8e:38:ad:34:6d
用法:ifconfig <network_interface> | grep inet | grep -v inet6 | sed 's/^[ \t]*//g' | cut -d ' ' -f 2| awk '{print $1}'
root@ubuntu:~# ifconfig enp2s0 | grep inet | grep -v inet6 | sed 's/^[ \t]*//g' | cut -d ' ' -f 2| awk '{print $1}'
192.168.81.208
root@ubutun:~# ifconfig enp2s0
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.81.208 netmask 255.255.254.0 broadcast 192.168.81.255
inet6 fe80::c0b1:bc04:43a5:9eab prefixlen 64 scopeid 0x20<link>
ether f4:8e:38:ad:34:6d txqueuelen 1000 (以太网)
RX packets 39571544 bytes 5640943675 (5.6 GB)
RX errors 0 dropped 1989163 overruns 0 frame 0
TX packets 45318631 bytes 36700361403 (36.7 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@ubuntu:~# ip a s enp2s0
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether f4:8e:38:ad:34:6d brd ff:ff:ff:ff:ff:ff
inet 192.168.81.208/23 brd 192.168.81.255 scope global noprefixroute enp2s0
valid_lft forever preferred_lft forever
inet 192.168.81.111/32 scope global enp2s0
valid_lft forever preferred_lft forever
inet6 fe80::c0b1:bc04:43a5:9eab/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#传参顺序:IP地址 子网掩码 网关 dns 有线网卡设备号
uuid=$(uuidgen)
address=${1:-"192.168.81.209"}
netmask=${2:-"23"}
gateway=${3:-"192.168.80.1"}
dns=${4:-"192.168.3.230"}
interface=${5:-"enp1s0"}
#时间戳
timestamp=`date +%s`
mac_address=`cat /sys/class/net/${interface}/address`
#将mac地址转化为大写
typeset -u mac
mac=${mac_address}
en="Wired connection 1"
cn="有线连接 1"
network=""
#获取语言
language=`cat /etc/default/locale | grep "LANG=" | awk '{print $1}'`
zh_cn="zh_CN"
en_us="en_US"
if [[ ${language} =~ ${zh_cn} ]];then
echo "系统语言为中文"
network=${cn}
else
echo "系统语言为英文"
network=${en}
fi
cat <<EOF >/etc/NetworkManager/system-connections/"${network}"
[connection]
id=${network}
uuid=${uuid}
type=ethernet
autoconnect-priority=-999
permissions=
timestamp=${timestamp}
[ethernet]
mac-address=${mac}
mac-address-blacklist=
[ipv4]
address1=${address}/${netmask},${gateway}
dns=${dns}
dns-search=
method=manual
[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto
EOF
#文件赋权
chmod 600 /etc/NetworkManager/system-connections/"${network}"
#IP临时生效,重启永久生效
#ifconfig ${interface} ${address}
echo -e "\033[1;32m 关闭网卡${interface} \033[0m"
ifconfig ${interface} down
sleep 10
echo -e "\033[1;32m 关闭网络管理服务 \033[0m"
systemctl stop network-manager
echo -e "\033[1;32m 开启网络管理服务 \033[0m"
systemctl start network-manager
echo -e "\033[1;32m 开启网卡${interface} \033[0m"
ifconfig ${interface} up
echo -e "\033[1;32m 查看修改后有线IP配置 \033[0m"
ip a
若有多个dns,请以
分号(;)
隔开
#传参顺序:wifi名称 wifi密码 网卡设备号 ip地址 子网掩码 网关 dns
uuid=$(uuidgen)
wifi_name=${1:-"MECDAPTAIN"}
wifi_psk=${2:-"MECT8888"}
interface=${3:-"wlp2s0"}
address=${4:-"192.168.81.209"}
netmask=${5:-"23"}
gateway=${6:-"192.168.80.1"}
dns=${7:-"192.168.3.230"}
#搜索到的无线网名称
#wifi_list=`iwlist ${interface} scan | grep ESSID |cut -d '"' -f 2`
mac_address=`cat /sys/class/net/${interface}/address`
#将mac地址转化为大写
typeset -u mac
mac=${mac_address}
cat <<EOF >/etc/NetworkManager/system-connections/${wifi_name}
[connection]
id=${wifi_name}
uuid=${uuid}
type=wifi
permissions=
[wifi]
mac-address=${mac}
mac-address-blacklist=
mode=infrastructure
ssid=${wifi_name}
[wifi-security]
key-mgmt=wpa-psk
psk=${wifi_psk}
[ipv4]
address1=${address}/${netmask},${gateway}
dns=${dns}
dns-search=
method=manual
[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto
EOF
echo -e "\033[1;31m 赋予配置文件600权限 \033[0m"
chmod 600 /etc/NetworkManager/system-connections/${wifi_name}
#IP临时生效,重启永久生效
#ifconfig ${interface} ${address}
echo -e "\033[1;32m 关闭网卡${interface} \033[0m"
ifconfig ${interface} down
sleep 10
echo -e "\033[1;32m 关闭网络管理服务 \033[0m"
systemctl stop network-manager
echo -e "\033[1;32m 开启网络管理服务 \033[0m"
systemctl start network-manager
echo -e "\033[1;32m 开启网卡${interface} \033[0m"
ifconfig ${interface} up
echo -e "\033[1;32m 查看修改后有线IP配置 \033[0m"
ip a
若有多个dns,请以
分号(;)
隔开
Ubuntu的网络配置文件都在
/etc/NetworkManager/system-connections/
下,直接修改这里的文件是最直接有效的。
#!/usr/bin/env bash
#传参顺序:IP地址 子网掩码 网关 dns1 dns2 有线网卡设备号
address=${1:-"192.168.81.209"}
netmask=${2:-"23"}
gateway=${3:-"192.168.80.1"}
dns1=${4:-"192.168.3.230"}
dns2=${5:-"192.168.3.231"}
interface=${6:-"ens33"}
uuid=$(uuidgen)
cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-${interface}
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="ens192"
UUID="${uuid}"
DEVICE="${interface}"
ONBOOT="yes"
IPADDR="${address}"
PREFIX="${netmask}"
GATEWAY="${gateway}"
IPV6_PRIVACY="no"
DNS1="${dns1}"
DNS2="${dns2}"
EOF
echo -e "\033[1;32m 重启网络服务 \033[0m"
systemctl restart network
echo -e "\033[1;32m 查看网络服务状态 \033[0m"
systemctl status network
echo -e '\033[1;32m 查看修改后的IP地址 \033[0m'
ip a
exit
centos的网络配置文件都在
/etc/sysconfig/network-scripts
下
以上脚本都可以改写为控制台输入的方式变成通用脚本。
用法:iwlist <network_interface> scan(scanning) | grep ESSID |cut -d '"' -f 2
或:iw dev <network_interface> scan | grep SSID |cut -d ':' -f 2 | awk '{print $1}'
root@ubuntu:~# iwlist wlp2s0 scan | grep ESSID |cut -d '"' -f 2
HP-Print-04-LaserJet Pro MFP
MikroTik-768962
ChinaNet-DDNw
i-Nanjing-Free
SGXY-Office
SGXY-Office
iTV-DDNw
ChinaNet-xHwF
ChinaNet-CaDp
ChinaNet-XZws
ChinaNet-VuPW
iTV-VuPW
i-Nanjing-Free
i-Nanjing-Free
DIRECT-PODESKTOP-CS6BIUPmsHE
i-Nanjing-Free
i-Nanjing-Free
DIRECT-67-HP M281 LaserJet
GNWS
i-Nanjing-Free
i-Nanjing-Free
TP-LINK_nanorainbow
i-Nanjing-Free
HP-Print-9B-Color LaserJet Pro
i-Nanjing-Free
i-Nanjing-Free
i-Nanjing-Free
TP-LINK_8303
i-Nanjing-Free
rfkill block/unblock all
rfkill block/unblock <interface>
interface包括:0 ,1, all,bluetooth,gps,uwb,wifi,wimax,wwan
rfkill list
如:
root@ubuntu:~# rfkill list
0: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
iwconfig <network_interface>
ip19@ubuntu:~$ iwconfig wlp2s0
wlp2s0 IEEE 802.11 ESSID:"MEDCAPTAIN_2.4G"
Mode:Managed Frequency:2.412 GHz Access Point: 0C:4B:54:FE:27:F2
Bit Rate=150 Mb/s Tx-Power=20 dBm
Retry short limit:7 RTS thr=2347 B Fragment thr:off
Power Management:on
Link Quality=70/70 Signal level=-26 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:5 Missed beacon:0
ifconfig <network_interface> down/up
说明:无线设备的开启和关闭,使用rfkill命令,用ifconfig命令不管用
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:Linux获取网卡型号、mac地址、修改IP地址的几种方法