---
# System prepended metadata

title: Linux Network Command
tags: [Linux, Networking]

---

# Linux Network Command


###### tags: `Linux` `Networking`

[TOC]

# 0.0.0.0 Meaning 

## Server 

0.0.0.0-> Receive Any Interface 

例如 
```
nc -lvnp 443
```
listening on 0.0.0.0 : 443

任意IP 連到 本主機任何一個 interface 都會收到 request info 


## Default Routing 

用來設定 (Default Routing)

當IP packet 在Routing table 中找不到路時 就會走 (Default Routing)

static routing 


```
ip route-static 0.0.0.0 0.0.0.0 192.168.1.1
```

匹配不到 next hop -> 192.168.1.1

因為 10.10.10.10(任意) 跟 0.0.0.0 And -> 0.0.0.0 跟原先 0.0.0.0 Match -> 就會走這條規則

----

# Network Parameter Configuration 

## ifconfig 

可以手動更改 Interface 的設定

Basic Syntax 

```
ifconfig {interface} {up|down} #enable or disable 
ifconfig interface {options}
```


Options: 
- up / down 
- mtu
- netmask
- broadcast

---

InFo Analysis

HWaddr -> MAC ADDRESS
RX -> Show Receptive Packet 
TX -> show Transmit Packet

texqueuelen -> Buffer 的長度
collistions -> packet 碰撞

![](https://hackmd.io/_uploads/r16EtpuLn.png)

### 一般interface設定
```
[root@www ~]# ifconfig eth0 192.168.100.100 \
> netmask 255.255.255.128 mtu 8000 
```

\  ->　換行輸入

### 在一個Interface 設定多個IP


```
ifconfig ens33:0 192.168.1.1
ifconfig ens33:1 192.168.1.2
```

![](https://hackmd.io/_uploads/ryFh2aOI2.png)


### Reset 手動 configuration 

```
/etc/init.d/network restart
```

![](https://hackmd.io/_uploads/SkD4p6OI2.png)


## ifup, ifdown


Script 是放在

/etc/sysconfig/network-scripts


ifcfg-ens33

![](https://hackmd.io/_uploads/ryG4RTuL2.png)


使用方法
```
[root@www ~]# ifup   {interface}
[root@www ~]# ifdown {interface}

[root@www ~]# ifup eth0
```

## Route


route 

主要顯示 Domain Name 

會去做DNS 反查詢

```
[centos@localhost ~]$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.203.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
```

---

route -n (常用)

主要顯示 IP address 
```
[centos@localhost ~]$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.203.2   0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.203.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

```




### Destination
-> 目標 Network 網段

Default --> 0.0.0.0  

---

### Gateway 

Gateway (需要Router 來通訊) Host -> Next hop 


Gateway 0.0.0.0 -> 目標在同個LAN (路由是直接由本機傳送)
透過MAC 直接通訊

### flags

Flags：總共有多個旗標，代表的意義如下：  

-   **U (route is up)：該路由是啟動的**；
-   H (target is a host)：目標是一部主機 (IP) 而非網域；
-   **G (use gateway)：需要透過外部的主機 (gateway) 來轉遞封包；**
-   R (reinstate route for dynamic routing)：使用動態路由時，恢復路由資訊的旗標；
-   D (dynamically installed by daemon or redirect)：已經由服務或轉 port 功能設定為動態路由
-   M (modified from routing daemon or redirect)：路由已經被修改了；
-   ! (reject route)：這個路由將不會被接受(用來抵擋不安全的網域！)


---

### iface 

這個路由傳遞封包的介面


### Routing 方式

會依Routing Table 的順序送 -> 類似防火牆規則

從小Range 開始找
class A -> class B -> class C -> 0.0.0.0

Target Dst -> netmask 做 AND match 就送


### Adding Routing 

```
[root@localhost centos]# route add -net 10.10.10.0 netmask 255.255.255.0 dev ens33
[root@localhost centos]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.203.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

```

### Delete Routing 

```
[root@localhost centos]# route del -net 10.10.10.0 netmask 255.255.255.0 dev ens33
[root@localhost centos]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.203.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

```

### Reset Routing Table 

/etc/init.d/network restart

---




# netstat 

```
netstat -tulnp 
```

flag

-t top
-u udp
-l listening
-n no domain parse 
-p process 

## Display networking statistics (like SNMP)
顯示網絡統計信息

```
[centos@localhost pts]$ netstat  --statistics 
Ip:
    248 total packets received
    0 forwarded
    0 incoming packets discarded
    247 incoming packets delivered
    244 requests sent out
    16 dropped because of missing route
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
    0 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
Tcp:
    2 active connections openings
    0 passive connection openings
    0 failed connection attempts
    0 connection resets received
    0 connections established
    25 segments received
    25 segments send out
    0 segments retransmited
    0 bad segments received.
    1 resets sent
Udp:
    211 packets received
    0 packets to unknown port received.
    0 packet receive errors
    215 packets sent
    0 receive buffer errors
    0 send buffer errors

```
or 
```
[centos@localhost pts]$ netstat -t -u --statistics
```

## Display interface table
顯示接口表

```
[centos@localhost pts]$ netstat  --interfaces
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33            1500      274      0      0 0           299      0      0      0 BMRU
lo              65536        0      0      0 0             0      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU
```

