linux網(wǎng)絡(luò)配置
1.ifconfig命令
ifconfig ##查看或設(shè)定網(wǎng)絡(luò)接口
ifconfig device ip/24 ##在對應(yīng)的網(wǎng)卡上臨時設(shè)定ip
ifconfig device down ##關(guān)閉網(wǎng)卡設(shè)備
ifconfig device up ##開啟網(wǎng)卡設(shè)備
ifconfig eth0 arp ##開啟網(wǎng)卡eth0 的arp協(xié)議;
ifconfig eth0 -arp ##關(guān)閉網(wǎng)卡eth0 的arp協(xié)議送火;
注意:device是你的網(wǎng)卡名
[root@server ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.74.124 netmask 255.255.255.0 broadcast 172.25.74.255
inet6 fe80::5054:ff:fe00:4a0b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:4a:0b txqueuelen 1000 (Ethernet)
RX packets 505 bytes 42601 (41.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 95 bytes 12266 (11.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 8 bytes 556 (556.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 556 (556.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
說明:
- eth0 網(wǎng)卡名
- inet 172.25.74.124 ip地址
- mtu 1500 最大傳輸單元
- netmask 255.255.255.0 子網(wǎng)掩碼
- broadcast 172.25.74.255 廣播地址
- inet6 fe80::5054:ff:fe00:4a0b prefixlen 64 ipv6的ip地址和子網(wǎng)掩碼
- ether 52:54:00:00:4a:0b mac地址
lo 是表示主機的回壞地址饲漾,這個一般是用來測試一個網(wǎng)絡(luò)程序,但又不想讓局域網(wǎng)或外網(wǎng)的用戶能夠查看掸宛,只能在此臺主機上運行和查看所用的網(wǎng)絡(luò)接口。比如把 HTTPD服務(wù)器的指定到回壞地址招拙,在瀏覽器輸入 127.0.0.1 就能看到你所架WEB網(wǎng)站了唧瘾。但只是您能看得到,局域網(wǎng)的其它主機或用戶無從知道别凤。
2.ip命令
ip addr add ip/24 dev eth0 #為eth0網(wǎng)卡添加一個ip
ip addr show eth0 #查看eth0網(wǎng)卡的ip
ip addr del ip/24 dev eth0 #刪除eth0網(wǎng)卡指定ip
3.nmcli命令
nmcli ##NetworkManager必須開啟
nmcli device connect eth0 ##啟用eth0網(wǎng)卡
nmcli device disconnect eth0 ##關(guān)閉eth0網(wǎng)卡
nmcli device show eth0 ##查看網(wǎng)卡信息
nmcli device status eth0 ##查看網(wǎng)卡服務(wù)接口信息
用nm-connection-editor改名稱為westos
nmcli connection show
nmcli connection down westos
nmcli connection up westos
nmcli connection delete westos
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.100/24 ##對應(yīng)nm-connection-editor來寫
nmcli connection modify westos ipv4.method auto
nmcli connection modify westos ipv4.method manual
nmcli connection modify westos ipv4.addresses 172.25.254.200/24
4.管理網(wǎng)絡(luò)配置文件
位置:網(wǎng)絡(luò)配置目錄/etc/sysconfig/network-scripts/
[root@server ~]# cd /etc/sysconfig/network-scripts/
[root@server network-scripts]# ls
ifcfg-Ethernet_connection_1 ifdown-Team ifup-post
ifcfg-lo ifdown-TeamPort ifup-ppp
ifdown ifdown-tunnel ifup-routes
ifdown-bnep ifup ifup-sit
ifdown-eth ifup-aliases ifup-Team
ifdown-ippp ifup-bnep ifup-TeamPort
ifdown-ipv6 ifup-eth ifup-tunnel
ifdown-isdn ifup-ippp ifup-wireless
ifdown-post ifup-ipv6 init.ipv6-global
ifdown-ppp ifup-isdn network-functions
ifdown-routes ifup-plip network-functions-ipv6
ifdown-sit ifup-plusb
以我的br0網(wǎng)卡為例介紹
DEVICE=br0 #網(wǎng)卡接口名稱
NAME="Bridge br0" #在圖形中的網(wǎng)卡名饰序,網(wǎng)絡(luò)連接的名字
ONBOOT=yes #網(wǎng)絡(luò)服務(wù)開啟時自動激活網(wǎng)卡
NM_CONTROLLED=yes
BOOTPROTO=none #設(shè)備工作方式 設(shè)置為none禁止DHCP,設(shè)置為static啟用靜態(tài)IP地址规哪,設(shè)置為dhcp開啟DHCP服務(wù)
IPADDR0=172.25.254.74 #第一個ip地址
PREFIX0=24 #第一個ip的子網(wǎng)掩碼
DEFROUTE=yes
ZONE=trusted
IPADDR1=172.25.74.250
PREFIX1=24
5.虛擬機聯(lián)網(wǎng)
1.首先設(shè)置真實主機為路由器菌羽,作為虛擬機的網(wǎng)關(guān)
[root@foundation74 network-scripts]# firewall-cmd --permanent --add-masquerade 開啟地址偽裝
success
[root@foundation74 network-scripts]# firewall-cmd --reload 重新加載
success
[root@foundation74 network-scripts]# firewall-cmd --list-all
trusted (active)
target: ACCEPT
icmp-block-inversion: no
interfaces: br0 wlp3s0
sources:
services:
ports:
protocols:
masquerade: yes <<<地址偽裝功能開啟,真實主機變成路由器
forward-ports:
sourceports:
icmp-blocks:
rich rules:
2.設(shè)置虛擬機的網(wǎng)關(guān)
第一種方法:
vim /etc/sysconfig/network ##全局網(wǎng)關(guān)由缆,針對所有沒有設(shè)定網(wǎng)關(guān)的網(wǎng)卡生效
GATEWAY=真機的ip
第二種方法:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY0=真機的ip ##檔網(wǎng)卡中設(shè)定的IP有多個時注祖,指定對于那個IP生效
GATEWAY=真機的ip ##檔網(wǎng)卡中設(shè)定的IP只有一個時
[root@server ~]# route -n 查看虛擬機的路由表
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.74 0.0.0.0 UG 1024 0 0 eth0
172.25.254.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
網(wǎng)關(guān)設(shè)置成功后,在真機連網(wǎng)的前提下均唉,就可以ping通外網(wǎng)的ip了是晨,說明可以上網(wǎng)了。
這時你在虛擬機內(nèi)ping域名是不通的,因為我們還沒有配置域名舔箭,虛擬機只認(rèn)識ip罩缴,下面我通過真機得到百度的ip,在使用虛擬機測試层扶。
真機
[root@foundation74 network-scripts]# ping www.baidu.com
PING www.a.shifen.com (180.149.131.98) 56(84) bytes of data.
64 bytes from 180.149.131.98 (180.149.131.98): icmp_seq=1 ttl=55 time=26.8 ms
得到百度的ip是180.149.131.98箫章,現(xiàn)在使用虛擬機
[root@server ~]# ping 180.149.131.98
PING 180.149.131.98 (180.149.131.98) 56(84) bytes of data.
64 bytes from 180.149.131.98: icmp_seq=4 ttl=54 time=23.7 ms
測試成功,也可以打開瀏覽器镜会,把ip寫入地址欄里測試檬寂,一樣可以打開百度的主頁。
3.配置DNS
DNS簡單來說就是可以根據(jù)域名解析得到ip
domain name server == 域名解析服務(wù) ##解析就是把域名變成IP
第一種配置域名的方法:
vim /etc/hosts ##本地解析文件
ip 域名 在這個文件里加入你常用的ip以及他的域名
180.149.131.98 www.baidu.com
[root@server ~]# systemctl restart network //修改了配置文件要重起服務(wù)
[root@server ~]# ping www.baidu.com //直接ping域名
PING www.baidu.com (180.149.131.98) 56(84) bytes of data.
64 bytes from www.baidu.com (180.149.131.98): icmp_seq=1 ttl=54 time=25.7 ms
你也可以不叫baidu
180.149.131.98 www.bd.com
[root@server ~]# ping www.bd.com
PING www.bd.com (180.149.131.98) 56(84) bytes of data.
64 bytes from www.bd.com (180.149.131.98): icmp_seq=1 ttl=54 time=31.6 ms
通過域名訪問成功戳表。
第二種配置域名的方法(測試前先把剛剛修改的配置恢復(fù)成初始狀態(tài)):
vim /etc/resolv.conf ##域名解析配置文件
nameserver 114.114.114.114 ##電信運營的公用dns
nameserver 218.30.19.50 ##陜西省本地dns
[root@server ~]# systemctl restart network
[root@server ~]# ping www.baidu.com
PING www.a.shifen.com (180.149.132.151) 56(84) bytes of data.
64 bytes from 180.149.132.151: icmp_seq=1 ttl=54 time=23.3 ms
通過域名訪問成功桶至。