Skip to main content




# 顯示所有網卡資訊
ip addr
ip a

# 顯示所有網卡的 IP
ip -br -c addr show  # 需要較新版 ip

# 顯示 eth0 網卡資訊
ip a show eth0 

# 開啟/關閉網卡
ip link set eth0 { up | down }

# 顯示所有的網路裝置
ip link show
ip -br -c link show
ip l show

# 設定 IP (非永久)
ip a add dev eth0

# 移除 IP (非永久)
ip a del dev eth0

# 顯示 default gateway 及路由表
ip route show
ip r show
ip route add via dev eth0
ip route del

# Default gateway
ip route add default via

# 網路即時狀態
ip -s link 

# 顯示 ARP 紀錄 (NOTE: 查詢連接網路設備的 MAC address 與 IP 對應表)
ip neigh show
ip n show

# 清除 ARP 清單裡的某個 IP 紀錄
ip -s -s n f <ip-address>

# 線上求助
ip a help



# List all of ethernet devices
nmcli con show
nmcli con show <conn-name>
nmcli dev status
# see only the active connections
nmcli con show -a

# Restart the network adapter enp0s3
nmcli con down enp0s3 && nmcli con up enp0s3

# Configure the static ip
nmcli con mod enp0s3 ipv4.addresses
nmcli con mod enp0s3 ipv4.gateway
nmcli con mod enp0s3 ipv4.method manual
nmcli con mod enp0s3 ipv4.dns ""

nmcli con down enp0s3
nmcli con up enp0s3 

# make a new ethernet connection with name Myhome1, assigned to device enp0s3
nmcli con add type ethernet con-name Myhome1 ifname enp0s3 ip4 gw4
cat /etc/sysconfig/network-scripts/ifcfg-Myhome1

GUI to Configure Network


Ubuntu/Debian Configure Network

sudo vi /etc/network/interfaces

auto eth0
iface eth0 inet static

nc - netcat

# Scanning the port range (20 - 1024)
nc -z 20-1024

Connection to 22 port [tcp/ssh] succeeded!
Connection to 80 port [tcp/http] succeeded!
Connection to 111 port [tcp/sunrpc] succeeded!
Connection to 443 port [tcp/https] succeeded!
Connection to 514 port [tcp/shell] succeeded!

# Scanning the specified port
nc -zv 21
nc: connect to port 21 (tcp) failed: Connection refused

# Port Scanning With netcat including displaying version #
echo "QUIT" | nc 22
echo "QUIT" | nc -v ssh
# OR pass the -vv  to get remote OpenSSH version # 
nc -vv ssh

nc : 在不同的 Linux 主機上傳輸檔案

# Install nc and pv
yum install netcat pv

# Machine A with IP :
# Machine B with IP :
# On Linux Machine A
# [*] tar -zcf = tar is a tape archive utility used to compress/uncompress archive files 
#     and arguments -c creates a new .tar archive file, -f specify type of the archive file 
#     and -z filter archive through gzip.
# [*] CentOS-7-x86_64-DVD-1503.iso = Specify the file name to send over network, it can be file 
#     or path to a directory.
# [*] pv = Pipe Viewer to monitor progress of data.
# [*] nc -l -p 5555 -q 5 = Networking tool used for send and receive data over tcp 
#     and arguments -l used to listen for an incoming connection, -p 555 specifies the source port 
#     to use and -q 5 waits the number of seconds and then quit.
tar -zcf - CentOS-7-x86_64-DVD-1503.iso | pv | nc -l -p 5555 -q 5

# On Linux Machine B
nc 5555 | pv | tar -zxf -


# Installation 
sudo apt install net-tools

sudo mii-tool <interface_name>

sudo mii-tool -v <interface_name>

sudo mii-tool –force 10baseT-FD <interface_name>

# Network devices use an auto-negotiation protocol to communicate the technologies they support. 
# It will then select the fastest mutually supported technology. 
# To restart the auto-negotiation of the interface, run the following command.
sudo mii-tool –restart <interface_name>

# For example, here I have set the speed of the eth0 interface to 10 Mbps and the duplex mode to half-duplex.
sudo mii-tool -F 10baseT-HD eth0

