本篇写一些关于linux
网络相关的基础命令、配置等。
1.查看主机名
[[email protected] ~]# hostnamelocalhost.localdomain
2.临时修改主机名
[[email protected] ~]# hostname server[[email protected] ~]# bash[[email protected] ~]# hostnameserver[[email protected] ~]# cat /etc/hostnamelocalhost.localdomain[[email protected] ~]# hostnamectl status Static hostname: localhost.localdomainTransIEnt hostname: server Icon name: computer-vm Chassis: vm Machine ID: ec132d04a74d4b7e828b3905a6b83437 Boot ID: 8d0046a6366944b08b3eb7e69b431f6b Virtualization: vmware Operating System: CentOS linux 7 (Core) CPE OS name: cpe:/o:centos:centos:7 Kernel: linux 3.10.0-693.el7.x86_64 Architecture: x86-64
3.永久修改主机名
[[email protected] ~]# hostnamectl set-hostname clIEnt[[email protected] ~]# bash[[email protected] ~]# hostnameclIEnt[[email protected] ~]# cat /etc/hostnameclIEnt[[email protected] ~]# hostnamectl status Static hostname: clIEnt Icon name: computer-vm Chassis: vm Machine ID: ec132d04a74d4b7e828b3905a6b83437 Boot ID: 8d0046a6366944b08b3eb7e69b431f6b Virtualization: vmware Operating System: CentOS linux 7 (Core) CPE OS name: cpe:/o:centos:centos:7 Kernel: linux 3.10.0-693.el7.x86_64 Architecture: x86-64ifconfig
1.查看已启用的网络接口信息
[[email protected] ~]# ifconfigens33: flags=4163<UP,broADCAST,RUNNING,MulTICAST> mtu 1500 inet 192.168.28.128 netmask 255.255.255.0 broadcast 192.168.28.255 inet6 fe80::605e:3c48:bafd:e550 prefixlen 64 scopeID 0x20<link> ether 00:0c:29:bc:ab:96 txqueuelen 1000 (Ethernet) RX packets 471 bytes 520023 (507.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 241 bytes 19798 (19.3 KiB) TX errors 0 dropped 0 overruns 0 carrIEr 0 collisions 0lo: 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 68 bytes 5916 (5.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 68 bytes 5916 (5.7 KiB) TX errors 0 dropped 0 overruns 0 carrIEr 0 collisions 0virbr0: flags=4099<UP,MulTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:c3:55:5a 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
ens33
:第一块以太网卡的名称。ens33
中的en
是EtherNet
的缩写,表示网卡类型为以太网,s
表示热插拔插槽上的设备hot-plug Slot
,数字33
表示插槽编号。
lo
:回环网络接口,lo
是loopback
的缩写,它不代表真正的网络接口,而是一个虚拟的网络接口,其IP
地址默认是127.0.0.1
。回环地址通常仅用于对本机的网络测试。
virbr0
:虚拟网桥的连接接口,默认为0
号。其作用是连接主机上的虚机网卡提供外网的功能。
2.查看指定的网络接口信息
[[email protected] ~]# ifconfig ens33ens33: flags=4163<UP,MulTICAST> mtu 1500 inet 192.168.28.129 netmask 255.255.255.0 broadcast 192.168.28.255 inet6 fe80::eb90:4805:2c20:18ac prefixlen 64 scopeID 0x20<link> ether 00:0c:29:5b:e0:09 txqueuelen 1000 (Ethernet) RX packets 899 bytes 207777 (202.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 205 bytes 26253 (25.6 KiB) TX errors 0 dropped 0 overruns 0 carrIEr 0 collisions 0
inet
:表示网络接口的IP
地址。
netmask
:表示网络接口的子网掩码。
broadcast
:表示网络接口所在网络的广播地址。
ether
:表示网络接口的MAC
地址。
3.设置网络接口的IP地址、子网掩码
ifconfig 接口名 IP地址 [netmask 子网掩码]
ifconfig 接口名 IP地址[/掩码长度]
[[email protected] ~]# ifconfig ens33 192.168.100.10 netmask 255.255.255.0[[email protected] ~]# ifconfig ens33ens33: flags=4163<UP,MulTICAST> mtu 1500 inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::605e:3c48:bafd:e550 prefixlen 64 scopeID 0x20<link> ether 00:0c:29:bc:ab:96 txqueuelen 1000 (Ethernet) RX packets 626 bytes 562243 (549.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 262 bytes 23344 (22.7 KiB) TX errors 0 dropped 0 overruns 0 carrIEr 0 collisions 0
临时配置,重启失效。不指定子网掩码时,将使用
IP
地址所在分类的默认子网掩码。
4.临时禁用或者重新激活网卡
ifconfig 接口名 down[[email protected] ~]# ifconfig ens33 down[[email protected] ~]# ifconfig ens33ens33: flags=4098<broADCAST,MulTICAST> mtu 1500 ether 00:0c:29:bc:ab:96 txqueuelen 1000 (Ethernet) RX packets 1067 bytes 667057 (651.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 263 bytes 23404 (22.8 KiB) TX errors 0 dropped 0 overruns 0 carrIEr 0 collisions 0ifconfig 接口名 up
[[email protected] ~]# ifconfig ens33 up[[email protected] ~]# ifconfig ens33ens33: flags=4163<UP,MulTICAST> mtu 1500 inet 192.168.28.128 netmask 255.255.255.0 broadcast 192.168.28.255 inet6 fe80::605e:3c48:bafd:e550 prefixlen 64 scopeID 0x20<link> ether 00:0c:29:bc:ab:96 txqueuelen 1000 (Ethernet) RX packets 1078 bytes 668954 (653.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 285 bytes 26835 (26.2 KiB) TX errors 0 dropped 0 overruns 0 carrIEr 0 collisions 0
5.设置虚拟网络接口
ifconfig 接口名:序号 IP地址[[email protected] ~]# ifconfig ens33:0 192.168.100.10[[email protected] ~]# ifconfig ens33:0ens33:0: flags=4163<UP,MulTICAST> mtu 1500 inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255 ether 00:0c:29:bc:ab:96 txqueuelen 1000 (Ethernet)[[email protected] ~]# ifconfig ens33:0 downroute
1.查看当前主机中的路由表信息
-n
:将路由记录中的地址显示位数字形式
[[email protected] ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Ifacedefault promote.cache-d 0.0.0.0 UG 100 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
2.添加、删除到指定网段的路由记录
route add -net 网段地址 gw IP地址[[email protected] ~]# route add -net 192.168.100.0/24 gw 192.168.28.1[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33192.168.100.0 192.168.28.1 255.255.255.0 UG 0 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0route del -net 网段地址
[[email protected] ~]# route del -net 192.168.100.0/24[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
3。添加、删除默认网关记录
route add default gw IP地址[[email protected] ~]# route add default gw 192.168.28.1[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.28.1 0.0.0.0 UG 0 0 0 ens330.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0route del default gw IP地址
[[email protected] ~]# route del default gw 192.168.28.1[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0netstat 查看当前的网络连接状态、路由表、接口统计等信息。
-n
:以数字的形式显示相关的主机地址、端口等信息。-t
:查看TCP
传输控制协议相关的信息。-u
:显示UDP
用户数据报协议相关的信息。-a
:显示当前主机中所有活动的网络连接信息,包括监听、非监听状态的服务端口。-l
:显示处于监听Listening
状态的网络连接及端口信息。-p
:显示与网络连接相关联的进程号、进程名称信息,该选项需要root
权限。-r
:显示路由表信息。
[[email protected] ~]# netstat -ntapActive 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 1335/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* ListEN 1009/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* ListEN 1012/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* ListEN 1306/master tcp6 0 0 :::111 :::* ListEN 1/systemd tcp6 0 0 :::22 :::* ListEN 1009/sshd tcp6 0 0 ::1:631 :::* ListEN 1012/cupsd tcp6 0 0 ::1:25 :::* ListEN 1306/master
[[email protected] ~]# netstat -nuapActive 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:5353 0.0.0.0:* 578/avahi-daemon: r udp 0 0 0.0.0.0:12162 0.0.0.0:* 799/dhclIEnt udp 0 0 192.168.122.1:53 0.0.0.0:* 1335/dnsmasq udp 0 0 0.0.0.0:67 0.0.0.0:* 1335/dnsmasq udp 0 0 0.0.0.0:68 0.0.0.0:* 799/dhclIEnt udp 0 0 127.0.0.1:323 0.0.0.0:* 624/chronyd udp 0 0 0.0.0.0:51570 0.0.0.0:* 578/avahi-daemon: r udp6 0 0 :::25751 :::* 799/dhclIEnt udp6 0 0 ::1:323 :::* 624/chronyd
[[email protected] ~]# netstat -rKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Ifacedefault promote.cache-d 0.0.0.0 UG 0 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0[[email protected] ~]# netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface0.0.0.0 192.168.28.2 0.0.0.0 UG 0 0 0 ens33192.168.28.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0Ping 测试网络连通性
[[email protected] ~]# Ping -c 4 127.0.0.1Ping 127.0.0.1 (127.0.0.1) 56(84) bytes of data.64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.036 ms64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.084 ms64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.085 ms64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.096 ms--- 127.0.0.1 Ping statistics ---4 packets transmitted,4 received,0% packet loss,time 3009msrtt min/avg/max/mdev = 0.036/0.075/0.096/0.023 mstraceroute 跟踪数据包的路由途径
[[email protected] ~]# traceroute 127.0.0.1traceroute to 127.0.0.1 (127.0.0.1),30 hops max,60 byte packets 1 localhost (127.0.0.1) 0.040 ms 0.010 ms 0.008 ms
traceroute
命令能够比Ping
命令更加准确地定位网络连接的故障点,因此执行速度会比Ping
命令稍慢。在网络测试与排错过程中,通常会先使用Ping
命令测试与目的主机的网络连接,如果发现网络连接有故障,再使用traceroute
命令跟踪查看是在哪个中间结点存在故障。
[[email protected] ~]# nslookup www.baIDu.comServer: 192.168.28.2Address: 192.168.28.2#53Non-authoritative answer:www.baIDu.com canonical name = www.a.shifen.com.name: www.a.shifen.comAddress: 183.232.231.174name: www.a.shifen.comAddress: 183.232.231.172ifcfg-* 修改网络接口配置文件
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33TYPE=EthernetBOOTPROTO=staticDEVICE=ens33ONBOOT=yesIPADDR=192.168.100.10NETMASK=255.255.255.0GATEWAY=192.168.100.1
TYPE
:设置网卡类型,Ethernet
表示以太网。BOOTPROTO
:设置网络接口的配置方式,值为static
时表示使用静态指定的IP
地址,为dhcp
时表示通过DHCP
的方式动态获取地址。DEVICE
:设置网络接口的名称。ONBOOT
:设置网络接口是否在linux
*** 作系统启动时激活。IPADDR
:设置网络接口的IP
地址。NETMASK
:设置网络接口的子网掩码。GATEWAY
:设置网络接口的默认网关地址。
[[email protected] ~]# ifdown ens33Device 'ens33' successfully disconnected.[[email protected] ~]# ifup ens33Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)[[email protected] ~]# ifconfig ens33ens33: flags=4163<UP,MulTICAST> mtu 1500 inet 192.168.100.10 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::20c:29ff:febc:ab96 prefixlen 64 scopeID 0x20<link> ether 00:0c:29:bc:ab:96 txqueuelen 1000 (Ethernet) RX packets 5640 bytes 1889047 (1.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 410 bytes 37045 (36.1 KiB) TX errors 0 dropped 0 overruns 0 carrIEr 0 collisions 0重启网络服务(会先关闭所有的网络接口,再根据配置文件重新启用所有的网络接口)
[[email protected] ~]# service network restartRestarting network (via systemctl): [ OK ]
[[email protected] ~]# systemctl restart network/etc/hosts 本地主机映射文件
[[email protected] ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
/etc/resolv.conf 指定为本机提供DNS解析的服务器地址,最多可以指定当访问一个未知的域名时,先查找该文件中是否有相应的映射记录,如果找不到再去向
DNS
服务器查询。
3
个,第3
个后面的会被忽略。 [[email protected] ~]# cat /etc/resolv.conf# Generated by NetworkManagersearch localdomainnameserver 192.168.28.2
search localdomain
:设置默认的搜索域(域名扩展名)。
例如,当访问主机localhost
时,就相当于访问localhost.localdomain
。
本地 → 对方
[[email protected] ~]# scp /etc/hosts [email protected]:/etc/hostsThe authenticity of host '192.168.28.129 (192.168.28.129)' can't be established.ECDSA key fingerprint is SHA256:QmZtJT0piBUSkF9P3GfYf3uEogzBWs08sI7j0eBE/cI.ECDSA key fingerprint is MD5:ef:e6:06:22:8a:0f:24:00:f8:af:a5:59:5b:a2:b8:b1.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.28.129' (ECDSA) to the List of kNown hosts.[email protected]'s password: hosts 100% 158 172.5KB/s 00:00
对方 → 本地
[[email protected] ~]# scp [email protected]:/etc/hosts /etc/hosts[email protected]'s password: hosts 100% 158 90.5KB/s 00:00总结
以上是内存溢出为你收集整理的Linux 网络基础全部内容,希望文章能够帮你解决Linux 网络基础所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)