1. 程式人生 > >Centos7 防火牆和Selinux 一些簡單操作命令

Centos7 防火牆和Selinux 一些簡單操作命令

今年企業對Java開發的市場需求,你看懂了嗎? >>>   

CentOS 7 預設採用了 firewalld ,儘管還是可以安裝並支援 iptables,但是還是跟上新東西為好!其實iptables 我也沒有用過,哈哈哈!

防火牆功能很強大,但是,日常要做的最多的還是開放埠,記錄一下,以便隨時 copy

1、 開放 8080 埠

1. 檢視防火牆狀態
	sudo systemctl status firewalld
	
	sudo firewall-cmd --state
		running
2. 防火牆基本命令
	# 開啟
	service firewalld start
	# 重啟
	service firewalld restart
	# 關閉
	sudo service firewalld stop
	
	# 檢視防火牆規則
	sudo firewall-cmd --list-all 
	sudo firewall-cmd --state
3. 開啟 8080 埠
	sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
	
	重啟防火牆
	sudo systemctl restart firewalld.service
	
	sudo firewall-cmd --reload

4. 不知道為什麼,就把自己關在外面了 ssh 連線不上去了!
	sudo firewall-cmd --list-all 
		public (active)
		  target: default
		  icmp-block-inversion: no
		  interfaces: enp0s3
		  sources: 
		  services: ssh dhcpv6-client
		  ports: 
		  protocols: 
		  masquerade: no
		  forward-ports: 
		  source-ports: 
		  icmp-blocks: 
		  rich rules: 
	** 可以看到,ports 一個都沒有!	  
	** 乾脆重新增加埠的時候,先加上 20,22 埠
	sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
	sudo firewall-cmd --zone=public --add-port=22/tcp --permanent
	sudo firewall-cmd --zone=public --add-port=21/tcp --permanent
	sudo firewall-cmd --zone=public --add-port=20/tcp --permanent
	sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
	sudo firewall-cmd --zone=public --add-port=4433/tcp --permanent

重啟防火牆
	sudo systemctl restart firewalld.service
重新檢視防火牆規則
	sudo firewall-cmd --list-all 
		public (active)
		  target: default
		  icmp-block-inversion: no
		  interfaces: enp0s3
		  sources: 
		  services: ssh dhcpv6-client
		  ports: 80/tcp 22/tcp 21/tcp 20/tcp 8080/tcp 4433/tcp
		  protocols: 
		  masquerade: no
		  forward-ports: 
		  source-ports: 
		  icmp-blocks: 
		  rich rules: 
** 現在可以安全的退出、重新 ssh 了!

2、再增加一個其他埠 (例如:4433)發生錯誤

	1. 測試:將以上 chuangke.conf 的埠改成 4433後
		sudo systemctl restart nginx
		返回錯誤!
			Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

2. 測試 nginx 配置是 OK!
	sudo nginx -t
		nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
		nginx: configuration file /etc/nginx/nginx.conf test is successful
	
3. 檢視錯誤資訊
		sudo systemctl status nginx.service
			......
		3月 29 15:07:26 centos7-71 nginx[18289]: nginx: [emerg] bind() to 0.0.0.0:4433 failed (13: Permission denied)
			......
			3月 29 15:07:26 centos7-71 systemd[1]: Unit nginx.service entered failed state.
			3月 29 15:07:26 centos7-71 systemd[1]: nginx.service failed.

** 4433 埠不允許!

3、semanage 解決 http 埠訪問配置(Selinux)

參考 https://blog.csdn.net/runsnail2018/article/details/81185138 https://zhb1208.iteye.com/blog/1432957

1. 直接安裝 semanage 會提示:沒有 semanage
	sudo yum update
	sudo yum install semanage 
		已載入外掛:fastestmirror
		Loading mirror speeds from cached hostfile
		 * base: mirrors.aliyun.com
		 * extras: mirrors.aliyun.com
		 * updates: mirrors.aliyun.com
		沒有可用軟體包 semanage。
		錯誤:無須任何處理

2. 按照參考文件,執行一下設定和安裝命令
	1). yum provides /usr/sbin/semanage
	2). yum -y install policycoreutils-python
	3). 現在就可以執行 semanage 命令了
3. 檢視 http 可以訪問的埠
	sudo semanage port -l | grep http_port_t
			http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
		pegasus_http_port_t            tcp      5988
	** 果然沒有發現 4433 埠!	
4. 增加 4433 到 http 訪問埠
	sudo semanage port -a -t http_port_t  -p tcp 4433
5. 再來看看 http 埠 
sudo semanage port -l | grep http_port_t
		http_port_t                    tcp      4433, 80, 81, 443, 488, 8008, 8009, 8443, 9000
		pegasus_http_port_t            tcp      5988
** 現在可以訪問 4433 了
	sudo systemctl restart nginx
	sudo systemctl status nginx.service
	OK!

一直沒有搞明白 SELinux 幹麼用的!現在有點兒明白了!

不是簡單從防火牆開放一個埠就可以了 還要配置 特定服務(這次是 HTTP)可以使用哪些埠

4、切換root目錄一直 403 錯誤

參考 https://blog.csdn.net/a690392431/article/details/85914076

** 這個部落格應該是正確的!但是,我照著操作了,還是不行!

實在沒有辦法了!
只好先關閉 SELinux !以後再學習!

sudo vim /etc/selinux/config
		# by wzh 20190329 disable SELINUX
		SELINUX=disabled
		# SELINUX=enforcing

重啟才能生效!

檢視 SELinux
sestatus
	SELinux status: