ip Command: Manage IP Addresses, Network Devices, Routing Tables, and Network Namespaces

Overview

The ip command in Linux is a powerful utility for managing IP addresses, network interfaces, routing tables, and network namespaces. It provides granular control over various network configurations, making it an essential tool for system administrators.

Syntax

ip addr add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]
ip route list|flush|add|del|change|append|replace|test ROUTE
ip link set IFACE [up|down] [arp on|off] [multicast on|off] \
        [promisc on|off] [mtu NUM] [name NAME] [qlen NUM] [address MAC] \
        [master IFACE | nomaster] [netns PID]
ip tunnel add|change|del|show [NAME] \
        [mode ipip|gre|sit] [remote ADDR] [local ADDR] [ttl TTL]
ip neigh show|flush [to PREFIX] [dev DEV] [nud STATE]
ip rule [list] | add|del SELECTOR ACTION

Key Features and Examples

Manage IP Addresses

View IP addresses:

ip addr

Add an IP address to a device:

ip addr add 192.168.1.1/24 dev eth0

Remove an IP address:

ip addr del 192.168.1.1/24 dev eth0

Flush all IP addresses for a device:

ip addr flush dev eth0

Manage Network Devices

Display all network interfaces:

ip link

Show specific device details:

ip link show dev eth0

Bring an interface up or down:

ip link set eth0 up
ip link set eth0 down

Set MTU for a device:

ip link set eth0 mtu 1450

Add or delete a bridge device:

ip link add br0 type bridge
ip link del br0

Create a virtual Ethernet pair:

ip link add veth0 type veth peer name veth1

Manage Routing Tables

Display the main routing table:

ip route show table main

Add a default route:

ip route add default via 10.8.1.1 dev eth0

Add a static route:

ip route add 10.8.1.0/24 via 10.8.1.1

Replace an existing route:

ip route replace 10.8.1.0/24 dev eth0

Flush the routing table:

ip route flush cache

Manage Routing Rules

Show routing rules:

ip rule

Add a rule to use a specific table:

ip rule add from 10.8.1.0/24 table 520

Blackhole traffic from a specific source:

ip rule add from 0/0 blackhole

Manage Network Namespaces
List network namespaces:

ip netns

Create a network namespace:

ip netns add s1

Delete a namespace:

ip netns del s1

Execute a command within a namespace:

ip netns exec s1 ip addr

Example: Setting MTU

MTU (Maximum Transmission Unit) defines the maximum packet size for data transmission on a network device. Adjusting MTU impacts performance and compatibility.

View the current MTU for eth0:

ip link show eth0

Set MTU to 1450 bytes:

ip link set eth0 mtu 1450

Common MTU Values:

  • Ethernet: 1500 bytes (default)
  • PPPoE: 1492 bytes (8 bytes for headers)
  • Wi-Fi: Often 1500 bytes but can vary slightly.

Quick Tips with ip

Color-coded output:

ip -c addr
ip -c route

Compact display:

ip -brief link
ip -brief addr

For detailed configuration guides, check the official documentation.

ip命令管理IP地址、网络设备、路由表和路由规则、网络命名空间

功能说明:在Linux系统中,ip命令用于管理IP地址、网络设备、路由表和路由规则、网络命名空间。

语  法:

ip addr add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]

ip route list|flush|add|del|change|append|replace|test ROUTE

ip link set IFACE [up|down] [arp on|off] [multicast on|off]

        [promisc on|off] [mtu NUM] [name NAME] [qlen NUM] [address MAC]

        [master IFACE | nomaster] [netns PID]

ip tunnel add|change|del|show [NAME]

        [mode ipip|gre|sit] [remote ADDR] [local ADDR] [ttl TTL]

ip neigh show|flush [to PREFIX] [dev DEV] [nud STATE]

ip rule [list] | add|del SELECTOR ACTION

参  数:

# 辅助参数

ip -n s1 addr 在网络命名空间中执行 ip addr

ip -c link 彩色显示接口

ip -c addr 彩色显示IP地址

ip -c route 彩色显示路由

ip -brief link 简洁显示接口

ip -brief addr 简洁显示地址

ip -brief -c link 彩色简洁显示接口

# ip addr管理IP地址

ip addr 查看IP地址

ip addr add 192.168.1.1/24 dev eth0 给指定设备添加IP地址

ip addr del 192.168.1.1/24 dev eth0 删除指定设备的地址

ip addr flush dev eth0 删除指定设备的所有IP地址

# ip link管理网络设备

ip link 显示所有网络接口

ip link show dev eth0 显示指定设备的信息

ip link set eth0 up 把 eth0 接口设备开启

ip link set eth0 down 把 eth0 接口设备关闭

ip link set eth0 mtu 1450 设置设备的 MTU

ip link add br0 type bridge 添加一个网桥设备

ip link del br0 删除一个网桥设备

ip link set eth0 master br0 把 eth0 添加到网桥 br0

ip link set eth0 nomaster 从网桥中删除 eth0

ip link add veth0 type veth peer name veth1 添加虚拟以太网设备

ip link set veth0 netns s1 把 veth0 移动到网络命名空间

ip link set veth1 netns s2 把 veth1 移动到网络命名空间

ip link set veth0 netns 1 把设备移动到全局网络命名空间

# ip route管理路由表

ip route 显示主路由表

ip route show table [local|main|default|num] 显示路由表

ip route show table main 显示主路由表

ip route show table 520 显示编号为 520 的路由表

ip route get 8.8.8.8 查询一个地址经过的路由

ip route get 8.8.8.8 mark 666 查询经过的路由(带标记)

ip route add default via 10.8.1.1 dev eth0 添加默认路由

ip route add 10.8.1.0/24 via 10.8.1.1 添加静态路由

ip route add 10.8.1.0/24 dev eth0 添加直连路由

ip route add 10.8.1.0/24 dev eth0 metric 10 添加带有 metric 的直连路由

ip route add 10.8.1.0/24 dev eth0 table 520 添加路由到编号 520 的路由表

ip route add table 520 10.8.1.0/24 dev eth0 另一种写法,突出表名

ip route delete 10.8.1.0/24 via 10.8.1.1 删除静态路由

ip route replace 10.8.1.0/24 dev eth0 替换路由

ip route flush cache 路由表立即生效

ip route flush table 520 清空编号为 520 的路由表

# 显示当前定义了哪些路由表

ip route show table all | grep -Eo ‘table [^ ]+ ‘ | sort | uniq

# ip rule管理路由规则

ip rule 显示路由规则

ip rule add table 520 所有包走一下 520 路由表

ip rule add from 0/0 lookup 520 所有包走一下 520 路由表

ip rule add from 0/0 table 520 同上

ip rule add from 0/0 blackhole 所有包丢弃

ip rule add from 0/0 prohibit 所有包拒绝,通信被管理员禁止

ip rule add from 0/0 unreachable 返回 network unreachable

ip rule del table 520 删除所有包走 520 路由表的规则

ip rule add from 10.8.1.0/24 table 520 来自特定网络的包走 520 路由表

ip rule add to 10.8.2.0/24 table 521 发往某网络的包走 521 路由表

ip rule add fwmark 588 table 520 标记为 588 的包走 520 路由表

ip rule add not fwmark 588 table 51820 没有标记为 588 的包走该路由表

ip rule add from 8.8.3.2/32 tos 10 table 2 来自特定 IP 且 TOS 为 10 的包

ip rule add prio 100 fwmark 1 lookup 100 优先级 100 的规则

# ip netns管理网络命名空间

ip netns 显示网络命名空间

ip netns add s1 创建一个网络命名空间

ip netns del s1 删除一个网络命名空间

ip netns attach NAME PID 改变进程网络命名空间

ip netns exec s1 command 在网络命名空间中执行命令

ip netns exec s1 ip link set lo up 在网络命名空间中设置 lo 设备

ip netns identify 查看当前进程的网络命名空间

ip netns identify PID 查看指定进程的网络命名空间

ip netns pids NAME 查看网络命名空间中的进程

ip -n s1 addr add 192.168.64.1/24 dev veth0 在网络命名空间中添加地址

   例:

1 设置MTU

MTU(Maximum Transmission Unit)是最大传输单元的缩写,表示网络中单个数据包在传输过程中能够承载的最大字节数。简单来说,MTU定义了在网络设备上(如网卡、路由器等)单次能够发送的最大数据量。

为什么MTU很重要?

  • 网络性能:较大的MTU可以提高网络传输效率,减少网络分片的发生。但如果MTU设置过大,可能导致某些网络设备无法处理,从而造成数据包丢失或传输失败。
  • 数据包分片:如果发送的数据包超过了MTU的限制,数据包就需要被分片,分片后每个小的数据包会分别发送到目标设备并重新组装。过多的分片会增加网络开销,降低性能。
  • 兼容性:不同的网络设备和协议可能对MTU有不同的要求,设置不当可能导致网络不兼容或丢包。

你可以使用以下命令来查看当前网络设备eth0的 MTU 设置:

ip link show eth0

设置网络设备 eth0 的 MTU 为 1450 字节:

