how do I change postfix port from 25 to 587?

If you only want it running on port 587 (and I'm not sure you do; I'd think you'd want it running on both 25 and 587), then find the line in /etc/postfix/master.cf that looks like this:

smtp      inet  n       -       n       -       -       smtpd

And change it to look like this:

587      inet  n       -       n       -       -       smtpd

If you want it running on both ports, then add the second line after the first one rather than replacing it.


According to your comments on other answers, you need to configure Postfix to use Gmail as a relay host. There are many tutorials on the Internet for this; here's a quick version.

Note: With this configuration, all mail must be sent using your Gmail address as "From".

  1. Undo all your changes to master.cf.

  2. In main.cf, add these settings:

    # This tells Postfix to hand off all messages to Gmail, and never do direct delivery.
    relayhost = [smtp.gmail.com]:587
    
    # This enables TLS (SMTPS) certificate verification, because Gmail has a valid one.
    smtp_tls_security_level = verify
    smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
    smtp_tls_session_cache_database = btree:/var/run/smtp_tls_session_cache
    
    # This tells Postfix to provide the username/password when Gmail asks for one.
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    
  3. In /etc/postfix/sasl_passwd, add your Gmail username and password, like this:

    [smtp.gmail.com]:587    [email protected]:mypassword
    
  4. Compile the sasl_passwd file into a database:

    postmap /etc/postfix/sasl_passwd
    
  5. Finally reload Postfix's main configuration:

    postfix reload