Linux 网络基础
本篇写一些关于 hostname1.查看主机名 [[email?protected] ~]# hostname localhost.localdomain 2.临时修改主机名 [[email?protected] ~]# hostname server [[email?protected] ~]# bash [[email?protected] ~]# hostname server [[email?protected] ~]# cat /etc/hostname localhost.localdomain [[email?protected] ~]# hostnamectl status Static hostname: localhost.localdomain Transient 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] ~]# hostname client [[email?protected] ~]# cat /etc/hostname client [[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-64 ifconfig1.查看已启用的网络接口信息 [[email?protected] ~]# ifconfig ens33: 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 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 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 0 virbr0: 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
2.查看指定的网络接口信息 [[email?protected] ~]# ifconfig ens33 ens33: 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
3.设置网络接口的IP地址、子网掩码
[[email?protected] ~]# ifconfig ens33 192.168.100.10 netmask 255.255.255.0 [[email?protected] ~]# ifconfig ens33 ens33: 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
4.临时禁用或者重新激活网卡
[[email?protected] ~]# ifconfig ens33 down [[email?protected] ~]# ifconfig ens33 ens33: 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 0
[[email?protected] ~]# ifconfig ens33 up [[email?protected] ~]# ifconfig ens33 ens33: 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.设置虚拟网络接口
[[email?protected] ~]# ifconfig ens33:0 192.168.100.10 [[email?protected] ~]# ifconfig ens33:0 ens33: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 down route1.查看当前主机中的路由表信息
[[email?protected] ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default promote.cache-d 0.0.0.0 UG 100 0 0 ens33 192.168.28.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 [[email?protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33 192.168.28.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 2.添加、删除到指定网段的路由记录
[[email?protected] ~]# route add -net 192.168.100.0/24 gw 192.168.28.1 [[email?protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33 192.168.28.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.100.0 192.168.28.1 255.255.255.0 UG 0 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[[email?protected] ~]# route del -net 192.168.100.0/24 [[email?protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33 192.168.28.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 3。添加、删除默认网关记录
[[email?protected] ~]# route add default gw 192.168.28.1 [[email?protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.28.1 0.0.0.0 UG 0 0 0 ens33 0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33 192.168.28.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
[[email?protected] ~]# route del default gw 192.168.28.1 [[email?protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.28.2 0.0.0.0 UG 100 0 0 ens33 192.168.28.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 netstat
[[email?protected] ~]# netstat -ntap 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 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 -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: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 -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface default promote.cache-d 0.0.0.0 UG 0 0 0 ens33 192.168.28.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 [[email?protected] ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.28.2 0.0.0.0 UG 0 0 0 ens33 192.168.28.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 ping
[[email?protected] ~]# ping -c 4 127.0.0.1 PING 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 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.084 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.085 ms 64 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 3009ms rtt min/avg/max/mdev = 0.036/0.075/0.096/0.023 ms traceroute
[[email?protected] ~]# 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.040 ms 0.010 ms 0.008 ms
nslookup
[[email?protected] ~]# nslookup www.baidu.com Server: 192.168.28.2 Address: 192.168.28.2#53 Non-authoritative answer: www.baidu.com canonical name = www.a.shifen.com. Name: www.a.shifen.com Address: 183.232.231.174 Name: www.a.shifen.com Address: 183.232.231.172 ifcfg-*
[[email?protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet BOOTPROTO=static DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.10 NETMASK=255.255.255.0 GATEWAY=192.168.100.1
[[email?protected] ~]# ifdown ens33 Device 'ens33' successfully disconnected. [[email?protected] ~]# ifup ens33 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) [[email?protected] ~]# ifconfig ens33 ens33: 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 restart Restarting network (via systemctl): [ OK ] [[email?protected] ~]# systemctl restart network /etc/hosts
[[email?protected] ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
/etc/resolv.conf
[[email?protected] ~]# cat /etc/resolv.conf # Generated by NetworkManager search localdomain nameserver 192.168.28.2
例如,当访问主机 scp
本地 → 对方 [[email?protected] ~]# scp /etc/hosts [email?protected]:/etc/hosts The 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)? yes Warning: 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |