Linux配置網(wǎng)絡(luò)
2024-06-28 16:04:42
供稿:網(wǎng)友
配置網(wǎng)路
一、常見的網(wǎng)絡(luò)接口接口eth0 以太網(wǎng)接口wlan0 無線接口lo 本地環(huán)回接口bond0 bonding接口 eth0和eth1綁定之后的一個(gè)虛擬接口,為了提高網(wǎng)速virbr0 Kvm橋接接口 <虛擬交換機(jī)>br0 Kvm橋接接口 <虛擬交換機(jī)vmnet0 VMware虛擬機(jī)網(wǎng)卡接口二、查看網(wǎng)絡(luò)信息ip、網(wǎng)關(guān)[root@localhost ~]# ip addr //查看IP、掩碼、MAC...[root@localhost ~]# ifconfig[root@localhost ~]# ip addr show eth0 //只顯示eth0的信息[root@localhost ~]# ifconfig eth0[root@localhost ~ ]# ip route //查看本機(jī)路由表[root@localhost ~]# route -n //查看路由表信息三、修改網(wǎng)絡(luò)信息臨時(shí):立即生效,重啟失效1. 添加臨時(shí)IP# ifconfig eth0 192.168.2.252/24 up //修改eth0的IP# ifconfig eth0:0 192.168.2.253/24 up //給eth0增加IP# ifconfig eth0:1 192.168.2.251/24 up //給eth0增加IP# ip addr add dev eth0 192.168.2.252/24 //增加新的臨時(shí)IP# ip addr add 192.168.3.252/24 dev eth0 # ip addr show eth0 2.臨時(shí)添加刪除網(wǎng)關(guān)# route add default gw 192.168.0.254# route del default gw 192.168.0.254或者是# ip r del3.臨時(shí)的方式添加的ip,重啟網(wǎng)絡(luò)服務(wù)之后失效重啟網(wǎng)絡(luò)服務(wù)的方式由兩種:[root@localhost ~]# /etc/init.d/network restart[root@localhost ~]# service network restartNetworkManager: RHEL5/6[root@localhost ~]# /etc/init.d/NetworkManager stop//開著它有很多高級(jí)的網(wǎng)絡(luò)服務(wù)用不了[root@localhost ~]# chkconfig NetworkManager offIptables:[root@localhost ~]# iptables -nL //查看防火墻的規(guī)則[root@localhost ~]# iptables -F //清空防火墻的規(guī)則[root@localhost ~]# service iptables save[root@localhost ~]# /etc/init.d/iptables stop //停掉服務(wù)[root@localhost ~]# chkconfig iptables off //設(shè)置開機(jī)不啟動(dòng)[root@localhost ~]# chkconfig iptables --list //查看各個(gè)運(yùn)行級(jí)別的狀態(tài)iptables 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉SElinux: 安全增強(qiáng)的Linux[root@localhost ~]# setenforce 0[root@localhost ~]# vim /etc/selinux/configSELINUX=disabled[root@localhost ~]# getenforce Disabled# init [0-6] 切換運(yùn)行級(jí)別0:關(guān)機(jī)1:單用戶(類似于windows操作系統(tǒng)的安全模式)2:多用戶狀態(tài)沒有網(wǎng)絡(luò)服務(wù) 3:多用戶狀態(tài)有網(wǎng)絡(luò)服務(wù)(在做開發(fā)時(shí),通常設(shè)置成這個(gè)啟動(dòng)級(jí)別,直接進(jìn)入到命令行的界面)4:系統(tǒng)未使用保留給用戶 5:圖形界面(這是linux默認(rèn)的啟動(dòng)級(jí)別,直接進(jìn)入圖形界面)6:系統(tǒng)重啟[root@localhost ~]# vim /etc/inittab 與系統(tǒng)的啟動(dòng)級(jí)別相關(guān)的配置文件id:5:initdefault: //默認(rèn)的運(yùn)行級(jí)別永久:配置文件 方法一:命令行的方式1. IP/掩碼/默認(rèn)網(wǎng)關(guān):/etc/sysconfig/network-scripts/ifcfg-eth0靜態(tài)(手工)DEVICE=eth0 設(shè)備名BOOTPROTO=none 啟動(dòng)協(xié)議: static | none | dhcpBROADCAST=192.168.2.255 廣播地址HWADDR=00:E0:4C:41:95:DB MAC地址TYPE=Ethernet 網(wǎng)卡類型UUID=1581f655-1c2c-4578-bcc6-e0622a4578dc 網(wǎng)卡的UUIDNM_CONTROLLED=yes 是否接受NetworkManager管理,no表示關(guān)閉網(wǎng)卡的管理軟件IPADDR=192.168.2.253 IPPREFIX=24 子網(wǎng)掩碼。NETMASK=255.255.255.0 ONBOOT=yes 是否隨服務(wù)啟動(dòng)而啟動(dòng),不開啟他相當(dāng)于沒有網(wǎng)卡GATEWAY=192.168.2.254 默認(rèn)網(wǎng)關(guān)注釋:靜態(tài)的方式配置ip地址等網(wǎng)絡(luò)信息,只需要由紅色的幾行,黑色的幾行信息是可有可無的,但若想ping通非本網(wǎng)段的主機(jī),就需要有網(wǎng)關(guān),如果不需要ping通非本網(wǎng)段的主機(jī),就不需要添加==動(dòng)態(tài)(通過DHCP服器分配)DEVICE=eth0BOOTPROTO=dhcpONBOOT=yes[root@localhost ~]# service network restart //影響IP、DNS和網(wǎng)關(guān)的[root@localhost ~]# /etc/init.d/network restart方法二:文本圖形工具 setup [root@localhost ~] service network restart[root@localhost ~] /etc/init.d/network restart擴(kuò)展:為一個(gè)網(wǎng)卡綁定多個(gè)IP方法一:[root@localhost ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0DEVICE=eth0:0ONBOOT=yesBOOTPROTO=noneIPADDR=10.10.10.1NETMASK=255.255.255.0[root@localhost ~] service network restart方法二:[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0 設(shè)備名ONBOOT=yes 是否啟用該接口BOOTPROTO=none 靜態(tài): static, none 動(dòng)態(tài):dhcp, dynamicIPADDR1=172.16.60.14PREFIX1=24IPADDR2=10.10.10.1PREFIX2=24 注:需要重啟之后才能永久生效[root@localhost ~]# service network restart[root@Server ~]# ip a show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:56:c7:2f brd ff:ff:ff:ff:ff:ff inet 192.168.2.102/24 brd 192.168.2.255 scope global eth0 inet6 fe80::20c:29ff:fe56:c72f/64 scope link valid_lft forever preferred_lft forever雙網(wǎng)卡配置方法:為虛擬機(jī)添加一塊網(wǎng)卡[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth1[root@localhost network-scripts]# vim ifcfg-eth1DEVICE="eth1"BOOTPROTO="static"ONBOOT="yes"IPADDR=192.16.140.3PREFIX=24[root@localhost network-scripts]# /etc/init.d/network restart四、其它一些管理工具# ethtool eth0Link detected: yes //查看網(wǎng)絡(luò)物理連接,看最后一行是否是yes# mii-tool eth0 //查看網(wǎng)絡(luò)物理連接,看是否出現(xiàn)link oketh0: negotiated 100baseTx-FD flow-control, link ok# ifdown eth0 //停用eth0# ifup eth0 //啟用eth0# dhclient //動(dòng)態(tài)獲取ip地址 虛擬機(jī)中修改網(wǎng)卡接口的配置文件:[root@localhost network-scripts]# vim /etc/udev/rules.d/70-persistent-net.rules