當前位置:首頁 » 操作系統 » linux子網掩碼

linux子網掩碼

發布時間: 2023-03-25 03:10:00

linux配置網路參數之IP地址、子網掩碼、網關地址,DNS

修改網卡命令規則 (eth0、eth1、eth2……)

打開grub內核引導程序,在……quiet 後面添加:

讓網卡命名規則生效

nmcli命令刪除錯誤網卡命名

nmcli命令添加網卡命名

解析: nmcli connection 添加 類型 乙太網設備 網卡設備名為eth0 nmcli命令的命名為eth0

修改IP地址、子網掩碼、網關地址、DNS

解析:nmcli connection 修改 網卡名 ipv4.方法 手工配置 ipv4.地址192.168.4.7/24 ipv4.網關 192.168.4.254 每次開機自動啟用以上所有參數

查看網卡配置文件

查看網關地址信息

② Linux怎麼設置IP,子網掩碼和網關

linux一般使用ifconfig命令修改linux主機的ip、網關或子網掩碼。 1.命令格式: ifconfig [網路設備] [參數] 2.命令功能: ifconfig 命令用來查看和配置網路設備。當網路環境發生改變時可通過此命令對網路進行相應的配置。 3.命令參數: up 啟動指定網路設備/網卡。 down 關閉指定網路設備/網卡。該參數可以有效地阻止通過指定介面的IP信息流,如果想永久地關閉一個介面,我們還需要從核心路由表中將該介面的路由信息全部刪除。 arp 設置指定網卡是否支持ARP協議。 -promisc 設置是否支持網卡的promiscuous模式,如果選擇此參數,網卡將接收網路中發給它所有的數據包 -allmulti 設置是否支持多播模式,如果選擇此參數,網卡將接收網路中所有的多播數據包 -a 顯示全部介面信息 -s 顯示摘要信息(類似於 netstat -i) add 給指定網卡配置IPv6地址 del 刪除指定網卡的IPv6地址 <硬體地址> 配置網卡最大的傳輸單元 mtu<位元組數> 設置網卡的最大傳輸單元 (bytes) netmask<子網掩碼> 設置網卡的子網掩碼。掩碼可以是有前綴0x的32位十六進制數,也可以是用點分開的4個十進制數。如果不打算將網路分成子網,可以不管這一選項;如果要使用子網,那麼請記住,網路中每一個系統必須有相同子網掩碼。 tunel 建立隧道 dstaddr 設定一個遠端地址,建立點對點通信 -broadcast<地址> 為指定網卡設置廣播協議 -pointtopoint<地址> 為網卡設置點對點通訊協議 multicast 為網卡設置組播標志 address 為網卡設置IPv4地址 txqueuelen<長度> 為網卡設置傳輸列隊的長度 4.使用實例: 實例1:顯示網路設備信息(激活狀態的) 命令: ifconfig 輸出: [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20 inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB) 說明: eth0 表示第一塊網卡, 其中 HWaddr 表示網卡的物理地址,可以看到目前這個網卡的物理地址(MAC地址)是 00:50:56:BF:26:20 inet addr 用來表示網卡的IP地址,此網卡的 IP地址是 192.168.120.204,廣播地址, Bcast:192.168.120.255,掩碼地址Mask:255.255.255.0 lo 是表示主機的回壞地址,這個一般是用來測試一個網路程序,但又不想讓區域網或外網的用戶能夠查看,只能在此台主機上運行和查看所用的網路介面。比如把 HTTPD伺服器的指定到回壞地址,在瀏覽器輸入 127.0.0.1 就能看到你所架WEB網站了。但只是您能看得到,區域網的其它主機或用戶無從知道。 第一行:連接類型:Ethernet(乙太網)HWaddr(硬體mac地址) 第二行:網卡的IP地址、子網、掩碼 第三行:UP(代表網卡開啟狀態)RUNNING(代表網卡的網線被接上)MULTICAST(支持組播)MTU:1500(最大傳輸單元):1500位元組 第四、五行:接收、發送數據包情況統計 第七行:接收、發送數據位元組數統計信息。 實例2:啟動關閉指定網卡 命令: ifconfig eth0 up ifconfig eth0 down 輸出: 說明: ifconfig eth0 up 為啟動網卡eth0 ;ifconfig eth0 down 為關閉網卡eth0。ssh登陸linux伺服器操作要小心,關閉了就不能開啟了,除非你有多網卡。 實例3:為網卡配置和刪除IPv6地址 命令: ifconfig eth0 add 33ffe:3240:800:1005::2/64 ifconfig eth0 del 33ffe:3240:800:1005::2/64 輸出: 說明: ifconfig eth0 add 33ffe:3240:800:1005::2/64 為網卡eth0配置IPv6地址; ifconfig eth0 add 33ffe:3240:800:1005::2/64 為網卡eth0刪除IPv6地址; 練習的時候,ssh登陸linux伺服器操作要小心,關閉了就不能開啟了,除非你有多網卡。 實例4:用ifconfig修改MAC地址 命令: ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE 輸出: [root@localhost ~]# ifconfig eth0 down //關閉網卡 [root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址 [root@localhost ~]# ifconfig eth0 up //啟動網卡 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:EE inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB) [root@localhost ~]# ifconfig eth0 hw ether 00:50:56:BF:26:20 //關閉網卡並修改MAC地址 [root@localhost ~]# ifconfig eth0 up //啟動網卡 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20 inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB) 說明: 實例5:配置IP地址 命令: 輸出: [root@localhost ~]# ifconfig eth0 192.168.120.56 [root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 [root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255 說明: ifconfig eth0 192.168.120.56 給eth0網卡配置IP地:192.168.120.56 ifconfig eth0 192.168.120.56 netmask 255.255.255.0 給eth0網卡配置IP地址:192.168.120.56 ,並加上子掩碼:255.255.255.0 ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255 /給eth0網卡配置IP地址:192.168.120.56,加上子掩碼:255.255.255.0,加上個廣播地址: 192.168.120.255 實例6:啟用和關閉ARP協議 命令: ifconfig eth0 arp ifconfig eth0 -arp 輸出: [root@localhost ~]# ifconfig eth0 arp [root@localhost ~]# ifconfig eth0 -arp 說明: ifconfig eth0 arp 開啟網卡eth0 的arp協議; ifconfig eth0 -arp 關閉網卡eth0 的arp協議; 實例7:設置最大傳輸單元 命令: ifconfig eth0 mtu 1500 輸出: [root@localhost ~]# ifconfig eth0 mtu 1480 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1 RX packets:8712395 errors:0 dropped:0 overruns:0 frame:0 TX packets:36631 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:597062089 (569.4 MiB) TX bytes:2643973 (2.5 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:9973 errors:0 dropped:0 overruns:0 frame:0 TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB) [root@localhost ~]# ifconfig eth0 mtu 1500 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8712548 errors:0 dropped:0 overruns:0 frame:0 TX packets:36685 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:597072333 (569.4 MiB) TX bytes:2650581 (2.5 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:9973 errors:0 dropped:0 overruns:0 frame:0 TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB) [root@localhost ~]# 說明: 設置能通過的最大數據包大小為 1500 bytes 備註:用ifconfig命令配置的網卡信息,在網卡重啟後機器重啟後,配置就不存在。要想將上述的配置信息永遠的存的電腦里,那就要修改網卡的配置文件了。

