UNIX网络编程卷1:套接字联网API-第2章:端口号和查看linux常用服务对应端口号

1.理解端口号的概念

  • 最直接的理解:在任意的某个时刻,我们可能正在同时运行多个服务进程。而这多个服务进程在传输层(以tcp/ip协议族为例)都是使用tcp/udp/sctp。所以,我们就需要以某种机制,来区分这些同时使用相同协议的进程,作为编程人员,我们第一反应就是给这些进程+协议再绑定上一个唯一的标识号,称这个标识号为端口号。
  • tcp/udp/sctp都是用16位整数来表示端口号
  • 当服务进程使用不同传输层协议时,可以使用相同的端口号
  • 以编程的角度来讲,当我们编写客户端网络程序时,想要和某个服务器通信,首先需要指出服务器(ip+端口号 ip表示特定主机,端口号可以知道具体与主机上的哪个服务通信)
  • 客户端网络程序还需给自己一个临时端口号(上述说的端口号是服务器的),这些端口号通常由传输层协议自动赋予)
  • 端口号种类:
    • 众所周知的端口:如其名,由IANA规定的分配给常用服务的端口号(一般端口号分配给tcp和udp的同一给定服务)
    • 已登记的端口:由IANA登记的端口,各大商家可以申请哟~
    • 动态的或私用的窗口:我们平时自己写服务时常用的端口
  • 有一点要注意,unix系统有保留端口的概念,指的是小于1024的,众所周知的端口都是这种类型。这些端口都只能赋予给特权用户进程的套接字。分配使用这些端口号的服务器必须以超级用户特权启动。(看似很小很简单的小玩意儿也可能会让你一整天调试的不要不要的~~)

2.套接字对的相关概念

  • 套接字对唯一标识一个网络上的每个tcp连接
  • 一个tcp连接的套接字对是一个定义该连接的两个端点的四元组:本地ip,本地tcp端口号,外地ip,外地tcp端口号
  • 有一点要注意,连接的一端并非只能提供一个ip地址和端口号,可能是多宿的
  • 标识每个端点的两个值通常称为一个套接字

3.如何查看常用服务及其端口号

譬如:

我们先安装一个ssh(Secure Shell)服务((我的ubuntu14.04 默认并没有安装)

sudo apt-get update

sudo apt-get install openssh-server (安装ssh服务端)

sudo apt-get install openssh-client (安装客户端,我就在自己的ubunu机器上调试,所以顺便再装一个客户端)

查看服务是否已经开启(netstat调试可参照我的另一篇博文:使用netstat调试应用

结果如下:

sunxiaowu@sunxiaowu:~$ sudo netstat -anp |grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1016/sshd
tcp6 0 0 :::22 :::* LISTEN 1016/sshd
unix 2 [ ACC ] STREAM LISTENING 16589 1789/gnome-keyring- /run/user/1000/keyring-MxgGfh/ssh

稍微解释一下,此处tcp6是什么意思?意思是tcp既可走网络层ipv4协议,又可走ipv6,ssh服务进程pid1016,端口号22,正处于监听状态(依次调用了socket,bind,listen,正阻塞在accept),sshd中d表示守护(daemon)进程,以后详解。

我们使用ssh客户端命令行登录

ssh ×××.*******.******.*******(目的主机ip地址)

输入yes

输入密码

登录成功

再次查看服务

如图所示,在我们用客户端ssh登录服务器后,显示情况如上。1016进程依旧监听(tcp和tcp6),新建了一个5697进程正处于ESTABLISHED状态,因为我是本机测试,故5689进程表示客户端进程,正处于和服务进程5697的连接状态。服务进程5697是哪里来的? 以后会在网络编程中详解

我们要学会查看unix文件来学习,有时候与其查看大量其他资料,不如直接看unix自带的一些文件说明

查看服务的端口分配

vim /etc/services

# Network services,Internet style
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence,officially ports have two entries
# even if the protocol doesn't support UDP operations.
#
# Updated from http://www.iana.org/assignments/port-numbers and other
# sources like http://www.freebsd.org/cgi/cvsweb.cgi/src/etc/services .
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.

tcpmux 1/tcp # TCP port service multiplexer
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
discard 9/udp sink null
systat 11/tcp users
daytime 13/tcp
daytime 13/udp
netstat 15/tcp
qotd 17/tcp quote
msp 18/tcp # message send protocol
。。。。。。。。。。

ipp 631/udp
#
# UNIX specific services
#
exec 512/tcp
biff 512/udp comsat
login 513/tcp
who 513/udp whod
shell 514/tcp cmd # no passwords used
syslog 514/udp
printer 515/tcp spooler # line printer spooler
talk 517/udp
ntalk 518/udp
route 520/udp router routed # RIP
timed 525/udp timeserver
tempo 526/tcp newdate
courier 530/tcp rpc
conference 531/tcp chat
netnews 532/tcp readnews
netwall 533/udp # for emergency broadcasts
。。。。。。。。。

# From ``Assigned Numbers'':
#
#> The Registered Ports are not controlled by the IANA and on most systems
#> can be used by ordinary user processes or programs executed by ordinary
#> users.
#
#> Ports are used in the TCP [45,106] to name the ends of logical
#> connections which carry long term conversations. For the purpose of
#> providing services to unknown callers,a service contact port is
#> defined. This list specifies the port used by the server process as its
#> contact port. While the IANA can not control uses of these ports it
#> does register or list uses of these ports as a convienence to the
#> community.
#
socks 1080/tcp # socks proxy server
socks 1080/udp
proofd 1093/tcp
proofd 1093/udp
rootd 1094/tcp
rootd 1094/udp
238,1 40%
。。。。。。。。。。

#=========================================================================
# The remaining port numbers are not as allocated by IANA.
#=========================================================================

# Kerberos (Project Athena/MIT) services
# Note that these are for Kerberos v4,and are unofficial. Sites running
# v4 should uncomment these and comment out the v5 entries above.
#
kerberos4 750/udp kerberos-iv kdc # Kerberos (server)
kerberos4 750/tcp kerberos-iv kdc
kerberos-master 751/udp kerberos_master # Kerberos authentication
kerberos-master 751/tcp
passwd-server 752/udp passwd_server # Kerberos passwd server
krb-prop 754/tcp krb_prop krb5_prop hprop # Kerberos slave propagation
krbupdate 760/tcp kreg # Kerberos registration
swat 901/tcp # swat
kpop 1109/tcp # Pop with Kerberos
knetd 2053/tcp # Kerberos de-multiplexor

。。。。。。。。。。

从这个配置文件可以查看服务对应的端口号

例如我要查看ssh服务对应信息

vim /etc/services

在命令行模式下,:/ssh

可以看到服务名ssh,被分配的端口号是23

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...