Nginx 因 防火牆,iptables,ftp/sftp,Selinux 服務導致無法遠程訪問及傳輸修改文件的問題

chkconfig iptables off 永久關閉防火牆

service iptables stop  臨時關閉 service iptables  start 臨時開啓

 

在虛擬機的Linux上安裝好Nginx後,啓動Nginx服務,在Linux環境下,輸入ip直接可以訪問到Nginx的歡迎界面,而在電腦本地訪問不到界面,出現錯誤。

這種情況下,一般是Linux系統的防火牆需要配置,把所需要訪問的網絡端口開放出去。 
查看如下:cd  / &&  cat etc/sysconfig/iptables

添加Nginx的默認端口80:   -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 

[root@pyy-docker /]# cd  / &&  cat etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

 

配置完成後,重啓防火牆設置 service iptables restart (關鍵步驟必須重啓)

可以訪問 Linux部署服務器地址測試了

 

SElinux(secure enhanced linux )安全增強的Linux

selinux  linux中的安全訪問策略

運行模式分爲三種 enforcing (強制模式)、permissive(寬容模式)、disabled(關閉)

查看狀態\

$ sestatus
SELinux status:                 enabled  #開機啓動
SELinuxfs mount:                /selinux
Current mode:                   enforcing  #強制模式
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted

臨時

# setenforce 0 #關閉 Selinux
# setenforce 1 #開啓 Selinux

永久方式,確實可用,需要重啓服務器!


參考 :https://blog.csdn.net/yonggeit/article/details/72388965 
 

1、修改 /etc/selinux/config

# vim /etc/selinux/config
2、修改 SELINUX=disabled ,修改後內容
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted
3、重啓電腦查看 Selinux 狀態,應該爲關閉狀態
$ getenforce 
Disabled

外網是可以訪問上去了,可是發現沒法返回目錄(使用ftp的主動模式,被動模式還是無法訪問),也上傳不了,因爲selinux作怪了。

修改selinux:

執行以下命令查看狀態:

[root@hyy ~]# getsebool -a | grep ftp  

allow_ftpd_anon_write --> off    # 允許匿名修改

ftpd_full_access --> off        # 允許數據訪問

allow_ftpd_use_cifs --> off

allow_ftpd_use_nfs --> off

tftp_home_dir --> off          # 允許home目錄

ftpd_connect_db --> off      # 允許連接數據庫

ftpd_use_passive_mode --> off   #  警告

httpd_enable_ftp_server --> off   # 允許 開啓ftp服務

tftp_anon_write --> off    # 允許匿名寫

[root@hyy ~]#

執行上面命令,再返回的結果看到兩行都是off,代表,沒有開啓外網的訪問

[root@hyy ~]# setsebool -P allow_ftpd_full_access on

                              設置   -P永久  全訪問  on 激活

[root@hyy ~]# setsebool -P ftp_home_dir on   目錄訪問

2 ssh只能遠程登陸到普通用戶,不能以root遠程登陸

默認是不允許root用戶登錄SSH的,如果登陸結果就會如下:

用PiY登陸時,總提示: 
login as: root 
Using keyboard-interactive authentication. 
Password:

Access denied 訪問超時

這個時候需要編輯其配置文件,輸入以下命令進行修改:

vim  /etc/ssh/sshd_config   文件  /etc/ssh/sshd_config,不是 /etc/ssh/ssh_config

然後進行修改.  找到

#PasswordAuthentication no 修改爲: PasswordAuthentication yes

這段的意思是,讓SSH使用密碼驗證登陸.  找到

#PermitRootLogin no 修改爲 PermitRootLogin yes

這段的意思是,讓SSH准許root用戶登錄.一般來說,爲了安全起見,這一段

還是不要允許的好.不過安全要求不高,可以這樣做.  找到

#PermitEmptyPasswords no 修改爲 PermitEmptyPasswords no

這段的意思是,允許空密碼登錄改爲不

保存、重啓

3  SSH能登錄,ftp不能遠程獲取目錄列表及修改文件

修改遠程訪問客戶端  如editplus/xshell/filwzilla的FTP/SFTP選擇sftp和活動狀態


                                       一般不用操作
