ubuntu

ubuntu syslog网络连接

小樊
42
2025-09-27 22:49:11
栏目: 智能运维

Configuring Syslog Network Connections on Ubuntu

Syslog is a standard protocol for collecting and transmitting system logs, enabling centralized log management across multiple devices. On Ubuntu, rsyslog (the default logging daemon) and syslog-ng are commonly used to configure network-based log transmission (sending logs to a remote server) or reception (accepting logs from other devices). Below is a structured guide covering both scenarios.


1. Prerequisites

Before configuring network connections, ensure the following:


2. Configuring Ubuntu as a Syslog Client (Send Logs to a Remote Server)

To forward logs from your Ubuntu system to a central syslog server, follow these steps:

Using rsyslog (UDP)

  1. Edit the rsyslog Configuration File:
    Open the default rsyslog configuration file (or create a custom one in /etc/rsyslog.d/):

    sudo nano /etc/rsyslog.conf
    

    Uncomment or add the following line to send all logs (*.*) to the remote server via UDP (port 514):

    *.* @remote_server_ip:514
    

    Replace remote_server_ip with the actual IP address of the syslog server.

  2. Restart rsyslog:
    Apply changes by restarting the service:

    sudo systemctl restart rsyslog
    

Using rsyslog (TCP) for Reliable Transmission

For guaranteed log delivery (e.g., over unreliable networks), use TCP instead of UDP. Modify the configuration line to:

*.* @@remote_server_ip:514

The double @ symbol indicates TCP. Restart rsyslog after saving changes.

Using syslog-ng (Alternative to rsyslog)

If you prefer syslog-ng (install via sudo apt install syslog-ng), edit its configuration file:

sudo nano /etc/syslog-ng/syslog-ng.conf

Add a destination for the remote server and a log rule:

destination d_remote { tcp("remote_server_ip" port(514)); };  # TCP
# OR destination d_remote { udp("remote_server_ip" port(514)); };  # UDP

source s_local { system(); internal(); };  # Collect local logs
log { source(s_local); destination(d_remote); };  # Forward local logs to remote server

Restart syslog-ng to apply changes:

sudo systemctl restart syslog-ng

3. Configuring Ubuntu as a Syslog Server (Receive Logs from Remote Clients)

To centralize logs from multiple devices (e.g., IoT devices, servers), configure Ubuntu to accept incoming syslog traffic:

Using rsyslog

  1. Enable Remote Log Reception:
    Edit the rsyslog configuration file:

    sudo nano /etc/rsyslog.conf
    

    Uncomment or add the following lines to enable UDP (and TCP for reliability):

    module(load="imudp")  # Load UDP module
    input(type="imudp" port="514")  # Listen on UDP port 514
    
    module(load="imtcp")  # Load TCP module (optional but recommended)
    input(type="imtcp" port="514")  # Listen on TCP port 514
    
  2. Restrict Access (Optional but Secure):
    To limit log reception to specific IP addresses (e.g., 192.168.1.0/24), add rules before the input lines:

    $AllowedSender UDP, 192.168.1.0/24  # Restrict UDP to subnet
    $AllowedSender TCP, 192.168.1.0/24  # Restrict TCP to subnet
    
  3. Restart rsyslog:

    sudo systemctl restart rsyslog
    

Using syslog-ng (Alternative to rsyslog)

  1. Enable Remote Log Reception:
    Edit the syslog-ng configuration file:

    sudo nano /etc/syslog-ng/syslog-ng.conf
    

    Add a source for network traffic and a log rule to store received logs:

    source s_network { 
        udp(ip(0.0.0.0) port(514));  # Listen on all interfaces (UDP)
        tcp(ip(0.0.0.0) port(514));  # Listen on all interfaces (TCP, optional)
    };
    
    destination d_local { file("/var/log/remote.log"); };  # Store remote logs in a separate file
    
    log { source(s_network); destination(d_local); };  # Forward network logs to the destination
    
  2. Restart syslog-ng:

    sudo systemctl restart syslog-ng
    

4. Verification

After configuration, verify that logs are being transmitted/received correctly:

On the Client Side

Use the logger command to send a test log to the remote server:

logger -t test "This is a test log message from the client"

On the Server Side

Check the designated log file (e.g., /var/log/syslog for rsyslog or /var/log/remote.log for syslog-ng) for incoming logs from the client. Use tail -f to monitor in real time.


5. Troubleshooting Common Issues

By following these steps, you can successfully configure syslog network connections on Ubuntu for centralized log management or distributed logging.

0
看了该问题的人还看了