1、ping 发送TCMP回显请求报文,并等待返回TCMP回显应答。 ping [OPTIONS]... destination 这里的目标destination可以是目的IP地址或者域名/主机名 [root@centos7 ~]# ping -c 3 www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121: icmp_seq=1 ttl=52 time=1.35 ms 64 bytes from 61.135.169.121: icmp_seq=2 ttl=52 time=1.32 ms 64 bytes from 61.135.169.121: icmp_seq=3 ttl=52 time=1.22 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 1.225/1.303/1.359/0.064 ms 首先,ping程序会向域名服务器(DNS)发送请求,解析域名www.baidu.com的IP地址。DNS返回域名的一个别名www.a.shifen.com以及对应的IP地址61.135.169.121。之后ping程序开始向这个地址发送请求报文,每1s发送一个,ping收到ICMP回显应答并将结果显示在终端上,包括ICMP序列号(icmp_seq),生存时间(ttl)和数据包往返时间(time)。最后,给出汇总信息,包括报文总收发情况,总时间,往返时间最小值、平均值、最大值、平均偏差(越大说明网络越不稳定)。 [root@centos7 ~]# ping www.a.com ping: unknown host www.a.com 当目的域名无法解析出IP地址时,会报未知主机的错 [root@centos7 ~]# ping 192.168.0.1 PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. ^C #这里按CTRL+C键手动终止了进程 --- 192.168.0.1 ping statistics --- 6 packets transmitted, 0 received, 100% packet loss, time 4999ms 当目的IP地址没有路由时不会收到任何ICMP回显报文 [root@centos7 ~]# ping -c2 10.0.1.2 PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data. From 10.0.1.254 icmp_seq=1 Destination Host Unreachable From 10.0.1.254 icmp_seq=2 Destination Host Unreachable --- 10.0.1.2 ping statistics --- 2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 999ms pipe 2 当有目的IP的路由但无法达到时显示目标不可达错误(Destination Host Unreachable)。 2、hostname显示或设置系统主机名 hostname [OPTIONS]... [NAME] 直接执行命令hostname时将显示主机名: [root@centos7 temp]# hostname centos7 [root@centos7 temp]# 这个主机名是系统的gethostname(2)函数返回的。 [root@centos7 temp]# hostname NAME [root@centos7 temp]# hostname NAME 这个临时修改实际上是修改了linux kernel中一个同为hostname的内核参数,它保存在/proc/sys/kernel/hostname中。如果需要永久修改则需要修改配置文件/etc/sysconfig/network,centos7中需要修改/etc/hostname。需要注意的是,如果配置文件中的主机名是localhost或localhost.localdomain时,系统会取得网络接口的IP地址,并用这个地址找出/etc/hosts文件中对应的主机名,然后将其设置成最终的hostname。 3、host DNS查询 host name host命令通过配置文件/etc/resolv.conf中指定的DNS服务器查询name的IP地址: [root@centos7 temp]# host www.baidu.com www.baidu.com is an alias for www.a.shifen.com. www.a.shifen.com has address 61.135.169.121 www.a.shifen.com has address 61.135.169.125 4、dig DNS dig和host命令的语法一致,但提供了更详细的信息和更多的选项: [root@centos7 ~]# dig www.baidu.com ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.2 <<>> www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22125 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.baidu.com. IN A ;; ANSWER SECTION: www.baidu.com. 113 IN CNAME www.a.shifen.com. www.a.shifen.com. 113 IN A 61.135.169.125 www.a.shifen.com. 113 IN A 61.135.169.121 ;; Query time: 2 msec ;; SERVER: 223.5.5.5#53(223.5.5.5) ;; WHEN: 四 11月 10 12:31:20 CST 2016 ;; MSG SIZE rcvd: 90 [root@centos7 ~]# 如只查询域名的A记录并以短格式显示: [root@centos7 ~]# dig www.baidu.com A +short www.a.shifen.com. 61.135.169.125 61.135.169.121 [root@centos7 ~]# 或者: [root@centos7 ~]# dig +nocmd www.baidu.com A +noall +answer www.baidu.com. 252 IN CNAME www.a.shifen.com. www.a.shifen.com. 252 IN A 61.135.169.125 www.a.shifen.com. 252 IN A 61.135.169.121 还可以用@server的方式指定DNS服务器: [root@centos7 ~]# dig +noall +answer www.baidu.com A @8.8.8.8 www.baidu.com. 21 IN CNAME www.a.shifen.com. www.a.shifen.com. 263 IN A 61.135.169.125 www.a.shifen.com. 263 IN A 61.135.169.121 更多的命令及选项请自行man 5、traceroute或tracepath 路由跟踪 [root@centos7 ~]# tracepath www.baidu.com 1?: [LOCALHOST] pmtu 1500 1: 10.0.1.103 0.396ms 1: 10.0.1.103 0.350ms 2: 210.51.161.1 1.187ms asymm 3 3: 210.51.161.1 8.186ms 4: 210.51.175.81 1.117ms 5: 61.148.142.61 8.554ms asymm 12 6: 61.148.147.13 1.694ms asymm 12 7: 123.126.8.117 3.934ms asymm 10 8: 61.148.155.46 2.703ms asymm 10 .... 这里只列出部分输出,表示跟踪到目的地址的路由,每一跳都返回。 6、ifconfig 配置网络接口 当命令没有任何参数时显示所有网络接口的信息: [root@centos7 ~]# ifconfig ens32: flags=4163 本例中显示了两个网卡ens32和ens33以及环回口lo的信息,包括mtu,ip地址,掩码,mac地址,传输和接收数据量等等。 [root@idc-v-71253 ~]# ifconfig -s ens32 Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens32 1500 11996951 0 0 0 12 0 0 0 BMRU 如给ens33增加一个新地址10.0.1.4: [root@centos7 ~]# ifconfig ens33:0 10.0.1.4/24 up [root@centos7 ~]# ifconfig ens33:0 ens33:0: flags=4163 [root@centos7 ~]# ifconfig ens33:0 down 如果需要永久增加或修改当前接口的地址,最好直接编辑网卡配置文件/etc/sysconfig/network-scripts/ifcfg-ens33(其他系统换成相应文件)中IPADDR字段,然后重启网络systemctl restart network或service network restart生效。 7、arp和arping 命令arp显示系统的arp缓存,命令arping给邻居主机发送ARP请求。 [root@idc-v-71253 ~]# arp -a ? (10.0.1.1) at 68:8f:84:01:f1:ff [ether] on ens33 ? (10.0.1.102) at 00:50:56:a4:18:9a [ether] on ens33 ? (10.0.1.254) at 00:50:56:a4:a9:16 [ether] on ens33 ? (10.0.1.10) at 00:50:56:a4:d2:e4 [ether] on ens33 ? (10.0.1.104) at 00:50:56:a4:37:a7 [ether] on ens33 ?表示未知域名,最后的网卡名表示arp表项对应的网络接口 [root@centos7 ~]# arping 10.0.1.252 -I ens33 ARPING 10.0.1.252 from 10.0.1.254 ens33 Unicast reply from 10.0.1.252 [00:50:56:A4:65:71] 0.843ms Unicast reply from 10.0.1.252 [00:50:56:A4:0A:09] 1.034ms 这里两条返回信息中的MAC地址不同,说明有两块网卡配置了相同的IP地址。选项-I指定发送arp请求的网络接口。 [root@centos7 ~]# arping -c3 -I ens33 -s 10.0.1.254 10.0.1.1 ARPING 10.0.1.1 from 10.0.1.254 ens33 Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 19.466ms Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 2.358ms Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 24.305ms Sent 3 probes (1 broadcast(s)) Received 3 response(s) -c指定发送arp请求次数,-s指定源地址,最后的IP表示发送目标(这里是网关地址)。 8、route 显示或更改路由表 [root@centos7 ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 1002 0 0 ens32 link-local 0.0.0.0 255.255.0.0 U 1003 0 0 ens33 172.20.71.0 0.0.0.0 255.255.255.0 U 0 0 0 ens32 192.168.78.0 10.0.1.104 255.255.255.0 UG 0 0 0 ens33 其中Destination表示目的网段或目标主机;Gateway表示网关地址;Genmask表示目的网段的掩码;Flags表示路由标志:U表示路由是启用(up)的、G表示网关;Metric表示目标距离,通常用跳数表示;Ref表示路由的引用数;Use表示路由查找计数;Iface表示此条路由的出口。 route add -net 192.56.76.0 netmask 255.255.255.0 dev ens32 如增加一条默认路由,指明它的网关为10.0.1.1 route add default gw 10.0.1.1 如增加一条到172.20.70.0的路由,网关为10.0.1.2 route add -net 172.20.70.0/24 gw 10.0.1.2 如删除默认路由 route del default 9、telnet 提供远程登录功能 由于telnet协议使用明文传输,在要求安全登录的环境中并不适用。现在通常用它来进行网络服务的端口测试: [root@centos7 ~]# telnet 10.0.1.251 80 Trying 10.0.1.251... Connected to 10.0.1.251. Escape character is '^]'. ^] #这里按了CTRL+],也可以按CTRL+C强行退出。 telnet> quit Connection closed. 这里对方的80端口是开启并允许通信的。当对端端口没有开启时: [root@centos7 ~]# telnet 10.0.1.251 81 Trying 10.0.1.251... telnet: connect to address 10.0.1.251: No route to host 当对端拒绝连接时: [root@centos7 ~]# telnet 10.0.1.251 8085 Trying 10.0.1.251... telnet: connect to address 10.0.1.251: Connection refused 10、ssh 远程登录程序 ssh [OPTIONS]... [user@]hostname [command] ssh的全称是Secure Shell,在不安全的网络主机间提供安全加密的通信,旨在代替其他远程登录协议。 [root@centos7 ~]# ssh 10.0.1.253 The authenticity of host '10.0.1.253 (10.0.1.253)' can't be established. ECDSA key fingerprint is 96:bd:a3:a7:87:09:1b:53:44:4c:9b:b9:5f:b2:97:89. Are you sure you want to continue connecting (yes/no)? yes #这里输入yes Warning: Permanently added '10.0.1.253' (ECDSA) to the list of known hosts. [email protected]'s password: #这里输入密码 Last login: Fri Nov 11 09:04:01 2016 from 192.168.78.137 [root@idc-v-71253 ~]# #已登录 当命令ssh后直接跟主机IP时表示使用默认用户root登录,如果是首次登录,需要确认添加该主机的认证key,当输入yes后,即会在本机/root/.ssh/known_hosts中增加一条该主机的记录,下一次登录时就不用再次确认了。然后需要输入用户密码,通过验证之后,我们就获得了目的主机的一个shell,我们就可以在这个shell中执行命令了。 [root@centos7 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): #回车 Enter passphrase (empty for no passphrase): #回车 Enter same passphrase again: #回车 Your identification has been saved in /root/.ssh/id_rsa. #私钥 Your public key has been saved in /root/.ssh/id_rsa.pub. #公钥 The key fingerprint is: be:c3:d0:02:50:35:35:fe:60:d6:2f:26:96:f0:e1:e6 root@centos7 The key's randomart image is: +--[ RSA 2048]----+ | ...o.o | | . o o | | . . * . | | . * = . | | . .S + . | | o=.o . | | +E | | o. | | .. | +-----------------+ [root@centos7 ~]# [root@centos7 ~]# ssh-copy-id 10.0.1.253 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '10.0.1.253'" and check to make sure that only the key(s) you wanted were added. [root@centos7 ~]# 其中命令ssh-keygen用来生成公钥私钥,选项-t指明密钥类型。之后使用命令ssh-copy-id将公钥发送至目标主机,这里需要输入目标主机用户密码。然后就可以免密码登录了: [root@centos7 ~]# ssh 10.0.1.253 Last login: Fri Nov 11 11:08:37 2016 from 10.0.1.254 [root@idc-v-71253 ~]# 还可以通过ssh远程执行命令: [root@centos7 ~]# ssh 10.0.1.252 "hostname" [email protected]'s password: #输入密码 idc-v-71252 #显示命令结果 [root@centos7 ~]# #并不登录 或者手动将公钥拷贝至目标主机: [root@centos7 ~]# cat /root/.ssh/id_rsa.pub | ssh 10.0.1.252 "cat - >> /root/.ssh/authorized_keys" [email protected]'s password: #输入密码 [root@centos7 ~]# ssh 10.0.1.252 #免密登录 Last login: Thu Nov 10 14:42:11 2016 from 192.168.78.135 [root@idc-v-71252 ~]# 选项-p为登录指定端口: [root@centos7 temp]# ssh -p22 10.0.1.252 Last login: Fri Nov 11 11:44:31 2016 from 10.0.1.254 [root@idc-v-71252 ~]# 端口设置在服务端配置文件/etc/ssh/sshd_config中,默认端口号为22,如更改需将#Port 22去掉注释并将22更改为需要的端口,然后重启sshd服务service sshd restart或systemctl restart sshd。 tar zc /home/temp | ssh user@host "tar xz" #本地temp目录备份到远程 ssh user@host "tar cz /home/temp" | tar xz #远程temp目录备份到本地 选项-L [bind_address:]port:host:hostport设置本地端口转发 [root@centos7 ~]# ssh -L 2222:10.0.1.252:22 10.0.1.253 Last login: Mon Nov 14 10:34:43 2016 from 10.0.1.254 [root@idc-v-71253 ~]# #注意如果这里exit断开连接,则此转发也将终止。 此命令的意思是绑定本地端口2222,并将所有发送至此端口的数据通过中间主机10.0.1.253转发至目标主机10.0.1.252的22端口,此时如果用ssh登录本机的2222端口,则实际登录的是主机10.0.1.252 [root@centos7 ~]# ssh -p 2222 127.0.0.1 Last login: Mon Nov 14 10:34:56 2016 from 10.0.1.253 [root@idc-v-71252 ~]# 这里默认绑定的是本机的环回口127.0.0.1,如绑 |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|