[TOC]
----
# 網絡安全與主機基本防護
## 定期更新系統軟件
### 軟件安裝更新`rpm`
- `rpm`——已安裝的軟件
~~~
[root@tatahy-lamp ~]# rpm -qa | grep -i tcpdump
tcpdump-4.9.2-4.el7_7.1.x86_64
~~~
### 系統更新`yum`
~~~
[root@tatahy-lamp ~]# yum -y update
~~~
- `yum`——未安裝的軟件
~~~
[root@tatahy-lamp ~]# yum search all tcpdump
Loaded plugins: langpacks
=========================================================== Matched: tcpdump ============================================================
tcpdump.x86_64 : A network traffic monitoring tool
batctl.x86_64 : B.A.T.M.A.N. advanced control and management tool
dnstop.x86_64 : Displays information about DNS traffic on your network
libpcap.i686 : A system-independent interface for user-level packet capture
libpcap.x86_64 : A system-independent interface for user-level packet capture
libpcap-devel.i686 : Libraries and header files for the libpcap library
libpcap-devel.x86_64 : Libraries and header files for the libpcap library
ngrep.x86_64 : Network layer grep tool
ssldump.x86_64 : An SSLv3/TLS network protocol analyzer
tcpflow.x86_64 : Network traffic recorder
tcpreplay.x86_64 : Replay captured network traffic
tcpxtract.x86_64 : Tool for extracting files from network traffic
~~~
## 限制連接端口
### 端口查看`netstat` `nmap`
## 限制網絡服務的權限(啟動SELinux)
# 服務器內部的資源管理與防火墻規劃
## 服務的管理
### 查詢系統服務`chkconfig` `systemctl list-unit-files`
* "System V"工具—— `chkconfig`
~~~
[root@tatahy-lamp init.d]# chkconfig --list
Note: This output shows SysV services only and does not include native systemd services.
SysV configuration data might be overridden by native systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use 'systemctl list-dependencies [target]'.
aegis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
~~~
* "systemd"工具——`systemctl list-unit-files`
~~~
[root@tatahy-lamp init.d]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path enabled
cups.path disabled
systemd-ask-password-console.path static
...
systemd-readahead-done.timer indirect
systemd-tmpfiles-clean.timer static
unbound-anchor.timer disabled
367 unit files listed.
~~~
>[info] `enabled`是開機啟動,`disabled`是開機不啟動
- 設置開機自啟項
~~~
[root@tatahy-lamp init.d]# systemctl enable redis
~~~
`Systemctl`是一個“systemd”工具,主要負責控制“systemd”系統和服務管理器。它整合了`chkconfig` 與` service`功能于一體。
“systemd”是一個系統管理守護進程、工具和庫的集合,用于取代“System V”初始進程。“systemd”的功能是用于集中管理和配置類UNIX系統。
在Linux生態系統中,“systemd”被部署到了大多數的標準Linux發行版中,只有為數不多的幾個發行版尚未部署。“systemd”通常是所有其它守護進程的父進程,但并非總是如此。
從CentOS7.x開始,CentOS開始使用“systemd”服務來代替“daemon”,原來管理系統啟動和管理系統服務的相關命令全部由`systemctl`命令來代替。
### 查詢當前系統打開文件`lsof`

