您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
查看网络设置
测试网络连接
本文主要介绍Linux中一些基础常用的Linux网络设置命令与一些脚本文件配置步骤。
格式:ifconfig [网络接口名] [ip地址](子网掩码)
[root@lokott ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 3556 bytes 232528 (227.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 104 bytes 16400 (16.0 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 1 (Local Loopback)
RX packets 56 bytes 6144 (6.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 56 bytes 6144 (6.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:71:37:09 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 3560 bytes 232768 (227.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 104 bytes 16400 (16.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
格式:hostname
[root@lokott ~]# hostname
lokott
[root@lokott ~]# cat /etc/hostname
lokott
[root@lokott ~]# echo "local" > /etc/hostname
[root@lokott ~]# cat /etc/hostname
local
[root@lokott ~]#需要重启才可以设置成功,另一种方法是使用hostnamectl set-hostname 主机名^c
[root@lokott ~]#reboot
[root@local ~]# hostnamectl set-hostname lokott
[root@local ~]# su
[root@lokott ~]# 换为lokott了^c
-n:数字显示(centos6中,7中加不加几乎一样)
[root@lokott ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.68.2 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
-n 序列号
-p 端口号
-a 所有服务端口
-r 显示路由表信息
-l 显示处于监听状态的网络连接及端口信息
-t tcp
-u udp
[root@lokott ~]# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1490/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 941/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 939/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1186/master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 941/sshd
tcp6 0 0 ::1:631 :::* LISTEN 939/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1186/master
[root@lokott ~]# netstat -nuap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 0.0.0.0:18066 0.0.0.0:* 3935/dhclient
udp 0 0 192.168.122.1:53 0.0.0.0:* 1490/dnsmasq
udp 0 0 0.0.0.0:67 0.0.0.0:* 1490/dnsmasq
udp 0 0 0.0.0.0:68 0.0.0.0:* 3935/dhclient
udp 0 0 0.0.0.0:5353 0.0.0.0:* 553/avahi-daemon: r
udp 0 0 0.0.0.0:50467 0.0.0.0:* 553/avahi-daemon: r
udp6 0 0 :::61206 :::* 3935/dhclient
[root@lokott ~]# netstat -r //与route一样
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default gateway 0.0.0.0 UG 0 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# traceroute 127.0.0.1
traceroute to 127.0.0.1 (127.0.0.1), 30 hops max, 60 byte packets
1 localhost (127.0.0.1) 0.030 ms 0.007 ms 0.035 ms
[root@lokott ~]# traceroute 192.168.10.11
traceroute to 192.168.10.11 (192.168.10.11), 30 hops max, 60 byte packets
1 gateway (192.168.68.2) 0.154 ms 0.085 ms 0.087 ms
2 * * *
3 * * *
4 * * *
5 * * *
6 * * *
7 *^C
[root@lokott ~]#
[root@lokott ~]# nslookup www.taobao.com
Server: 192.168.68.2
Address: 192.168.68.2#53
Non-authoritative answer:
www.taobao.com canonical name = www.taobao.com.danuoyi.tbcache.com.
Name: www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.235
Name: www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.234
Name: www.taobao.com.danuoyi.tbcache.com
Address: 211.138.124.238
补充:在Windows系统的dos界面也一样适用
1.临时配置——调试网络时使用
2.固定配置——更改配置文件,需要重启network服务或重启主机后才会生效,相当于永久配置。
[root@lokott ~]# ifconfig ens33:0 192.168.155.55 //虚接口配置
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.155.55 netmask 255.255.255.0 broadcast 192.168.155.255
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
[root@lokott ~]# ifconfig ens33:0 down
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
[root@lokott ~]# ifconfig ens33:0 192.168.155.55
[root@lokott ~]# ifdown ens33:0
用法:ifdown <设备名>
[root@lokott ~]# ifdown ens33
成功断开设备 'ens33'。
[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 5048 bytes 329861 (322.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 490 bytes 46907 (45.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
[root@lokott ~]# ifup ens33
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 5082 bytes 332183 (324.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 514 bytes 50940 (49.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@lokott ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route add -net 192.168.10.0/24 gw 192.168.122.1
[root@lokott ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.68.2 0.0.0.0 UG 100 0 0 ens33
192.168.10.0 192.168.122.1 255.255.255.0 UG 0 0 0 virbr0
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route del -net 192.168.10.0/24 //删除路由记录
[root@lokott ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.68.2 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route del default gw 192.168.68.2 //删除默认网关
[root@lokott ~]# route add default gw 192.168.68.2 //添加默认网关
[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet" //类型为以太网
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp" //动态获取
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33" //网卡名字为ens33
UUID="5785d9d2-2603-4b14-bbfb-d95f6545e260"
DEVICE="ens33" //设置网络接口名称为ens33
ONBOOT="yes" //设置网络接口在Linux系统启动时激活
[root@lokott ~]#
当然我们可以设置静态ip地址如下(务必重启网络服务):
[root@lokott ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="5785d9d2-2603-4b14-bbfb-d95f6545e260"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.68.130"
NETMASK="255.255.255.0"
GATEWAY="192.168.68.2"
[root@lokott ~]# systemctl restart network
[root@lokott ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 5803 bytes 377581 (368.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 552 bytes 55914 (54.6 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 1 (Local Loopback)
RX packets 168 bytes 16496 (16.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 168 bytes 16496 (16.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:71:37:09 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@lokott ~]#
1)指定为本机提供DNS解析的服务器地址信息
[root@lokott ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.68.2
[root@lokott ~]#
2)本地主机映射文件
[root@lokott ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
一般可以将经常需要访问的主机信息存放在这里,根据这里的映射表可以查找是否有与之对应的记录,没有再去DNS服务器查询,从而提高上网速度。但如果错误添加就会出现异常。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。