Linux学习笔记-网络命令

1.12.网络命令

1.12.1.write

给指定用户发信息,以Ctrl+D保存结束并发送

以root用户给test用户发条信息

[root@localhost cn]# write test

i missing you.

[root@localhost cn]#

test收到的消息

[test@localhost ~]$

Message from [email protected] on pts/0 at 21:58 ...

i missing you.

EOF

该命令发消息时,用户必须在线才能发,否则发出去的。

可以使用w看有哪些用户在线

1.12.2.wall

英文愿意:write all

发广播消息

语法:wall [message]

发送后,自己收到一份

[root@localhost cn]# wall hello world!

[root@localhost cn]#

Broadcast message from [email protected] (pts/0) (Fri Apr 27 22:01:56 2018):

hello world!

test用户也收到一份

Broadcast message from [email protected] (pts/0) (Fri Apr 27 22:01:56 2018):

hello world!

1.12.3.ping

测试网络连通性

ping 选项 IP地址

-c 指定发送次数

默认不会停止,一直发送数据包,必须Ctrl+C才能结束

[root@localhost cn]# ping 192.168.1.1

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.913 ms

64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.54 ms

64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.804 ms

64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.797 ms

^C

--- 192.168.1.1 ping statistics ---

10 packets transmitted, 10 received, 0% packet loss, time 9006ms

rtt min/avg/max/mdev = 0.735/0.947/1.542/0.225 ms

packet loss 丢包率,看以判断网络质量。

使用-c选项,指定发送次数

[root@localhost cn]# ping -c 2 192.168.1.1

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.856 ms

64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.874 ms

--- 192.168.1.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 0.856/0.865/0.874/0.009 ms

[root@localhost cn]#

1.12.4.ifconfig

英文原意:interface configure

查看和设置网卡信息

语法:ifconfig 网卡名称 IP地址

查看网卡

