首页 > 代码库 > linux系统基础网络配置
linux系统基础网络配置
1.修改主机名
临时方法:
退出当前shell重新登录即可生效。此法只能临时修改生效。重启系统后失效。
提示:很多人使用hostname主机名来修改,其实这个只是作为暂时的。重启后将恢复到配置前的主机名
永久方法:
[root@greymouster ~]# hostname greymouster
提示:这里修改完后,执行/etc/init.d/network restart 或 source /etc/sysconfig/network等做法都不会生效,如果要单一的修改这个文件可能就需要重启服务器了
[root@greymouster ~]# reboot
重启后就永久生效了
修改 /etc/hosts 里面对应的主机名也要修改下
[root@greymouster ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 greymouster ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
提示:如果/etc/hosts不改,以后会遇到一些问题,如sendmail启动缓慢,ldap服务解析缓慢,sudo切用户缓慢等等都是主机名和/etc/hosts中的解析不对应导致的
2)网卡配置文件说明:
[root@greymouster ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #这里是网卡名称第一块网卡为eth0,第二块为eth1.... HWADDR=00:0C:29:1F:B7:7B #这里是mac地址,不同的机器不能重复,尤其是在克隆虚拟机或复制网卡配置时要注意 TYPE=Ethernet #类型 UUID=938bff60-0b1e-4ed3-9ff8-74eb8ba0ed57 ONBOOT=yes #开机网卡自启动 NM_CONTROLLED=no
DNS1:8.8.8.8 #DNS
DNS2:202.106.0.20 #DNS BOOTPROTO=static #是否是静态的ip地址 IPADDR=192.168.1.169 #ip地址 NETMASK=255.255.255.0 #子网掩码 GATEWAY=192.168.1.1 #网关的配置,也可以命令行通过route添加删除 [root@greymouster ~]#
3)配置修改ip地址:
配置服务器ip及DNS等网络配置的方法:
直接编辑上面的配置文件
IPADDR=192.168.1.169
[root@greymouster ~]# /etc/init.d/network restart 正在关闭接口 eth0: [确定] 关闭环回接口: [确定] 弹出环回接口: [确定] 弹出界面 eth0: Determining if ip address 192.168.1.169 is already in use for device eth0... [确定] [root@greymouster ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:1F:B7:7B inet addr:192.168.1.169 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1f:b77b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:42845 errors:3 dropped:0 overruns:0 frame:0 TX packets:2711 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3406987 (3.2 MiB) TX bytes:332569 (324.7 KiB) Interrupt:19 Base address:0x2000 [root@greymouster ~]#
ifup eth0 启动网卡
ifdown eth0 关闭网卡
临时配置两个ip 重启后会失效
[root@greymouster ~]# ifconfig eth0:0 192.168.1.168 netmask 255.255.255.0 [root@greymouster ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:1F:B7:7B inet addr:192.168.1.169 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1f:b77b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:43371 errors:3 dropped:0 overruns:0 frame:0 TX packets:2832 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3458368 (3.2 MiB) TX bytes:347403 (339.2 KiB) Interrupt:19 Base address:0x2000 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:1F:B7:7B inet addr:192.168.1.168 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:19 Base address:0x2000 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:65536 Metric:1 RX packets:9 errors:0 dropped:0 overruns:0 frame:0 TX packets:9 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:612 (612.0 b) TX bytes:612 (612.0 b) [root@greymouster ~]#
关闭临时ip: ifconfig eth0:0 down
要想永久生效 就要复制配置文件改为eth0:0
[root@greymouster ~]# cd /etc/sysconfig/network-scripts/ [root@greymouster network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0 [root@greymouster network-scripts]# vi ifcfg-eth0:0 DEVICE=eth0:0 HWADDR=00:0C:29:1F:B7:7B TYPE=Ethernet UUID=938bff60-0b1e-4ed3-9ff8-74eb8ba0ed57 ONBOOT=yes NM_CONTROLLED=no BOOTPROTO=static IPADDR=192.168.1.168 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 ~
[root@greymouster network-scripts]# /etc/init.d/network restart
4)配置修改DNS:
[root@greymouster network-scripts]# cat /etc/resolv.conf nameserver 8.8.8.8 nameserver 202.106.0.20
因为centos6.8以上 只要/etc/sysconfig/network-scripts/ifcfg-eth0 配置DNS1 重启后就会同步到 /etc/resolv.conf里面 所以在/etc/sysconfig/network-scripts/ifcfg-eth0 配置DNS一样
5)查看网关
[root@greymouster network-scripts]# route -n 查看 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
#下面的命令是临时生效的
[root@greymouster network-scripts]# route del default gw 192.168.1.1 删除网关
[root@greymouster network-scripts]# route add default gw 192.168.1.1 添加网关
网关首先看/etc/sysconfig/network/ifcfg-eth0里面的GATEWAY 如果没有则找/etc/sysconfig/network下的GATEWAY
[root@greymouster ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=greymouster GATEWAY=192.168.1.1 [root@greymouster ~]#
提示:如果/etc/sysconfig/network/ifcfg-eth0里面的GATEWAY配置了 则先生效
#第一生效文件
[root@greymouster ~]# grep -i gate /etc/sysconfig/network-scripts/ifcfg-eth0 GATEWAY=192.168.1.1
#第二生效文件 [root@greymouster ~]# grep -i gate /etc/sysconfig/network GATEWAY=192.168.1.1 [root@greymouster ~]#
查看DNS域名解析
[root@greymouster ~]# dig www.baidu.com
[root@greymouster ~]# nslookup
linux 网络检查:ping
[root@greymouster ~]# traceroute www.baidu.com
windows:跟踪路由
cmd: tracert -d www.baidu.com
tcpdump 抓包:
[root@greymouster ~]# host www.baidu.com www.baidu.com is an alias for www.a.shifen.com. www.a.shifen.com has address 119.75.217.109 www.a.shifen.com has address 119.75.218.70
linux系统基础网络配置