{"data":{"site":{"siteMetadata":{"siteUrl":"https://sat10am.dev","shortname":"sat10am"}},"post":{"id":"Post_1-markdown","title":"CentOS 7 Mail Server 구축하기 Postfix + Dovecot","view":0,"slug":"/posts/centos7-mail-server","publishAt":"2018.09.28","tags":[{"name":"linux"}],"childMarkdownRemark":{"html":"<p>메일서버를 구축할일이 생겨서 간단하게 Postfix와 Dovecot개념을 익힐겸 구축해보았다.\n이번 포스팅은 간단하게 Postfix+Dovecot를 이용해 SMTP, IMAP 서버를 구축하는 과정에대해 작성해보려 한다. </p>\n<p>먼저 설치와 셋팅에 앞서서 도메인의 DNS를 먼저 셋팅해주도록 하자.\n일반적으로 도메인이 example.com 이라면 메일서버는 mail.example.com 서브도메인을 활용하게 된다.\nmail.example.com 의 A레코드를 먼저 등록한 후 example.com 의 MX레코드를 mail.example.com으로 설정한다. </p>\n<p>먼저 yum을 update해준다.\n<code class=\"language-text\">yum -y update</code></p>\n<h2 id=\"postfix\"><a href=\"#postfix\" aria-label=\"postfix permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Postfix</h2>\n<p>완료되었다면 아마 postfix가 설치되어있을 것이다.\n만약 없을경우 설치해주도록 하자.\n<code class=\"language-text\">yum -y install postfix</code></p>\n<p>다음은 ssl인증서를 셋팅한다.(ssl을 이용하지 않는다면 필수사항이 아니다.)</p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">mkdir /etc/postfix/ssl\ncd /etc/postfix/ssl</code></pre></div>\n<p>ssl폴더로 이동했다면 openssl 을통해 key,crt 파일을 만든다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">openssl req -x509 -nodes -newkey rsa:2048 -keyout server.key -out server.crt -nodes -days 365</code></pre></div>\n<p>이제 postfix 설정을 수정한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">vi /etc/postfix/main.cf</code></pre></div>\n<p>아래 나와있는 설정들을 현재 상황에 맞게 수정하도록 한다.\n없는부분이 있을시엔 추가하도록 한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">myhostname = mail.yourdomain.com\nmydomain = yourdomain.com\nmyorigin = $mydomain\nhome_mailbox = mail/\nmynetworks = 127.0.0.0/8\ninet_interfaces = all\ninet_protocols = all\nmydestination = $myhostname, localhost.$mydomain, localhost, $mydomain\nsmtpd_sasl_type = dovecot\nsmtpd_sasl_path = private/auth\nsmtpd_sasl_local_domain =\nsmtpd_sasl_security_options = noanonymous\nbroken_sasl_auth_clients = yes\nsmtpd_sasl_auth_enable = yes\nsmtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination\nsmtp_tls_security_level = may\nsmtpd_tls_security_level = may\nsmtp_tls_note_starttls_offer = yes\nsmtpd_tls_loglevel = 1\nsmtpd_tls_key_file = /etc/postfix/ssl/server.key\nsmtpd_tls_cert_file = /etc/postfix/ssl/server.crt\nsmtpd_tls_received_header = yes\nsmtpd_tls_session_cache_timeout = 3600s\ntls_random_source = dev:/dev/urandom</code></pre></div>\n<p>다음은 master.cf파일을 수정한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">vi /etc/postfix/master.cf</code></pre></div>\n<p>아래와 다르게 주석이 되있는부분들은 해제하도록 한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">smtp      inet  n       -       n       -       -       smtpd\n#smtp      inet  n       -       n       -       1       postscreen\n#smtpd     pass  -       -       n       -       -       smtpd\n#dnsblog   unix  -       -       n       -       0       dnsblog\n#tlsproxy  unix  -       -       n       -       0       tlsproxy\nsubmission inet n       -       n       -       -       smtpd\n  -o syslog_name=postfix/submission\n  -o smtpd_tls_security_level=encrypt\n  -o smtpd_sasl_auth_enable=yes\n  -o smtpd_reject_unlisted_recipient=no\n#  -o smtpd_client_restrictions=$mua_client_restrictions\n#  -o smtpd_helo_restrictions=$mua_helo_restrictions\n#  -o smtpd_sender_restrictions=$mua_sender_restrictions\n  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject\n  -o milter_macro_daemon_name=ORIGINATING\nsmtps     inet  n       -       n       -       -       smtpd\n  -o syslog_name=postfix/smtps\n  -o smtpd_tls_wrappermode=yes\n  -o smtpd_sasl_auth_enable=yes\n  -o smtpd_reject_unlisted_recipient=no\n#  -o smtpd_client_restrictions=$mua_client_restrictions\n#  -o smtpd_helo_restrictions=$mua_helo_restrictions\n#  -o smtpd_sender_restrictions=$mua_sender_restrictions\n  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject\n  -o milter_macro_daemon_name=ORIGINATING</code></pre></div>\n<h2 id=\"dovecot\"><a href=\"#dovecot\" aria-label=\"dovecot permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Dovecot</h2>\n<p>IMAP/POP3로 이메일을 수신할 수 있도록 Dovecot를 설치하자. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">yum -y install dovecot</code></pre></div>\n<p>이제 설정을 수정하도록 하자.</p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">vi /etc/dovecot/conf.d/10-master.conf</code></pre></div>\n<p>smtp-auth를 찾아서 아래와같이 설정한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\"># Postfix smtp-auth\nunix_listener /var/spool/postfix/private/auth {\n\tmode = 0660\n\tuser = postfix\n\tgroup = postfix\n}</code></pre></div>\n<p>auth 설정을 수정한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">vi /etc/dovecot/conf.d/10-auth.conf</code></pre></div>\n<p>auth_mechanisms을 찾아서 뒤에 login을 붙여준다. 아마 plain으로 되어있을것이다.</p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">auth_mechanisms = plain login\n\n# 이 설정은 안해주면 계정을 입력할때 Test@example.com 이 아니라 Test로 입력해야 로그인 된다. \n# 꼭 설정해주도록 하자.\nauth_username_format = %Ln</code></pre></div>\n<p>mail location 을 지정한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">vi /etc/dovecot/conf.d/10-mail.conf</code></pre></div>\n<p>이 부분은 위에 home_mailbox와 맞춰주어야 한다.\n제대로 설정되어있지 않으면 나중에 메일수신이 안될 수 있다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">mail_location = maildir:~/mail</code></pre></div>\n<p>다음은 pop3 설정이다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">vi /etc/dovecot/conf.d/20-pop3.conf</code></pre></div>\n<p>아래 라인이 주석처리를 해제한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">pop3_uidl_format = %08Xu%08Xv </code></pre></div>\n<p>서비스를 재시작해서 모든 설정을 적용한다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">systemctl restart postfix\nsystemctl enable postfix\nsystemctl restart dovecot\nsystemctl enable dovecot</code></pre></div>\n<p>관련포트를 firewall에 추가해주고 테스트하도록 하자. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">firewall-cmd --permanent --add-service=smtp\nfirewall-cmd --permanent --add-port=587/tcp\nfirewall-cmd --permanent --add-port=465/tcp\nfirewall-cmd --permanent --add-port=110/tcp\nfirewall-cmd --permanent --add-service=pop3s\nfirewall-cmd --permanent --add-port=143/tcp\nfirewall-cmd --permanent --add-service=imaps\nfirewall-cmd --permanent --add-service=http\nfirewall-cmd --reload</code></pre></div>\n<p>테스트시에는 telnet이나 <a href=\"https://mxtoolbox.com/\">MXToolbox</a>를 이용하면 된다.\n간단히 테스트가 성공했다면 Outlook, 혹은 이메일 클라이언트에 연결해서 동작을 확인하면 된다.\n이메일 클라이언트로 연결시 계정은 기본적으로 리눅스 계정이 공유된다. </p>\n<div class=\"gatsby-highlight\" data-language=\"sh\"><pre class=\"language-sh\"><code class=\"language-sh\">adduser test\npasswd test\n\n# mail directory \n# /home/test/mail</code></pre></div>\n<p>간단하게 만들어서 테스트해볼 수 있다. </p>\n<p>계정은 이외에도 mysql이나 다른방법으로 관리할 수 있는 것 같다.\n<a href=\"https://www.linode.com/docs/email/postfix/email-with-postfix-dovecot-and-mysql/\">postfix &#x26; mysql</a></p>\n<h2 id=\"마무리\"><a href=\"#%EB%A7%88%EB%AC%B4%EB%A6%AC\" aria-label=\"마무리 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>마무리</h2>\n<p>위 과정들이 어렵다면 사실 docker를 이용해서 관련 이미지를 받아서 사용할 수 있다.\n이미 잘 설정되어있는 이미지들이 조금 검색하면 많이나오는 편이다.\n하지만 관련설정을 모를시 혼동이 있을 수 있으므로 vm에서라도 간단히 테스트해보면 좋을 것 같다. </p>"},"author":{"username":"y0c","intro":"Web Developer & Student","email":"holnet1026@gmail.com","github":"https://github.com/y0c","profile":{"url":"/uploads/28f8f2ee1ab84c0b86610b60402bc4ee.jpeg"}},"thumbnail":{"childImageSharp":{"fluid":{"aspectRatio":1.5486725663716814,"src":"/static/2405891257038c97f8860671503adc06/ac3ec/582461c24b608caa144f2ab24d4a006c.jpg","srcSet":"/static/2405891257038c97f8860671503adc06/08c2b/582461c24b608caa144f2ab24d4a006c.jpg 175w,\n/static/2405891257038c97f8860671503adc06/e3cce/582461c24b608caa144f2ab24d4a006c.jpg 350w,\n/static/2405891257038c97f8860671503adc06/ac3ec/582461c24b608caa144f2ab24d4a006c.jpg 700w,\n/static/2405891257038c97f8860671503adc06/1ff7d/582461c24b608caa144f2ab24d4a006c.jpg 1050w,\n/static/2405891257038c97f8860671503adc06/1b259/582461c24b608caa144f2ab24d4a006c.jpg 1280w","sizes":"(max-width: 700px) 100vw, 700px"}}}},"previous":{"title":"Docker Private Registry 구축하기","slug":"/posts/docker-private-registry"}},"pageContext":{"isCreatedByStatefulCreatePages":false,"id":"Post_1-markdown","hasPrevious":true,"hasNext":false,"previousId":"Post_2-markdown","nextId":null}}