FTP
服務

1.  開啓FTP服務步驟:
2   安裝FTP服務包:#yum -y install vsftpd
3.  設置開機啓動vsftpd ftp服務:# chkconfig vsftpd on
4.  開啓FTP服務:# service vsftpd restartPS:需關閉selinux (setenforce 0)
                SFTP服務
  1. #ssh –V  來查看openssh的版本,如果低於4.8p1,需要自行升級安裝,
  2. 創建sftp組:#groupadd sftp  
  3. 創建測試賬戶:#useradd -g sftp -s /bin/false testuser   修改密碼:# passwd testuser
  4. 創建目錄:#mkdir /opt/sftp   #cd /opt/sftp;      #mkdir testuser
  5. 修改測試賬戶HOME路徑:# usermod -d /opt/sftp/testuser testuser
  6. 配置sshd_config :# vim /etc/ssh/sshd_config
  7. 註釋掉:Subsystem   sftp    /usr/libexec/openssh/sftp-server
    1. 添加如下幾行
    2. Subsystem       sftp    internal-sftp  
    3. Match Group sftp  
    4. ChrootDirectory /opt/sftp/%u  
    5. ForceCommand    internal-sftp  
    6. AllowTcpForwarding no  
    7. X11Forwarding no  
  8. 設定Chroot目錄權限:chown root:sftp /opt/sftp/testuser #chmod 755 /opt/sftp/testuser  
    1. # mkdir /opt/sftp/testuser/upload  
    2. # chown uplus:sftp /opt/sftp/testuser/upload  
    3. # chmod 755 /opt/sftp/testuser/upload  

 

  1. 重啓sshd服務:# service sshd restart  

httpd常規使用參考

上述只是簡單的概念,便於理解,下面以httpd爲例,做實際操作說明。

    1)httpd啓動時需要使用端口22 . 80、81、443等,需要開通權限

# 查看當前httpd的端口權限

[root@test /]# semanage port -l|grep http

http_cache_port_t              tcp      3128, 8080, 8118, 11211, 10001-10010

http_cache_port_t              udp      3130, 11211

http_port_t                    tcp     22 , 80, 443, 488, 8008, 8009, 8443

# 通常默認情況下22 ,80、443端口已經打開,現在需要添加對81端口的支持,使用以下命令

[root@test /]# semanage port -a -t http_port_t -p tcp 81  #與上邊防火牆端口設置相同開放81端口

# 刪除該端口

[root@test /]# semanage port -d -t http_port_t -p tcp 81

    2)由於開啓ssl需要使用到服務器證書,證書文件存放在特定的目錄下(例如:/myweb/ssl),由於目錄權限問題導致啓動失敗。

錯誤提示:SSLCertificateFile: file '/myweb/ssl/server.crt' does not exist or is empty

# 修改目錄權限,使httpd進程(域)有權限訪問證書相關的配置文件(注意:由於目錄存在多層,需要對每層目錄的權限進行設置,-R表示目錄及其子目錄、文件)

# 默認情況下httpd的安裝路徑/etc/httpd/的權限就是httpd_config_t

chcon -R -t httpd_config_t /myweb/ssl/

chcon -t httpd_config_t /myweb/

    3)網站訪問目錄權限設置(httpd進程要求其訪問的文件權限爲httpd_sys_content_t)

# httpd默認安裝的DocumentRoot指向的目錄是/var/www/html,其權限默認已經設置爲httpd_sys_content_t

# 如果修改DocumentRoot或設置了特定的Directory,則需要修改對應目錄權限

# 例如:

chcon -t httpd_sys_content_t /myweb/www/

    4)網站使用到第三方模塊

chcon -t httpd_modules_t /your/module/xxx.so

    5)如果需要啓動第三方程序,則需要修改程序權限爲httpd_exec_t

# 例如,在httpd中加入了modsecurity,安裝路徑在/etc/httpd/mlogc/

# 修改可執行程序/etc/httpd/mlogc/mlogc權限

chcon -t httpd_exec_t /etc/httpd/mlogc/mlogc
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章