# Run the following command to watch a single interface and report changes in the link status. 
# That is to say, the interfaces are listed at one second intervals by default.
sudo mii-tool -w <interface>

sudo mii-tool -l <interface_name>

# Most importantly, you should be able to reset it to its default configuration 
# if something goes wrong. For that, run the following command
sudo mii-tool -R <Interface_name>


# Bringing UP/Down Network Interface
systemctl restart network
# or
systemctl restart network.service

speedtest CLI

# Ubuntu/Debian
curl -s | sudo bash
sudo apt-get install speedtest

# CentOS/RedHat
curl -s | sudo bash
sudo yum install speedtest

State of Network Cable

# Device: enp5s0
# Output: 1 means Connected
cat /sys/class/net/enp5s0/carrier
# Output: Up  means Connected
cat /sys/class/net/enp5s0/operstate

# Using ethtool
# Output: Link detected: yes
sudo ethtool enp5s0

# Using ip
# Output: state UP
ip a

Disable IPv6

Ubuntu 20.04
sudo vi /etc/default/grub

# Change the line as follows

# Update the GRUB
sudo update-grub

# Reboot
systemctl reboot
Debian 10

/etc/sysctl.conf :

# Disable IPv6 on all network adapters
net.ipv6.conf.all.disable_ipv6 = 1

Apply the change :

sysctl -p
RedHat 7


# To disable for all interfaces
net.ipv6.conf.all.disable_ipv6 = 1


如果沒有重建開機映像檔,會使得 rpcbind.service 無法正常運作,這會影響 NFS 的掛載。

RedHat 8

Create the file /etc/sysctl.d/ipv6.conf :

# First, disable for all interfaces
net.ipv6.conf.all.disable_ipv6 = 1
# If using the sysctl method, the protocol must be disabled all specific interfaces as well. 
#net.ipv6.conf.<interface>.disable_ipv6 = 1

Reload sysctl :

sysctl -p /etc/sysctl.d/ipv6.conf

Create a backup of the initramfs :

cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).bak.$(date +%m-%d-%H%M%S).img

Rebuild the Initial RAM Disk Image :

dracut -f -v

Verifying file inclusion :

lsinitrd /boot/initramfs-<version>.img  | grep 'etc/sysctl.d/ipv6.conf'

Comment out any IPv6 addresses found in /etc/hosts, including ::1 localhost address

cp -p /etc/hosts /etc/hosts.disableipv6
sed -i 's/^[[:space:]]*::/#::/' /etc/hosts

WiFi Management

# Replace 'wlan0' with your wifi interface
sudo iwlist wlan0 scan | egrep "Cell|ESSID|Encryption|Quality"

Block Attackers IP Address

Drop or Block Attackers IP Address With Null Routes On a Linux

# Using route command
route add gw lo
# veryfy it
netstat -nr
route -n
# Or
route add -host reject
ip route get

# Using ip command
ip route add blackhole
ip route add blackhole
# verify it
ip route

# Removing null routing
route delete
# Or
route del -host reject
# Or
ip route delete dev eth0


製作 Linux VM Template 時,每一次修改 Template 後都會產生新的編號作為網路裝置名稱。

然而透過這個 Template 新增的 Linux VM,系統的網路介面其實只有一個,不過裝置名稱可能已經編到 eth1 或 eth2 以後。正常來說,系統如果只有一個網路介面,網路裝置名稱通常為 eth0。

假使想要清除那些已經不存在的裝置名稱,或者讓系統對目前的網路裝置重新以 eth0 開始編號,步驟如下:

RedHat 6.x: 編輯 /etc/udev/rules.d/70-persistent-net.rules 

# PCI device 0x15ad:0x07b0 (vmxnet3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:83:7c:eb", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x15ad:0x07b0 (vmxnet3) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:83:7c:eb", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

註解或移除那些舊裝置名稱,只保留目前的裝置 eth1,然後將該行的 NAME 改成 eth0。

# PCI device 0x15ad:0x07b0 (vmxnet3) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:83:7c:eb", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

存檔後重起 VM。

VM 啟動後,使用 setup 或 system-config-network 新增網路介面 eth0 的網路設定。