③ 1、Linux系統基本網路

1.1、伺服器注意事項:

遠程伺服器不允許關機,只能重啟

重啟時應該先關閉服務

不要在伺服器訪問高峰運行高負載命令

遠程配置防火牆時不要把自己踢出伺服器

指定合理的密碼規范並定期更新

合理分配許可權

定期備份重要的數據和日誌

1.2、設置網路橋接命令和(ANT模式):

systemctl restart network------重啟網卡service network restart---------重啟網路服務

systemctl  stop NetworkManager 臨時暫停網路管理器systemctl disable NetworkManager 永久關閉網路管理器

systemctl start NetworkManager      擁有root用戶的可執行許可權

systemctl stop NetworkManager      停止並禁用虛擬機 NetworkManager 服務

systemctl disable NetworkManager

注意:修改網路配置文件後,需要重新載入網路連接,如果是通過network.service則使用命令:systemctl restart network;如果是通過NetworkManager.service則使用nmcli命令:nmcli connection reload。

設置網路主要操作(橋接模式和)

[root@localhost network-scripts]# cd etc/sysconfig/network-scripts/

[root@localhost network-scripts]# vi ifcfg-ens33

systemctl restart network------重啟網卡

service network restart---------重啟網路服務

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=static            設置靜態

DEFROUTE=yes

