4-修改络配置文件
一次性配置IP地址操作
[root@test2 Desktop]# ifconfig eth0 192.168.1.107/24 //配置IP地址命令
[root@test2 Desktop]# ifconfig //查看配置后的信息
eth0 Link encap:Ethernet HWaddr 00:0C:29:3B:AF:22
inet addr:192.168.1.107 Bcast:192.168.1.255 Mask:255.255.255.0 //新配置的IP地址
inet6 addr: fe80::20c:29ff:fe3b:af22/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6528 errors:0 dropped:0 overruns:0 frame:0
TX packets:3259 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:451562 (440.9 KiB) TX bytes:330389 (322.6 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:287 errors:0 dropped:0 overruns:0 frame:0
TX packets:287 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:20772 (20.2 KiB) TX bytes:20772 (20.2 KiB)
[root@test2 Desktop]# service network restart //重新启动网络服务
Shutting down interface eth0: Device state: 3 (disconnected)
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Active connection state: activated
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/5
[ OK ]
[root@test2 Desktop]# ifconfig //再次查看
eth0 Link encap:Ethernet HWaddr 00:0C:29:3B:AF:22
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0 //地址变成了默认的
inet6 addr: fe80::20c:29ff:fe3b:af22/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6530 errors:0 dropped:0 overruns:0 frame:0
TX packets:3261 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:451682 (441.0 KiB) TX bytes:330535 (322.7 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:303 errors:0 dropped:0 overruns:0 frame:0
TX packets:303 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:21988 (21.4 KiB) TX bytes:21988 (21.4 KiB)
[root@test2 Desktop]#
网络接口配置文件
网络接口配置文件默认位于目录“/etc/sysconfig/network-scripts/”中,文件格式为“ifcfg-xxx”,其中“xxx”是网络接口的名称。
[root@bourn jason]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 //设置网络接口的名称
HWADDR=00:0C:29:3B:AF:22
TYPE=Ethernet
UUID=e8defc61-064f-44a5-8772-b1bf4cc3cec8
ONBOOT=yes //设置网络接口是否在Linux系统启动时激活
NM_CONTROLLED=yes //一般此项设定为no
BOOTPROTO=static //设置网络接口的配置方式,分为“static”和“dhcp”
IPADDR=192.168.0.103 //设置网络接口的IPD地址
NETMASK= //设置网络接口的子网掩码
GATEWAY= //设置网络接口的默认网关地址
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/sysconfig/network-scripts/ifcfg-eth0" 9L, 161C 1,1 All
启用、禁用网络接口配置
- 当修改了网络接口的配置文件以后,若要使新的配置生效,可以重新启动network服务或者重启主机。默认情况下,重启network服务将会先关闭所有的网络接口,然后再根据配置文件重新启用所有的网络接口
service network restart
- 如果只是想禁用、启用某一个网络接口,可以分别使用两个接口控制脚本ifdown、ifup。例如,执行以下操作将会关闭eth0,然后在启用eth1网卡
- ifdown eth1
- ifup eth1
xshell中操作
[root@bourn jason]# ifdown eth0 //xshell中关掉eth0
Connection closed by foreign host.
Disconnected from remote host(192.168.0.103:23) at 16:55:40.
Type `help' to learn how to use Xshell prompt.
[c:\~]$ telnet 192.168.0.103
注意远程连接主机的的时候不要使用ifdown、ifup,而是使用service network restart,这样连接不会断开。
虚拟机操作
[root@test2 Desktop]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:3B:AF:22
inet6 addr: fe80::20c:29ff:fe3b:af22/64 Scope:Link //原来在第二行的ipv4地址消失了
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4462 errors:0 dropped:0 overruns:0 frame:0
TX packets:1912 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:313237 (305.8 KiB) TX bytes:168526 (164.5 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:191 errors:0 dropped:0 overruns:0 frame:0
TX packets:191 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:13488 (13.1 KiB) TX bytes:13488 (13.1 KiB)
[root@test2 Desktop]# ifup etho
/sbin/ifup: configuration for etho not found. //命令输入错误,应该是eth0,而不是字母“o”
Usage: ifup <device name>
[root@test2 Desktop]# ifup eth0 //重新开启eth0,要注意,这个奏效的前提是上面的脚本中“BOOT=yes”
Active connection state: activated
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/3
[root@test2 Desktop]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:3B:AF:22
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0 //开启后可以看到ipv4的相关信息
inet6 addr: fe80::20c:29ff:fe3b:af22/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4462 errors:0 dropped:0 overruns:0 frame:0
TX packets:1912 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:313237 (305.8 KiB) TX bytes:168526 (164.5 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:207 errors:0 dropped:0 overruns:0 frame:0
TX packets:207 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:14704 (14.3 KiB) TX bytes:14704 (14.3 KiB)
[root@test2 Desktop]#
man ifup
service network restart
[root@test2 jason]# service network restart
Shutting down interface eth0: Device state: 3 (disconnected)
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Active connection state: activated
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/4
[ OK ]
[root@test2 jason]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:3B:AF:22
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe3b:af22/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4615 errors:0 dropped:0 overruns:0 frame:0
TX packets:2006 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:323109 (315.5 KiB) TX bytes:174263 (170.1 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:255 errors:0 dropped:0 overruns:0 frame:0
TX packets:255 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:18340 (17.9 KiB) TX bytes:18340 (17.9 KiB)
[root@test2 jason]#
从这里能看出来,远程连接没有因为网络服务重启而中断
man service
设置主机名
[root@test2 jason]# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=Daring //原来主机名是test2,现在更改为Daring
~
~
~
~
~
~ :wq
重启后
[jason@Daring ~]$ hostname //主机名更改为Daring
Daring
[jason@Daring ~]$
域名解析配置文件
方法一
[root@test2 jason]# vi /etc/resolv.conf
# Generated by NetworkManager
# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com
nameserver 202.106.0.20
~
~
~
~
~
~
~
~
~
~
~
~
~
:wq
方法二
[root@test2 jason]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
119.75.218.70 www.baidu.com
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:wq
windows当中的路径是C:\Windows\System32\drivers\etc