~~~
lsof -i:8080:查看8080端口占用
lsof abc.txt:顯示開啟文件abc.txt的進程
lsof -c abc:顯示abc進程現在打開的文件
lsof -c -p 1234:列出進程號為1234的進程所打開的文件
lsof -g gid:顯示歸屬gid的進程情況
lsof +d /usr/local/:顯示目錄下被進程開啟的文件
lsof +D /usr/local/:同上,但是會搜索目錄下的目錄,時間較長
lsof -d 4:顯示使用fd為4的進程
lsof -i -U:顯示所有打開的端口和UNIX domain文件
~~~
### 已啟動的網絡監聽服務`netstat`
~~~
[root@tatahy-lamp emqx]# netstat -tulpn
Active Internet connections (only servers)
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 510/rpcbind
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 827/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 826/sshd
tcp6 0 0 :::3306 :::* LISTEN 1053/mysqld
tcp6 0 0 :::111 :::* LISTEN 510/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 510/rpcbind
udp 0 0 172.18.174.15:123 0.0.0.0:* 522/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 522/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 522/ntpd
udp 0 0 0.0.0.0:673 0.0.0.0:* 510/rpcbind
udp6 0 0 :::111 :::* 510/rpcbind
udp6 0 0 :::123 :::* 522/ntpd
udp6 0 0 :::673 :::* 510/rpcbind
~~~
* 與路由(route)有關的參數:
-r:列出路由表,功能與`route`命令相同
-n:不使用主機名與服務名稱,使用IP與port number,與`route -n`相同
* 與網絡接口有關的參數:
-a:列出所有的連接狀態,包括`tcp/udp/unix/socket`等
-t : 指明顯示TCP端口
-u : 指明顯示UDP端口
-l : 僅顯示監聽套接字(所謂套接字就是使應用程序能夠讀寫與收發通訊協議(protocol)與資料的程序)
-p : 顯示進程標識符和程序名稱,每一個套接字/端口都屬于一個程序。
-c:設置幾秒后自動更新一次。例如:`-c 5`為每5秒更新一次網絡狀態
>[warning] 在‘program name’項下的是實際程序的執行文件,可能與`/etc/init.d/`里的服務腳本文件名不同,
>[info] CentOS7.0, `/etc/init.d/README`
> You are looking for the traditional init scripts in /etc/rc.d/init.d, and they are gone?
> Here's an explanation on what's going on:
> You are running a systemd-based OS where traditional init scripts have been replaced by native systemd services files. Service files provide very similar functionality to init scripts. To make use of service files simply invoke "systemctl", which will output a list of all currently running services (and other units). Use `systemctl list-unit-files` to get a listing of all known unit files, including stopped, disabled and masked ones. Use `systemctl start foobar.service` and `systemctl stop foobar.service` to start or stop a service, respectively. For further details, please refer to `systemctl`.
### 設置任務計劃`crontab`
設置任務計劃文件,讓服務器定時自動執行任務。
## 服務器的軟件設置
服務器里安裝的軟件要知道
### 1. 軟件的安裝與查詢`rpm` `yum`
- `rpm`——已安裝的軟件
~~~
[root@tatahy-lamp ~]# rpm -qa | grep -i tcpdump
tcpdump-4.9.2-4.el7_7.1.x86_64
~~~
- `yum`——未安裝的軟件
~~~
[root@tatahy-lamp ~]# yum search all tcpdump
Loaded plugins: langpacks
=========================================================== Matched: tcpdump ============================================================
tcpdump.x86_64 : A network traffic monitoring tool
batctl.x86_64 : B.A.T.M.A.N. advanced control and management tool
dnstop.x86_64 : Displays information about DNS traffic on your network
libpcap.i686 : A system-independent interface for user-level packet capture
libpcap.x86_64 : A system-independent interface for user-level packet capture
libpcap-devel.i686 : Libraries and header files for the libpcap library
libpcap-devel.x86_64 : Libraries and header files for the libpcap library
ngrep.x86_64 : Network layer grep tool
ssldump.x86_64 : An SSLv3/TLS network protocol analyzer
tcpflow.x86_64 : Network traffic recorder
tcpreplay.x86_64 : Replay captured network traffic
tcpxtract.x86_64 : Tool for extracting files from network traffic
~~~
### 2. 軟件在Linux中以什么樣的服務來實現?`systemctl` `iptables`
* 服務的是否為開機啟動?
* 服務啟動了幾個端口?
CentOS中,各個網絡服務與port number的默認對應關系是寫在`/etc/services`文件中。
* 服務的端口如何設置防火墻?
`iptables`——端口/防火墻設置
linux系統(centos、redhat等)的防火墻是默認只對外開放了22端口。
?linux系統的端口設置在`/etc/sysconfig/iptables`文件中配置。
以oracle的1521端口為例
1. 阻止所有服務器訪問1521端口
~~~
$ iptables -I INPUT -p tcp --dport 1521 -j DROP
~~~
2. 放行172.17.79.4
~~~
$ iptables -I INPUT -s 172.17.79.4 -p tcp --dport 1521 -j ACCEPT
~~~
3. 保存配置
~~~
$ service iptables save
~~~
4. 重啟防火墻
~~~
$ service iptables restart
~~~
后續如果想繼續添加ip,執行2、3、4步驟即可。
:-: 常見端口與網絡服務的對應表
| 端口 | 服務名稱與內容 |
| --- | --- |
| 20 | FTP-data,文件傳輸協議所使用的主動數據傳輸端口 |
| 21 | FTP,文件傳輸協議的命令端口 |
| 22 | SSH,較為安全的遠程連接服務 |
| 23 | Telnet,早期的遠程連接服務器軟件 |
| 25 | SMTP,簡單郵件傳輸協議,用在作為Mail Server的端口 |
| 53 | DNS,用在作為名稱解析的域名服務器 |
| 80 | WWW,全球信息網服務器 |
| 110 | POP3,郵件接收協議,辦公室用的收信軟件都通過這個端口 |
| 443 | HTTPS,有安全加密機制的WWW服務器 |
>[warning] Server端小于1024的端口必須由root賬號啟動。
> Client端則隨機用一個大于1024且沒有使用的端口號就可以了。
* 服務如何持續提供數據?
* 訪問服務的賬號及權限?
* 訪問服務的賬號及權限的設置?
### 3. 客戶端的連接測試
### 4. 服務如果出錯該如何查詢錯誤信息?
## 深入
### [[ 如何查看 Linux 中所有正在運行的服務]](https://blog.csdn.net/Pipcie/article/details/82586151)
### 連接到Internet的必要網絡參數
* IP
* Netmask
* Broadcast
* Gateway
* DNS
- 軟件工程
- 1. 基礎
- 計算
- 網絡
- 存儲
- 2. 開發/運維
- 微服務
- 容器化(Docker)
- 容器網絡
- 持續集成
- 持續發布
- 3. 架構
- 操作系統
- Linux服務器
- windows
- 內存
- 應用軟件
- 前端
- 后端
- 數據庫
- 協議
- 服務
- 分布式
- LNMP+Vue.js
- web網站架構技術
- 架構演化
- 架構分層
- Layer1. Frontend
- Layer2. Application
- Layer3. Service
- Layer4. Storage
- Layer5. Backend
- Layer6. Operation
- Layer7. Security
- Layer8. DataCenter
- 架構模式
- 架構要素
- 1. Performance
- 2. Availability
- 3. 可伸縮性
- 4. 可擴展性
- 5. 安全
- 6. 成本
- 4. 開發項目
- vue-php