this post was submitted on 27 Jan 2026
2 points (100.0% liked)

Ubuntu Linux

2308 readers
1 users here now

Linux for Human Beings.

Ubuntu is a popular Linux operating system for PC / mobile devices, etc.

Developed by Canonical & based on Debian (another older Linux OS) which is known for it's rock solid stability.

Ubuntu is trusted everywhere computing by professionals and common users alike.

https://ubuntu.com/

founded 6 years ago
MODERATORS
 

cross-posted from: https://lemmy.ca/post/59381417

I often need to allow some randomly selected port to be open (tcp & udp) in the Uncomplicated Firewall (UFW), while some app is active. Then I'd like to close it. The port number is written in a file, say portfile

At the moment I'm doing this manually: read the number, then call sudo ufw allow xxxx/tcp in a terminal. Later on, delete the port rule with sudo ufw delete [rulenumber].

I'm trying to write a bash script to do this in a more automated way. It's easy to read the number from the flie as a variable, then call ufw with that number (provided the script is started as sudo).

What's not clear to me is how to delete the UFW rule once the application is closed. I could start the app within the bash script itself. Maybe it'd just be a matter of waiting for it to finish?

I'm very thankful for suggestions and ideas โ€“ and learning more about bash tricks :)

top 1 comments
sorted by: hot top controversial new old
[โ€“] PointyFluff@lemmy.ml 2 points 2 weeks ago* (last edited 2 weeks ago)

[sed](https://dn720006.ca.archive.org/0/items/english-collections-k-z/Sed%20%26%20awk%2C%202nd%20Edition%20-%20Dale%20Dougherty%20%26%20Arnold%20Robbins.pdf)