網路管理
Tutorials
ip
# 顯示所有網卡資訊
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 192.168.1.200/255.255.255.0 dev eth0
# 移除 IP (非永久)
ip a del 192.168.1.200/255.255.255.0 dev eth0
# 顯示 default gateway 及路由表
ip route show
ip r show
ip route add 10.10.20.0/24 via 192.168.50.100 dev eth0
ip route del 10.10.20.0/24
# Default gateway
ip route add default via 192.168.50.100
# 網路即時狀態
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
在多個網路埠的主機上,如何得知哪些埠有接上網路線
# 1. 列出所有網路埠
ip link show 如果埠號顯示 DOWN 必須先啟動
# 2. 啟動網路埠 NOTE: 啟動前要注意 IP 是否會衝突
ip link set eth6 up
# 3. 啟動後檢測線路
ethtool eth6 | grep detected
nmcli
# 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 192.168.20.170/24
nmcli con mod enp0s3 ipv4.gateway 192.168.20.1
nmcli con mod enp0s3 ipv4.method manual
nmcli con mod enp0s3 ipv4.dns "8.8.8.8"
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 192.168.1.50/24 gw4 192.168.1.1
cat /etc/sysconfig/network-scripts/ifcfg-Myhome1
GUI to Configure Network
nmtui
Ubuntu/Debian Configure Network
sudo vi /etc/network/interfaces
auto eth0
iface eth0 inet static
address 10.1.1.201
netmask 255.255.255.0
network 10.1.1.0
broadcast 10.1.1.255
gateway 10.1.1.254
dns-nameservers 10.1.1.254
dns-search my.domain.com
nc - netcat
# Scanning the port range (20 - 1024)
nc -z 192.168.21.202 20-1024
Connection to 192.168.21.202 22 port [tcp/ssh] succeeded!
Connection to 192.168.21.202 80 port [tcp/http] succeeded!
Connection to 192.168.21.202 111 port [tcp/sunrpc] succeeded!
Connection to 192.168.21.202 443 port [tcp/https] succeeded!
Connection to 192.168.21.202 514 port [tcp/shell] succeeded!
# Scanning the specified port
nc -zv 192.168.21.202 21
nc: connect to 192.168.21.202 port 21 (tcp) failed: Connection refused
# Port Scanning With netcat including displaying version #
echo "QUIT" | nc 192.168.2.17 22
echo "QUIT" | nc -v 192.168.2.254 ssh
# OR pass the -vv to get remote OpenSSH version #
nc -vv 192.168.2.254 ssh
nc : 在不同的 Linux 主機上傳輸檔案
# Install nc and pv
yum install netcat pv
# Machine A with IP : 192.168.0.4
# Machine B with IP : 192.168.0.7
# 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 192.168.1.4 5555 | pv | tar -zxf -
mii-tool
# Installation
sudo apt install net-tools
# CHECK A SINGLE INTERFACE
sudo mii-tool <interface_name>
# SEE DETAILED INFORMATION
sudo mii-tool -v <interface_name>
# SET NETWORK INTERFACE SPEED
sudo mii-tool –force 10baseT-FD <interface_name>
# RESTART AUTO-NEGOTIATION
# 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>
# CHANGE THE DUPLEX MODE
# 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
# REPORT LINK STATUS CHANGES
# 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>
# REPORT LINK STATUS
sudo mii-tool -l <interface_name>
# RESET THE CONFIGURATIONS
# 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>
systemctl
# Bringing UP/Down Network Interface
systemctl restart network
# or
systemctl restart network.service
speedtest CLI
# Ubuntu/Debian
curl -s https://install.speedtest.net/app/cli/install.deb.sh | sudo bash
sudo apt-get install speedtest
# CentOS/RedHat
curl -s https://install.speedtest.net/app/cli/install.rpm.sh | 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
GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1"
# 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 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