基础知识:
UbuntuServer20.04.1LTS安装配置图解教程
准备篇:
一、配置防火墙,开启80端口、端口
1.1安装iptables防火墙
UbuntuServer默认没有开启任何防火墙的,但是默认安装了ufw防火墙,我们这里推荐使用iptables防火墙。
ufwstatus#查看系统自带的ufw防火墙状态
Status:inactive#表示关闭
ufwdisable#关闭ufw防火墙,参数enable是开启,我们这里关闭
apt-getremoveufw#卸载ufw
apt-getpurgeufw#清除ufw依赖包
whereisiptables#查看系统是否安装防火墙
apt-getinstalliptables#运行此命令安装防火墙
mkdir/etc/sysconfig#创建防火墙配置文件存放目录
touch/etc/sysconfig/iptables#创建防火墙配置文件
nano/etc/sysconfig/iptables#编辑添加防火墙规则
#sampleconfigurationforiptablesservice
#youcaneditthismanuallyorusesystem-config-firewall
#pleasedonotaskustoaddadditionalports/servicestothisdefaultconfiguration
*filter
:INPUTACCEPT[0:0]
:FORWARDACCEPT[0:0]
:OUTPUTACCEPT[0:0]
-AINPUT-mstate--stateRELATED,ESTABLISHED-jACCEPT
-AINPUT-picmp-jACCEPT
-AINPUT-ilo-jACCEPT
-AINPUT-ptcp-mstate--stateNEW-mtcp--dport22-jACCEPT
-AINPUT-ptcp-mstate--stateNEW-mtcp--dport80-jACCEPT
-AINPUT-ptcp-mstate--stateNEW-mtcp--dport-jACCEPT
-AINPUT-ptcp-mstate--stateNEW-mtcp--dport-jACCEPT
-AINPUT-jREJECT--reject-withicmp-host-prohibited
-AFORWARD-jREJECT--reject-withicmp-host-prohibited
COMMIT
ctrl+o#保存
ctrl+x#退出
/sbin/iptables-restore/etc/sysconfig/iptables#使防火墙规则生效
特别注意:
1、修改完防火墙规则文件/etc/sysconfig/iptables后,需要再次执行
/sbin/iptables-restore/etc/sysconfig/iptables命令,防火墙规则才能生效。
2、系统重启后,防火墙默认不会开机启动,需要再次执行/sbin/iptables-restore/etc/sysconfig/iptables命令,防火墙规则才能生效。
3、如果要临时关闭防火墙,需要清空/etc/sysconfig/iptables配置文件,再次执行/sbin/iptables-restore/etc/sysconfig/iptables命令。
4、如果要再次开启防火墙,需要恢复/etc/sysconfig/iptables配置文件,再次执行/sbin/iptables-restore/etc/sysconfig/iptables命令。
1.2添加防火墙管理脚本
nano/etc/init.d/iptables#编辑添加脚本
#脚本中的IPTABLES_CONFIG=/etc/sysconfig/iptables是防火墙配置规则文件的路径。
#!/bin/sh-e
###BEGININITINFO
#Provides:iptables
#Required-Start:mountvirtfsifupdownlocal_fs
#Default-Start:S
#Default-Stop:06
###ENDINITINFO
#July9,
#JamesB.Crockerubuntu
james.crocker.name#CreativeCommonsAttribution-ShareAlike3.0License(BY,SA)
#Scripttoload/unload/saveiptablesfirewallsettings.
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
IPTABLES=/sbin/iptables
IPTABLES_SAVE=/sbin/iptables-save
IPTABLES_RESTORE=/sbin/iptables-restore
IPTABLES_CONFIG=/etc/sysconfig/iptables
[-xIPTABLES]
exit0
./lib/lsb/init-functions
case"1"in
start)
log_action_begin_msg"Startingfirewall"
typeusplash_write/dev/null2/dev/nullusplash_write"TIMEOUT"
true
ifIPTABLES_RESTOREIPTABLES_CONFIG;then
log_action_end_msg?
else
log_action_end_msg?
fi
typeusplash_write/dev/null2/dev/nullusplash_write"TIMEOUT15"
true
;;
stop)
log_action_begin_msg"Savingcurrentfirewallconfiguration"
ifIPTABLES_SAVEIPTABLES_CONFIG;then
log_action_end_msg?
else
log_action_end_msg?
fi
log_action_begin_msg"FlushingALLfirewallrulesfromchains!"
ifIPTABLES-F;then
log_action_end_msg?
else
log_action_end_msg?
fi
log_action_begin_msg"DeletingALLfirewallchains[Warning:ACCEPTINGALLPORTSERVICES!]"
ifIPTABLES-X;then
IPTABLES-PINPUTACCEPT
IPTABLES-PFORWARDACCEPT
IPTABLES-POUTPUTACCEPT
log_action_end_msg?
else
log_action_end_msg?
fi
;;
save)
log_action_begin_msg"Savingcurrentfirewallconfiguration"
ifIPTABLES_SAVEIPTABLES_CONFIG;then
log_action_end_msg?
else
log_action_end_msg?
fi
;;
force-reload
restart)
log_action_begin_msg"Reloadingfirewallconfiguration[Warning:POTENTIALNETWORKINSECURITYDURINGRELOAD]"
IPTABLES-F
IPTABLES-X
ifIPTABLES_RESTOREIPTABLES_CONFIG;then
log_action_end_msg?
else
log_action_end_msg?
fi
;;
*)
echo"Usage:/etc/init.d/iptables{start
stop
save
restart
force-reload}"
exit1
;;
esac
exit0
ctrl+o#保存
ctrl+x#退出
chmod+x/etc/init.d/iptables#添加执行权限
update-rc.diptablesdefaults99#添加服务
systemctlstartiptables.service#启动
serviceiptablesstop#停止
#现在就可以使用上面的命令管理防火墙了,启动、停止
#如果修改了防火墙配置规则,还是需要执行/sbin/iptables-restore/etc/sysconfig/iptables命令使其生效,然后再使用防火墙管理脚本进行管理
1.3设置防火墙开机启动
1.3.1使用系统启动脚本进行设置
cp/lib/systemd/system/rc-local.service/lib/systemd/system/rc-local.service-bak#备份
ln-s/lib/systemd/system/rc-local.service/etc/systemd/system/#创建软连接文件
nano/lib/systemd/system/rc-local.service#添加[Install]段到最后
#SPDX-License-Identifier:LGPL-2.1+
#
#Thisfileispartofsystemd.
#
#systemdisfreesoftware;youcanredistributeitand/ormodifyit
#underthetermsoftheGNULesserGeneralPublicLicenseaspublishedby
#theFreeSoftwareFoundation;eitherversion2.1oftheLicense,or
#(atyouroption)anylaterversion.
#Thisunitgetspulledautomaticallyintomulti-user.targetby
#systemd-rc-local-generatorif/etc/rc.localisexecutable.
[Unit]
Description=/etc/rc.localCompatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.localstart
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
ctrl+o#保存
ctrl+x#退出
nano/etc/rc.local#创建文件,添加防火墙启动命令
#!/bin/bash
/sbin/iptables-restore/etc/sysconfig/iptables
ctrl+o#保存
ctrl+x#退出
chmod+x/etc/rc.local#添加执行权限
#重新启动系统进行测试,现在防火墙已经开机自启动了
1.3.2使用sysv-rc-conf服务设置开机启动
UbuntuServer20.x系统中默认已经没有sysv-rc-conf包了,我们无法直接使用apt-get安装
现在我们修改apt-get源
cp/etc/apt/sources.list/etc/apt/sources.list-bak#备份
nano/etc/apt/sources.list#编辑添加下面一行代码
deb