IPV4_FAILURE_FATAL=yes

IPV6INIT=yes

IPV6_AUTOCONF=yes

IPV6_DEFROUTE=yes

IPV6_FAILURE_FATAL=no

IPV6_ADDR_GEN_MODE=stable-privacy

NAME=ens33

UUID=10c17057-e9c9-4831-a8ff-0757ea0abc0b

DEVICE=ens33

ONBOOT=yes                          #開機重啟

#IPADDR=192.168.43.168              #ip地址(需要跟主機同一個網段,不同一個IP)

IPADDR=10.63.73.20                 

#NETMASK=255.255.255.0              #子網掩碼(下面三個都跟主機一樣)

NETMASK=255.255.0.0

#GATEWAY=192.168.43.1              #網關

GATEWAK=10.63.255.254

#GATEWAK=10.200.0.3

#DNS1=192.168.43.1                  #DNS服務

設置網路主要步驟(Nat模式)

1.打開Vm,點擊編輯->虛擬網路編輯

2.選擇VMnet8,將VMnet信息改為NAT模式,比如我的本機IP是192.168.138.1,子網掩碼為255.255.255.0

所以我將下面的IP配置成192.168.138.0 子網掩碼配置成255.255.255

3.再點擊NAT設置將網管配置成192.168.138.2(ps:網關不要配置成和自己IP地址一樣)

然後再應用確定

4.右鍵點擊虛擬機設置,網路適配器改為自定義(選擇Vmnet8 Nat模式)

5.接下來開啟虛擬機

cd /etc/sysconfig/network-scripts/

vim ifcfg-eno16777736

然後再執行命令:/etc/init.d/network restart

1.3、設置防火牆

1.3.1、防火牆命令

停止防火牆:

systemctl stop firewalld.service #停止firewall 防火牆

service  iptables stop  #(centos7版本之前)

永久關閉防火牆:

systemctl disable firewalld.service #禁止firewall開機啟動 ( 永久關閉防火牆 )

chkconfig iptables off #永久關閉防火牆

開始防火牆:

systemctl start firewalld  #啟動防火牆

systemctl restart iptables.service #重啟防火牆使配置生效

systemctl enable iptables.service #設置防火牆開機啟動(重啟)

查看防火牆狀態:

systemctl status firewalld

service  iptables status # (7版本之前)

1.3.2、Linux chkconfig 命令

Linux chkconfig 命令用於檢查,設置系統的各種服務。

這是Red Hat公司遵循GPL規則所開發的程序,它可查詢操作系統在每一個執行等級中會執行哪些系統服務,其中包括各類常駐服務。

語法

chkconfig [--add][--del][--list][系統服務] 或 chkconfig [--level <等級代號>][系統服務][on/off/reset]

參數 :

--add 增加所指定的系統服務,讓 chkconfig 指令得以管理它,並同時在系統啟動的敘述文件內增加相關數據。

--del 刪除所指定的系統服務,不再由 chkconfig 指令管理,並同時在系統啟動的敘述文件內刪除相關數據。

--level<等級代號> 指定讀系統服務要在哪一個執行等級中開啟或關畢。

實例

列出chkconfig 所知道的所有命令,可以用chkconfig –list查看所有的服務及其在每個級別的開啟狀態。