[root@localhost cn]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.1.200  netmask 255.255.255.0  broadcast 192.168.1.255

        inet6 fe80::87e1:e178:41b3:d5b1  prefixlen 64  scopeid 0x20<link>

        ether 00:15:5d:01:65:06  txqueuelen 1000  (Ethernet)

        RX packets 4592  bytes 859283 (839.1 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 1655  bytes 160274 (156.5 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 5  bytes 560 (560.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 5  bytes 560 (560.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo回环网卡

RX packets 接收数据包及大小

TX packets 发送数据包及大小

设置IP,临时更改,非永久修改

范例:

ifconfig eth0 192.168.1.202

1.12.5.mail

查看发送电子邮件

这个比write好的是,不需要用户必须在线。

以Ctrl+D保存结束并发送邮件

mail命令的使用需要安装mailx安装包

[root@localhost cn]# yum install mailx

给root用户发送一封邮件

[root@localhost cn]# mail root

Subject: test subject

hi.

EOT

查看发送的邮件

[root@localhost cn]# mail

Heirloom Mail version 12.5 7/5/10.  Type ? for help.

"/var/spool/mail/root": 1 message 1 new

>N  1 root                  Fri Apr 27 22:18  18/610   "test subject"

N表示未读

1 表示邮件的ID

root 是发送用户

发送时间

邮件标题

进入邮件查看交互命令下,以&开头命令

输入help

可以查看命令帮助

& help

               mail commands

type <message list>             type messages

next                            goto and type next message

from <message list>             give head lines of messages

headers                         print out active message headers

delete <message list>           delete messages

undelete <message list>         undelete messages

save <message list> folder      append messages to folder and mark as saved

copy <message list> folder      append messages to folder without marking them

write <message list> file       append message texts to file, save attachments

preserve <message list>         keep incoming messages in mailbox even if saved

Reply <message list>            reply to message senders

reply <message list>            reply to message senders and all recipients

mail addresses                  mail to specific recipients

file folder                     change to another folder

quit                            quit and apply changes to folder

xit                             quit and discard changes made to folder

!                               shell escape

cd <directory>                  chdir to directory or home if none given

list                            list names of all available commands

A <message list> consists of integers, ranges of same, or other criteria

separated by spaces.  If omitted, mail uses the last message typed.

&

输入邮件ID 1查看邮件内容

& 1

Message  1:

From [email protected]  Fri Apr 27 22:18:17 2018

Return-Path: <[email protected]>

X-Original-To: root

Delivered-To: [email protected]

Date: Fri, 27 Apr 2018 22:18:17 +0800

To: [email protected]

Subject: test subject

User-Agent: Heirloom mailx 12.5 7/5/10

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

hi.

&

输入h回到邮件列表

邮件看过后,N就没有显示了

& h

>   1 root                  Fri Apr 27 22:18  18/610   "test subject"

&

输入d 邮件ID,删除邮件

& d 1

& h

No applicable messages

&

输入q退出

& q

[root@localhost cn]#

1.12.6.last

列出目前与过去所有登入系统的用户信息,还包括系统重启

[root@localhost cn]# last

用户  登录终端    登录的IP登录时间是否还在线/退出时间、持续时间

test     pts/1        192.168.1.101    Fri Apr 27 21:57   still logged in

root     pts/0        192.168.1.101    Fri Apr 27 21:20   still logged in

reboot   system boot  3.10.0-693.el7.x Fri Apr 27 19:50 - 22:28  (02:37)

test     pts/1        192.168.1.101    Thu Apr 26 23:22 - 23:22  (00:00)

test     pts/1        192.168.1.101    Thu Apr 26 23:07 - 23:22  (00:15)

reboot   system boot  3.10.0-693.el7.x Wed Apr 25 21:43 - 22:28 (2+00:45)

root     pts/0        192.168.1.101    Tue Apr 24 20:19 - crash (1+01:23)

wtmp begins Sun Apr 22 16:10:16 2018

用户列为reboot时,表示系统重启了。

1.12.7.lastlog

最后一次登录信息

列出所有的用户信息,列出最后一次登录信息

[root@localhost cn]# lastlog

Username         Port     From             Latest

root             pts/0    192.168.1.101    Fri Apr 27 21:20:22 +0800 2018

bin                                        **Never logged in**

daemon                                     **Never logged in**

adm                                        **Never logged in**

lp                                         **Never logged in**

sync                                       **Never logged in**

shutdown                                   **Never logged in**

halt                                       **Never logged in**

mail                                       **Never logged in**

operator                                   **Never logged in**

games                                      **Never logged in**

ftp                                        **Never logged in**

nobody                                     **Never logged in**

systemd-network                            **Never logged in**

dbus                                       **Never logged in**

polkitd                                    **Never logged in**

postfix                                    **Never logged in**

sshd                                       **Never logged in**

test             pts/1    192.168.1.101    Fri Apr 27 21:57:39 +0800 2018

-u 用户ID 查看某个用户的登录信息

[root@localhost cn]# lastlog -u 0

Username         Port     From             Latest

root             pts/0    192.168.1.101    Fri Apr 27 21:20:22 +0800 2018

[root@localhost cn]# lastlog -u 1000

Username         Port     From             Latest

test             pts/1    192.168.1.101    Fri Apr 27 21:57:39 +0800 2018

如何查看用户ID?

[root@localhost cn]# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash

1.12.8.traceroute

显示数据包到主机间的路径

该命令需要安装包traceroute

[root@localhost cn]# yum install traceroute

[root@localhost cn]# traceroute www.baidu.com

traceroute to www.baidu.com (119.75.213.61), 30 hops max, 60 byte packets

 1  gateway (192.168.1.1)  0.525 ms  0.503 ms  1.113 ms

 2  * * *

 3  * * *

 4  10.144.11.202 (10.144.11.202)  1.729 ms  1.720 ms  1.709 ms

 5  14.197.242.145 (14.197.242.145)  4.232 ms *  4.900 ms

 6  14.197.248.253 (14.197.248.253)  7.647 ms 14.197.248.241 (14.197.248.241)  5.957 ms 14.197.248.253 (14.197.248.253)  6.582 ms

 7  14.197.252.197 (14.197.252.197)  45.213 ms 14.197.252.189 (14.197.252.189)  44.424 ms 14.197.252.37 (14.197.252.37)  45.195 ms

 8  14.197.252.54 (14.197.252.54)  45.174 ms  45.180 ms 14.197.252.50 (14.197.252.50)  45.170 ms

 9  14.197.149.178 (14.197.149.178)  45.162 ms 14.197.149.182 (14.197.149.182)  41.744 ms *

10  * * *

11  * 182.61.253.126 (182.61.253.126)  53.336 ms  42.238 ms

12  * * *

13  * * *

14  * * *

15  * * *

16  * * *

17  * * *

18  * * *

19  * * *

20  * * *

21  * * *

22  * * *

23  * * *

24  * * *

25  * * *

26  * * *

27  * * *

28  * * *

29  * * *

30  * * *

1.12.9.netstat

net status网络状态

显示网络相关的信息

-t TCP协议,有握手,确定接收到。

-u UDP协议,不握手,直接丢个信息过去,不管你接到不接到

-l 监听

-r 路由

-n 显示IP地址和端号

范例:

netstat –tlun 查看本机监听的端口

[root@localhost cn]# netstat -tlun

Active Internet connections (only servers)

协议 接收传输距离,0表示正常

Proto Recv-Q Send-Q Local Address           Foreign Address         State

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN

tcp6       0      0 :::22                   :::*                    LISTEN

tcp6       0      0 ::1:25                  :::*                    LISTEN

[root@localhost cn]#

TCP才有监听LISTEN,UDP不会有

netstat –an 查看本机所有的网络连接

-a all的意思

[root@localhost cn]# netstat -an

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address           Foreign Address         State

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN

tcp        0      0 192.168.1.200:22        192.168.1.101:52591     ESTABLISHED

tcp        0     36 192.168.1.200:22        192.168.1.101:51745     ESTABLISHED

tcp        0      0 192.168.1.200:22        192.168.1.101:52592     ESTABLISHED

tcp        0      0 192.168.1.200:22        192.168.1.101:51748     ESTABLISHED

tcp6       0      0 :::22                   :::*                    LISTEN

tcp6       0      0 ::1:25                  :::*                    LISTEN

raw6       0      0 :::58                   :::*                    7

Active UNIX domain sockets (servers and established)

Proto RefCnt Flags       Type       State         I-Node   Path

unix  2      [ ACC ]     STREAM     LISTENING     19078    public/flush

unix  2      [ ACC ]     STREAM     LISTENING     19093    public/showq

ESTABLISHED 已经连接状态

netstat –rn 查看本机路由表

可以查看到网关地址

[root@localhost cn]# netstat -rn

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0

192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

[root@localhost cn]#

1.12.10.setup

配置网络

red hat 自带的管理工具集合,在其他版本Linux中不存在。

可以支持:

防火墙配置

网络配置

系统服务

验证配置

安装setuptool

[root@localhost cn]# setup

-bash: setup: command not found

 [root@localhost cn]# yum install setuptool

Loaded plugins: fastestmirror

直接输入setup命令,即可进入配置工具

[root@localhost cn]# setup

1.12.11.mount

挂载

mount [-t 文件系统] 设备文件名  挂载点

挂载光盘

mount –t iso9660 /dev//sr0 /mnt/cdrom

iso9660

表示的光盘文件系统,这个固定的。系统就知道,实事上可以不写。

设备文件名,固定的,光盘就是/dev/sr0,也可以指定为软链接/dev/cdrom

[root@localhost dev]# ll /dev/cdrom

lrwxrwxrwx. 1 root root 3 Apr 27 23:11 /dev/cdrom -> sr0

1、安装光盘

2、创建挂载点

[root@localhost /]# mkdir /mnt/cdrom

3、挂载

[root@localhost dev]# mount /dev/cdrom /mnt/cdrom

mount: /dev/sr0 is write-protected, mounting read-only

[root@localhost dev]#

提示信息表示光盘是只读,被近挂载为只读文件

进入挂载点就可以访问了,/dev/cdrom是设备,是进不去的,不能访问

[root@localhost dev]# cd /mnt/cdrom

[root@localhost cdrom]# ls

autorun.inf  boot  bootmgr  bootmgr.efi  efi  setup.exe  sources  support  upgrade

[root@localhost cdrom]#

4、卸载

Linux中你挂载使用中,必须自己手动卸载

在挂载点目录时,卸载不了,如下提示:需要退出挂载点目录

[root@localhost cdrom]# umount /mnt/cdrom

umount: /mnt/cdrom: target is busy.

        (In some cases useful info about processes that use

         the device is found by lsof(8) or fuser(1))

[root@localhost cdrom]#

卸载以后,目录/mnt/cdrom就什么内容没有了。

卸载时,可以指定挂载点,还可以指定设备:umount /dev/sr0

[root@localhost /]# umount /mnt/cdrom

[root@localhost /]# ls /mnt/cdrom

[root@localhost /]#

猜你喜欢

转载自wlcacc.iteye.com/blog/2421881