ip link set eth0 mtu 1450

这意味着 eth0 网络接口上通过的每个数据包不能超过 1450 字节。如果某个应用程序尝试发送更大的数据包,网络协议栈就会将其拆分成多个小包进行发送。

常见网络的MTU值

  • 以太网(Ethernet):默认的MTU值通常是 1500 字节。
  • PPPoE(Point-to-Point Protocol over Ethernet):通常 MTU 为 1492 字节,因为额外的 8 字节用于标头。
  • Wi-Fi:通常与以太网相同,也为 1500 字节,但某些无线网络可能因为协议开销而略低。

参考

https://github.com/skywind3000/awesome-cheatsheets/blob/master/tools/ip.txt

新版本的Laravel没有必要使用Laravel Scaffold Generator作为代码生成器了

Laravel 5.3+ Scaffold Generator代码生成器能让你通过执行一条 Artisan 命令,完成注册路由、新建模型、新建表单验证类、新建资源控制器以及所需视图文件等任务,不仅约束了项目开发的风格,还能极大地提高我们的开发效率。

但是Laravel 10、11 等高版本,在使用 php artisan make:model 命令生成模型类的文件时,可以开启–all 或 -a 选项,来同时生成对应的迁移、工厂、填充器、策略、控制器和表单请求等文件:

php artisan make:model Flight –all
php artisan make:model Flight -a

所以,已经无需使用Laravel Scaffold Generator作为代码生成器了!

Dutch Auction: Applications and Use Cases

A Dutch auction, also known as a descending-price auction, is a widely used mechanism, particularly suited for scenarios requiring rapid transactions or the allocation of limited resources.

In a Dutch auction, the price of an item starts high and decreases over time. If multiple buyers are interested, the first buyer to stop the auction at an acceptable price secures the item. Buyers are incentivized to act quickly, as waiting too long risks losing the item to someone else. As a result, items rarely drop to zero or negative prices—unless no buyer values them.

Common Applications

  1. Flower Auctions
    The most iconic use of Dutch auctions is in the Netherlands’ flower markets. Flowers are perishable, so fast transactions are critical. In these auctions, the auctioneer starts with a high price and gradually lowers it until a buyer stops the process at their preferred price, ensuring a quick sale.
  2. Bond Issuance
    Governments and corporations sometimes use Dutch auctions to determine the price and allocation of bonds. Investors place purchase bids as prices drop, and all successful bidders pay a uniform price. This method prevents price inflation caused by competitive bidding.
  3. Electricity Markets
    Dutch auctions are used in electricity allocation markets, such as procuring backup power during high demand. Utility companies aim to secure sufficient electricity at the lowest possible cost.
  4. Agricultural Product Auctions
    Similar to flower auctions, Dutch auctions are used to sell perishable agricultural goods like fruits and vegetables. This ensures quick sales at market-accepted prices, minimizing waste.
  5. Online Advertising Bidding
    Real-Time Bidding (RTB) in online advertising platforms employs mechanisms akin to Dutch auctions. Ad slots start with high prices that decrease over time, allowing advertisers to claim slots when the price aligns with their budget.
  6. Inventory Clearance
    Retailers and second-hand markets use Dutch auctions to clear inventory or discounted goods. Prices begin high and drop progressively, encouraging buyers to act promptly to avoid losing out or delaying purchases.
  7. Financial Asset Liquidation
    In financial markets, Dutch auctions are employed to liquidate assets quickly, such as bankruptcy assets or secondary stock sales. The goal is to convert assets into cash at the highest feasible price.
  8. Art and Collectibles
    While rare in high-end art auctions, Dutch auctions are sometimes used for items with immediate demand, such as license plates or commemorative coins, to accelerate transactions.
  9. Spectrum Auctions
    Governments often use Dutch auction-like mechanisms to allocate wireless communication frequencies, balancing efficient allocation with market-driven pricing.
  10. Parking Allocation
    Some smart parking systems adopt Dutch auction principles during peak periods, lowering prices incrementally to attract drivers.

Key Advantage

The primary strength of Dutch auctions lies in their efficiency and speed. They are ideal for situations where rapid decision-making and flexible supply-demand dynamics are crucial. Whether for perishable goods, financial assets, or high-demand resources, Dutch auctions provide a streamlined solution for swift and fair transactions.

价格递减的拍卖(荷兰式拍卖)

价格递减的拍卖(又称荷兰式拍卖)在实际中有许多应用场景,尤其适用于快速完成交易或针对有限资源的分配。