# chkconfig --list

開啟服務

# chkconfig telnet on   //開啟 Telnet 服務

# chkconfig --list      //列出 chkconfig 所知道的所有的服務的情況

關閉服務

# chkconfig telnet off  // 關閉 Telnet 服務

# chkconfig --list      // 列出 chkconfig 所知道的所有的服務的情況

[root@cent01 sbin]# chkconfig --level 3 network off  //關閉3級別的network服務

[root@cent01 sbin]# chkconfig --list //3級別已關閉

network        0:關 1:關 2:開 3:關 4:開 5:開 6:關

[root@cent01 sbin]# chkconfig network on //不輸入級別,默認打開2,3,4,5級別

[root@cent01 sbin]# chkconfig --list

network        0:關 1:關 2:開 3:開 4:開 5:開 6:關

[root@cent01 sbin]# chkconfig --del network //刪除network

[root@cent01 sbin]# chkconfig --list  //network已消失

mysqld          0:關 1:關 2:開 3:開 4:開 5:開 6:關

netconsole      0:關 1:關 2:關 3:關 4:關 5:關 6:關

[root@cent01 sbin]# chkconfig --add network //增加network服務

[root@cent01 sbin]# chkconfig --list  //network服務又恢復了

mysqld          0:關 1:關 2:開 3:開 4:開 5:開 6:關

netconsole      0:關 1:關 2:關 3:關 4:關 5:關 6:關

network        0:關 1:關 2:開 3:開 4:開 5:開 6:關

[root@cent01 sbin]# chkconfig --list

註:該輸出結果只顯示 SysV 服務,並不包含

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。

      要列出 systemd 服務,請執行 'systemctl list-unit-files'。

      查看在具體 target 啟用的服務請執行

      'systemctl list-dependencies [target]'。

mysqld          0:關 1:關 2:開 3:開 4:開 5:開 6:關

netconsole      0:關 1:關 2:關 3:關 4:關 5:關 6:關

注意: 但是這里只顯示了SysV管理的服務,centos6及之前都是這個管理工具,但是在centos7用的是systemd管理,所以systemd管理的服務在這里沒有顯示出來。

運行級別為系統啟動級別,具體含義如下:

0 shutdown關機

1 單用戶模式

2 無NFS支持的多用戶模式

3 完全多用戶模式,常用的命令行模式

4 保留給用戶自定義

5 圖形界面登錄,比3多了一個圖形界面

6 重啟

1.3.2、Linux systemd命令

systemd是管理開機啟動程序的工具(SysV啟動開機進程時一次只能啟動一個,而systemd則一次可以啟動多個服務,這樣就導致systemd的開機速度會更快。)

[root@localhost ~]# systemctl list-units --all --type=service

  UNIT                            LOAD      ACTIVE  SUB    DESCRIPTION

  abrt-ccpp.service              loaded    active  exited  Install ABRT coremp hook

  abrt-oops.service              loaded    active  running ABRT kernel log watcher

  UNIT                            LOAD      ACTIVE  SUB    DESCRIPTION

[root@cent01 sbin]# ls /usr/lib/systemd/system  //啟動的腳本文件目錄

[root@cent01 ~]# systemctl list-units  //列出正在運行的unit

[root@cent01 ~]# systemctl list-units --all //列出所有的unit,包括active和inactive

[root@cent01 ~]# systemctl list-units --all --state=inactive //列出inactive的unit

[root@cent01 ~]# systemctl list-units --all --type=service  //列出所有狀態的service

[root@cent01 ~]# systemctl list-units --type=service  //列出active的service

[root@cent01 ~]# systemctl is-active crond.service  //查看某個unit是否active

systemctl enable crond.service //開機啟動 .service可以省略

systemctl disable crond.service //禁止開機啟動

systemctl status crond.service  //查看服務狀態

systemctl start crond.service //啟動服務

systemctl stop crond.service  //停止服務

systemctl restart crond.service  //重啟服務

systemctl is-enabled crond.service  //查看某個服務是否開機啟動

unit /usr/lib/systemd/system 此目錄下列出了很多文件,這些文件都是unit。類別如下:

service 系統服務target 多個unit組成的組device 硬體設備mount 文件系統掛載點automount 自動掛載點path 文件或路徑scope 不是由systemd啟動的外部進程slice 進程組snapshot systemd快照socket 進程間通信的套接字swap swap文件timer 定時器

target target類似於centos6的啟動級別,target內又包含多個unit的組合,當然target內也可以包含target。啟動target就是啟動多個unit,用target來管理這些unit。

[root@cent01 ~]# systemctl list-units --all --type=target  //查看當前所有的target

  UNIT                      LOAD      ACTIVE  SUB    DESCRIPTION

  basic.target              loaded    active  active Basic System

  bluetooth.target          loaded    active  active Bluetooth

[root@localhost ~]# systemctl list-dependencies multi-user.target

multi-user.target

● ├─abrt-ccpp.service

● ├─abrt-oops.service

● ├─abrt-vmcore.service

● ├─abrt-xorg.service

[root@localhost ~]# systemctl get-default  //查看系統默認的target

multi-user.target

systemctl set-default multi-user.target  //設置默認的target

multi-user.target等同於centos6的運行級別3。他們的對應關系如下:

SysV運行級別systemd target備注

0poweroff.target關閉系統

1rescure.target單用戶模式

2multiuser.target用戶自定義級別,通常識別為3

3multiuser.target多用戶命令行模式

4multiuser.target用戶自定義級別,通常識別為3

5graphical.target多用戶圖形界面,比級別3隻多一個GUI

6reboot.target重啟

所以總結起來,一個service屬於一種unit,多個unit組成一個target,當然target裡面也可以包含target。

1.4、ifconfig命令配置IP網路參數

格式:

[root@localhost /]#ifconfig [網路設備] [ip地址] [MAC地址] [netmask掩碼地址] [broadcast廣播地址(NDC)] [up/down]

[root@localhost /]#ifconfig eth0 192.168.74.130 netmask 255.255.255.0 up

用ifconfig命令配置eth0別名設備,為eth0綁定多個IP地址。

[root@localhost /]#ifconfig eth0:1 192.168.74.130

[root@localhost /]#ifconfig eth0:2 192.168.73.130

1.5、使用routedel命令添加路由

格式:

[root@localhost /]#routedel [-net|host] [網域或主機] netmask [mask] [gw]

[root@localhost /]#route #查看路由信息

功能:添加路由

-net : 表示後面接的路由為一個網路。

-host : 表示後面接的為連接到單部主機的路由。

netmask : 與網路有關,可以設定netmask決定網路的大小。

gw : gateway (網關)的簡寫,後面接的是ip地址。

1.6、使用hostname命令修改主機名稱

[root@localhost /]#hostname service.jw.com

hostnamectl set-hostname nod1

④ 在LINUX下如何用命令 配置ip地址 子網掩碼和網管

1、首先進入到Linux系統中,輸入用戶名和密碼完成登錄。

熱點內容
android磁場感測器 發布:2025-05-20 04:50:46 瀏覽:827
python經典編程題 發布:2025-05-20 04:42:33 瀏覽:781
xp電腦訪問win7 發布:2025-05-20 04:41:59 瀏覽:616
金融的配置是什麼 發布:2025-05-20 04:41:07 瀏覽:466
解壓擠耳朵 發布:2025-05-20 04:37:02 瀏覽:887
QP演算法包 發布:2025-05-20 04:31:54 瀏覽:969
ps3連ftp 發布:2025-05-20 04:19:11 瀏覽:818
計算機編譯干什麼的 發布:2025-05-20 04:05:18 瀏覽:47
安卓如何調手機時間 發布:2025-05-20 04:01:31 瀏覽:917
風扇轉壓縮機不轉 發布:2025-05-20 03:57:47 瀏覽:285