Firewalls have been implemented for exactly such cases. And under Linux, the wonderful tool iptables is used. With this tool, ports, protocols, network interfaces and IP addresses can be released and blocked. Here is a list of the most important commands. It is important that each block command is accompanied by an allow command. We will only deal with the Allow commands here. Think positively.
Let's start with the classic. We want to release a special port on a server. Port 80, for example, would be HTTP, 443 the one for HTTPS. To release these ports, we use the following command.
                    
                    firewall:iptables:ports:allow:port
                
                        
                    Block a port from incoming traffic.
                
            
    $ sudo iptables -A INPUT -p tcp --dport ${port_number} -j ACCEPT
    
        try on your machine
    
                        
                
                    explain this command
                
            
            
    
                    
                    firewall:iptables:ip-address:allow:address
                
                        
                    Allow incoming traffic from specific IP addresses.
                
            
    $ sudo iptables -A INPUT -s ${ip_address} -j ACCEPT
    
        try on your machine
    
                        
                
                    explain this command
                
            
            
    
                    
                    firewall:iptables:interface:allow:name
                
                        
                    Allow incoming traffic from specific network interface.
                
            
    $ sudo iptables -A INPUT -i ${interface_name} -j ACCEPT
    
        try on your machine
    
                        
                
                    explain this command
                
            
            
    
                    
                    firewall:iptables:outgoing:block-all
                
                        
                    Block all incoming traffic.
                
            
    $ sudo iptables -P INPUT DROP
    
        try on your machine
    
                        
                
                    explain this command
                
            
            
    
                    
                    firewall:iptables:protocol:allow:name
                
                        
                    Allow incoming traffic from specific protocol.
                
            
    $ sudo iptables -A INPUT -p ${protocol_name} -j ACCEPT
    
        try on your machine
    
                        
                
                    explain this command
                
            
            
    
