1.获取所有有(无)网卡型号
方法一、ifconfig
用法: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
用法: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 (推荐)
用法:ls /sys/class/net | grep <network_interface>
root@ubuntu:~# ls /sys/class/net | grep enp
enp2s0
enp3s0
2.获取有(无)线网卡对应 mac 地址
用法:cat /sys/class/net/<network_interface>/address
root@ubuntu:~# cat /sys/class/net/enp2s0/address
f4:8e:38:ad:34:6d
3.获取有(无)线网卡对应的 IP 地址
用法: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
4.补充:查看某个网卡信息
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
5.修改 IP 地址
Ubuntu
有线
#传参顺序: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/
下,直接修改这里的文件是最直接有效的。
centos
有线
6.搜索无线网名称
用法: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
7.无线设备的开启和关闭(临时,重启失效)
1.开启/关闭飞行模式
rfkill block/unblock all
2.开启/关闭某个无线设备
rfkill block/unblock <interface>
interface 包括:0 ,1, all,bluetooth,gps,uwb,wifi,wimax,wwan
3.显示当前机器的无线设备状态
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
8.查看无线网卡连接的网络信息
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
9.禁用/启用有线网卡(临时,重启失效)
ifconfig <network_interface> down/up
说明:无线设备的开启和关闭,使用 rfkill 命令,用 ifconfig 命令不管用
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于