I've installed CSF-LFD as described here. in /etc/csf/csf.conf You have these two code blocks:

# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,2077,2078,2079,2080,2082,2083,2086,2087,2095,2096"

# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,37,43,53,80,110,113,443,587,873,993,995,2086,2087,2089,2703"

I need both port groups to be changed just to "22, 80, 443, 9000" to make sure my Nginx server environment allows incoming data only from these ports.

What's the best, safest way to do that with sed?

I think that this is a good way:

sed -i 's/^TCP_IN = .*/TCP_IN = "22, 80, 443, 9000"/' /etc/csf/csf.conf
sed -i 's/^TCP_OUT = .*/TCP_OUT = "22, 80, 443, 9000"/' /etc/csf/csf.conf

Yet, given csf.conf is a huge file and might have huge potential for changes, there might be a tiny better way.


It can be done is one pass with sed:

sed -Ei 's/^(TCP_(IN|OUT) = ).*/\1"22, 80, 443, 9000"/' /etc/csf/csf.conf
  • (IN|OUT) - regex alternation group, matches either IN or OUT substring
  • \1 - backreference, references the 1st captured group (...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.