根據端口號查找對應的服務名
1)grep -w 端口號 /etc/services
2)grep "\b端口號\b" /etc/services
以2049端口為例,執行grep -w 2049 /etc/services或grep "\b2049\b" /etc/services 即可查找到其對應的服務名為nfs
grep -w 2049 /etc/services
nfs 2049/tcp nfsd shilp # Network File System
nfs 2049/udp nfsd shilp # Network File System
nfs 2049/sctp nfsd shilp # Network File System
# grep "\b2049\b" /etc/services
nfs 2049/tcp nfsd shilp # Network File System
nfs 2049/udp nfsd shilp # Network File System
nfs 2049/sctp nfsd shilp # Network File System
其中-w表示只顯示全字符合的列,即精確匹配;"\b"表示匹配一個字邊界,即字與空格間的位置。例如,“er\b”匹配“never”中的“er”,但不匹配“verb”中的“er”(若匹配“verb”中的“er”可使用"\B"進行非字邊界匹配)。
根據服務名查找對應的端口號
1)grep -w 服務名 /etc/services
2)grep "\b服務名\b" /etc/services
以nfs服務為例,執行grep -w nfs /etc/services 或grep "\bnfs\b"/etc/services即可查找到其對應的端口號為2049
# grep -w nfs /etc/services
nfs 2049/tcp nfsd shilp # Network File System
nfs 2049/udp nfsd shilp # Network File System
nfs 2049/sctp nfsd shilp # Network File System
# grep "\bnfs\b" /etc/services
nfs 2049/tcp nfsd shilp # Network File System
nfs 2049/udp nfsd shilp # Network File System
nfs 2049/sctp nfsd shilp # Network File System
其中-w表示只顯示全字符合的列,即精確匹配;"\b"表示匹配一個字邊界,即字與空格間的位置。\bnfs\b即為精確匹配nfs。