价格递减拍卖某件物品,如果存在多个买家想要购买这件物品,那么当价格降低到一定程度时,一定会有其中一个最想要它的买家叫停买下它,因为这个买家如果不及时叫停拍下的话,就很有可能会被其他买家拍下。因此价格递减拍卖某件物品,很少会真正导致价格降到0甚至负数,除非大家都不想要这件物品。

以下是一些典型应用场景:

1. 花卉拍卖

荷兰式拍卖最著名的应用场景是在荷兰的花卉市场。花卉是易腐商品,必须快速交易,价格递减拍卖能够让买家迅速做出决策,避免延误。拍卖过程中,拍卖师会从高价开始逐步降低价格,买家在适合的价格点时喊停。

2. 债券发行

政府债券或公司债券有时使用荷兰式拍卖来确定价格和分配。投资者在降价过程中提交购买请求,最终成交价格是所有成功投标者的统一价格。这种方式避免了竞标者为争抢资源而提高价格的现象。

3. 电力市场

在一些国家的电力分配市场中,价格递减拍卖用于分配发电权或购买发电能力。例如,高需求时的备用电力采购,电力公司希望以最低价格购买足够的电量。

4. 农产品拍卖

对于易腐烂的农产品,如水果和蔬菜,荷兰式拍卖有助于快速确定价格并完成交易,避免浪费。和花卉市场类似,农民或供应商可以通过这种方式以市场接受的价格快速卖出产品。

5. 网络广告竞价

**实时竞价广告系统(RTB)**中的某些机制也有类似荷兰拍卖的特点。广告位起价较高,逐步降低,广告主在适合的价位抢占广告资源。

6. 库存清理

零售行业或二手市场常用于清理库存或折扣商品。起初价格高,逐步降价,鼓励买家尽快购买,避免拖延导致商品滞销。

7. 金融资产清算

在金融市场中,荷兰式拍卖可以用于快速清算某些资产,如破产企业的资产、二级市场的股票交易等。目标是以尽可能高的价格快速完成资产变现。

8. 艺术品与珍藏品拍卖

虽然较少见于高端艺术品拍卖,但对于一些具有即时需求的物品(如车牌号、特殊纪念币等),荷兰式拍卖有时会用来加速交易。

9. 频谱拍卖

政府拍卖无线通信频段时,有时采用类似荷兰式拍卖的机制。

10.停车位分配

某些智能停车系统会以价格递减方式吸引停车需求高峰期的车主。 荷兰式拍卖的核心优势在于效率高、决策迅速,适合处理需要快速成交且供需灵活的场景。

Avoid Using Resource Routes and Resource Controllers in Laravel

In the Laravel framework, resource routes and their corresponding resource controllers are notoriously limited in flexibility, so it’s best to avoid them. There’s no silver bullet here—you might think they save you time upfront, but customizing them can become a headache. It’s often better to skip them altogether from the start and implement routes and controllers tailored to your specific needs.

尽量不要使用资源路由和资源控制器

Laravel框架中,资源路由和对应的资源控制器扩展性太差,尽量别用。没有银弹,你以为你能偷到懒,但其实资源路由和资源控制器定制起来可麻烦了,不如一开始就不要使用它们。

On the Granularity of Git Commits

When using Git, it’s best practice to commit your code after completing a small logical step—such as implementing one or more functions that achieve a minor feature—or upon finishing a module. This approach makes code tracking and merging much easier.

Avoid committing only after writing a large amount of code. If one small logical step contains an error, rolling back your commit could mean losing significant portions of otherwise correct code.

Similarly, if you make changes to existing code or comments, like fixing a bug, you should commit those changes immediately. Don’t wait until you’ve finished writing a new feature to bundle everything into one large commit.

关于git commit代码的粒度

应该做完一个小的逻辑步骤(例如写完了一个或几个函数实现了一个小功能)就提交一次,完成一个模块就提交一次,这样方便代码回溯,也方便代码合并。不要写了大量代码后再提交一次,万一其中的一个小逻辑步骤的代码写错了,代码回滚后,大量其他正确的代码都白写了。

修改了之前的代码或注释,例如修复一个bug,也应该及时提交一次,而不是继续写完当前小功能的代码之后一起提交。

问题不在于“家族传承吾辈责”,问题在于“垄断”

“家族传承吾辈责”是可以的,也是应该的,这样才能造就百年企业。问题在于“垄断”,你通过权力手段搞垄断,只许自己家族或团队把持这个行业,在国内你们确实一直赢赢赢,但是长期垄断会导致傲慢、低质的服务和产品,在国际市场上,你们就会输输输……不能搞垄断,要给人民选择权,有竞争才会有进步。