Gmail + Postfix on Ubuntu 14.04

if I tell you that the draft of this article has already existed for 400 days (more than a year)! You may wonder how ridiculous I am to not finish it earlier. Okay, the fact is that I put too much things inside one article. Now, I plan to break it up into a series of articles.

In this article, you will know how to setup a free Gmail Relay on your ubuntu server. There maybe quite a lot of online resources to do the simliar thing, but I prefer to write my own as a example and to refresh my forgetful brain. 😀

Installing Postfix

Install Postfix using the following command:

sudo apt-get install postfix

You will see this prompt at some point. Select Internet Site.

Screenshot from 2016-04-13 22:43:47

Screenshot from 2016-04-16 17:07:18

Postfix Configuration

Once the installation is finished, change your terminal’s current directory to

/etc/postfix

Configure Gmail account and password

create a file call gmail_passwd, replace the information with your credentials.

[smtp.gmail.com]:587 username@gmail.com:password

execute the following command to generate a postfix database.

sudo postmap /etc/postfix/gmail_passwd

For safely reason, you should allow only the root user can access this 2 files.

sudo chown root:root /etc/postfix/gmail_passwd /etc/postfix/gmail_passwd.db
sudo chmod 0600 /etc/postfix/gmail_passwd /etc/postfix/gmail_passwd.db

 Configure your Relay Server

open /etc/postfix/main.cf with whatever editor you like:

vim /etc/postfix/main.cf

add the following line to map your credentials

smtp_sasl_password_maps = hash:/etc/postfix/gmail_passwd

Enable TLS and sasl authiencation for Gmail SMTP Server

smtp_use_tls = yes
smtp_sasl_auth_enable = yes

Gmail SMTP Server does not support postfix’s security options. Reset them by:

smtp_sasl_security_options =

Relayhost field means the SMTP relay host. Please put the GMail SMTP server address here. Make sure this address is identical to the one in gmail_passwd

relayhost = [smtp.gmail.com]:587

here is the complete version of the main.cf:

# See /usr/share/postfix/main.cf.dist for a commented, more complete version

# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_use_tls = yes
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtp_sasl_password_maps = hash:/etc/postfix/gmail_passwd
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = office-local
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = office-local, localhost.localdomain, , localhost
relayhost = [smtp.gmail.com]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

reload the configuration:

service postfix reload 

check the status by:

tail -f /var/log/mail.log

check mail.err if there is any error. This file may not exist if there is no error, or it is the first time of you to configure mail related service.

tail -f /var/log/mail.err

Gmail Account Setting

Screenshot from 2016-04-16 16:34:10

Postfix may not works with the default security level of Gmail. Please turn on the “allow less secure apps” option via this link:

https://www.google.com/settings/security/lesssecureapps


 Testing

mailx is a popular utility in Linux World for sending and receiving mail in terminal. Execute the following command to install it.

sudo apt-get install mailx

Now you can test your postfix configuration by sending a mail to someone. Replace to@whatever.com with a valid email address.

echo "Hello Body" | mail -s "Hello Subject" -a to@whatever.com

If it was sent successfully, you should see this in the mail.log:

Apr 16 08:41:57 office-local postfix/smtp[1274]: 1603753E: to=<xxx@whatever.com>, relay=smtp.gmail.com[64.233.189.108]:587, delay=1723, delays=1720/0.01/1.4/0.85, dsn=2.0.0, status=sent (250 2.0.0 OK 1460796117 h5sm69972904pat.0 - gsmtp)
Apr 16 08:41:57 office-local postfix/qmgr[1197]: 1603753E: removed

 Trouble Shooting

Apr 16 08:27:23 office-local postfix/smtp[1007]: E81E93C5: to=<xxx@whatever.com>, relay=smtp.gmail.com[64.233.189.109]:587, delay=7, delays=0.01/0.01/7/0, dsn=4.7.14, status=deferred (SASL authentication failed; server smtp.gmail.com[64.233.189.109] said: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtS?534-5.7.14 qQtdCND0kjQn0lwMS5NJc9nXMTotKJG76X83IjhjNi5jaGjyZed5WfHf_Rm0SxoaiwFAAU?534-5.7.14 tvpINwFpyp2YOKpYmEnCnlmUn33voaf7jG8lN71043p3bgcKNY8Zqk2WB-zKKEoWW58zkM?534-5.7.14 uwn5lZcHpxEJvx5HXAEf0MQcOu_EVJLEFByoP35IPcKquJM5qnI4nBVGzFD93uC6IaSxDH?534-5.7.14 DhDeKmFa94G2XQXZItNqqLtrW8Yco> Please log in via your web browser and?534-5.7.14 then try again.?534-5.7.14 Learn more at?534 5.7.14 https://support.google.com/mail/answer/78754 n74sm69735534pfa.45 - gsmtp)

your Gmail account is not allow less-secure apps to sign-in


 

Reference

http://mhawthorne.net/posts/postfix-configuring-gmail-as-relay.html

https://community.runabove.com/kb/en/instances/how-to-relay-postfix-mails-via-smtp.gmail.com-on-ubuntu-14.04.html

Configure Postfix to Use Gmail SMTP on Ubuntu

Advertisements

OneTab keep crashing

It was just a normal day, a day similar to others.  My OneTab was suddenly not working. Some people here might not know it, OneTab is a chrome extension that make keeping and restoring tabs easy.

Screenshot from 2016-04-06 18:04:16.png

Honestly , I didn’t get surprised. Why?

I have a habit of keeping lots of tabs open in multiple windows. In a regular day, I can open up to 30+ tabs easily. That’s why I need some tools like OneTab to manage all of them. Also, I keep all the lists even they are restored. This led to that over the past 2 years, I have 4k+ tab records inside the OneTab.

The reason is simple, when I open up the OneTab page, I have a sense that it becomes slower and slower, it’s because there are too many items in the page which may cause a memory leak. What’s worse is that I can’t even remove and export the data!

So, what can I do?

Popular Linux Distro mostly is storing the data of the chrome extensions under the following path:

 /home/{YOURNAME}/.config/google-chrome/Default/Local Storage

For each extensions, there would be a .localstorage file and a .localstorage-journal file. One tab is stored as:

 chrome-extension_chphlpgkkbolifaimnlloiipkdnihall_0.localstorage

 chrome-extension_chphlpgkkbolifaimnlloiipkdnihall_0.localstorage-journal

In fact, those .localstorage files are just SQLite files. Fortunately I can solve it by doing some DB operations.

To make life simpler, I do it in GUI. Thanks to the author of sqlitebrowser.

Screenshot from 2016-04-12 00:36:38.png

Screenshot from 2016-04-12 00:37:462.png

Basically, it’s just a simple database containing 1 key-value table only. There are 7 records in this table but only 2 records, idCounter and state, are matters.

  • idCounter: the last primary key of tabs
  • state: the tabs records that stored in JSON string

Copy and paste the value of “state” to any editor, you should see something like this:

{
  "tabGroups": [
    {
      "id": 3198,
      "tabsMeta": [
        {
          "id": 3182,
          "url": "http://localhost:3000/users/75/edit",
          "title": "Servlet Error"
        },
        {
          "id": 3183,
          "url": "https://docs.racket-lang.org/reference/lambda.html?q=lambda#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29",
          "title": "3.8 Procedure Expressions: lambda and case-lambda"
        },
        {
          "id": 3185,
          "url": "https://docs.racket-lang.org/reference/exns.html?q=object-name#%28def._%28%28quote._~23~25kernel%29._raise-user-error%29%29",
          "title": "10.2 Exceptions"
        },
        {
          "id": 3188,
          "url": "https://docs.racket-lang.org/reference/ivaraccess.html",
          "title": "6.4 Field and Method Access"
        },
        {
          "id": 3197,
          "url": "http://stackoverflow.com/questions/23296384/application-not-a-procedure-racket",
          "title": "condition - application: not a procedure racket - Stack Overflow"
        }
      ],
      "createDate": 1453743658563
    },
    {
      "id": 3212,
      "tabsMeta": [
        {
          "id": 3199,
          "url": "https://docs.racket-lang.org/reference/exns.html?q=object-name#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail%29%29",
          "title": "10.2 Exceptions"
        },
        {
          "id": 3200,
          "url": "https://docs.racket-lang.org/reference/define-struct.html?q=object-name#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-struct%29%29",
          "title": "5.1 Defining Structure Types: struct"
        },
        ...
      ],
      "createDate": 1454092984398
    },
     ...
  ]
}

It’s Just a pure Json data. I make a copy of this for backup purposes. Now I can remove some items here. I did few tests to find the recommended size, I prefer to limit to 1000. Besides that make sure the id of the last item is the same as the value of idCounter in itemTable.

Data is ready, But …

You can’t make this patch work if chrome is running. Basically, all the chrome’s instances must be terminated before you are touching the extension data. To do so, you might use some commands like: kill, top or htop, etc.

Finally just replace the state field of itemTable with the modified version of the json data. And of course, OneTab is back!

ISCSI 設定筆記 (2): Ubuntu 上使用 Initiator 連接 ISCSI Target

尋找指定 IP 的 ISCSI Server 所提供的 ISCSI Target :

iscsiadm -m discovery -t sendtargets -p 172.0.0.1

顯示基本連接設定:

iscsiadm -m node -p 172.0.0.1 -o show

更改設定的格式如下:

iscsiadm -m node -p 172.0.0.1 -o update -n <name> -v <value>

或者

iscsiadm -m node -p 172.0.0.1 --op=update --name=<name> --value=<value>

設定成以用戶登錄:

iscsiadm -m node -p 172.0.0.1 -T "iqn.2013-8.com.osiutino:target0" -o update -n node.session.auth.authmethod -v CHAP
iscsiadm -m node -p 172.0.0.1 -T "iqn.2013-8.com.osiutino:target0" -o update -n node.session.auth.username -v username
iscsiadm -m node -p 172.0.0.1 -T "iqn.2013-8.com.osiutino:target0" -o update -n node.session.auth.password -v password

開機自動連接這個 target:

iscsiadm -m node -p 172.0.0.1 -o update -n node.conn[0].startup = automatic

手動登錄:

iscsiadm -m node -p 172.0.0.1 --login

出現以下訊息代表登錄成功

Logging in to [iface: default, target: iqn.2013-8.com.osiutino:target0, portal: 172.0.0.1,3260] (multiple)
Login to [iface: default, target: iqn.2013-8.com.osiutino:target0, portal: 172.0.0.1,3260] successful.

手動登出:

iscsiadm -m node -p 172.0.0.1 --logout

若果剛才入登錄成功,在終端機中輸入以下指令即可查閱該 target 的磁碟資訊:

 fdisk -l 

SAN/iSCSI/open-iscsi – Debian Wiki

Ubuntu 12.04 LTS – Configure iSCSI Initiator : Server World

Ubuntu 12.04 LTS – Configure iSCSI Target : Server World

ISCSI 設定筆記 (1): Ubuntu 上架設 ISCSI Server

最近改動一下備份方案,打算掛載多個 iscsi targets 作備份之用,並且順便寫份筆記做個記錄。

利用 ssh 連接到 ubuntu server ,輸入以下指令找出建立 iscsi target 的磁碟:

fdisk -l

輪出如下:

Disk /dev/sda: 256.1 GB, 256060514304 bytes
255 heads, 63 sectors/track, 31130 cylinders, total 500118192 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000cca3d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758   500117503   249807873    5  Extended
Partition 2 does not start on physical sector boundary.
/dev/sda5          501760   500117503   249807872   8e  Linux LVM

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
81 heads, 63 sectors/track, 382818 cylinders, total 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000abc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1953525167   976761560   83  Linux

在 sdb1 上建立 physical volume :

pvcreate /dev/sdb1

查看一下 server 上 physical volume(s) 的資訊:

pvdisplay

新增 1 個 volume group:

vgcreate vg_target0 /dev/sdb1

如同 pvdisplay, 輸入以下指令即可查看 本機 volume group(s) 資訊:

vgdisplay

新增 1 個 100Gb 的 logical volume:

lvcreate -L 100G -n lv_target0 vg_target0

查看這個 LV:

lvdisplay

修改這個設定檔:

vim /etc/iet/ietd.conf

加入以下設定資料:

Target iqn.2013-8.com.osiutino:target0
	LUN 0 Path=/dev/vg_target0/lv_target0,Type=blockio	
	initiator-adress 172.0.0.2
	incominguser username password
	alias LUN1

重啟服務以載入新設定:

service iscsitarget restart

Using iSCSI On Ubuntu 10.04 (Initiator And Target) | HowtoForge – Linux Howtos and Tutorials

酷爾的家: 在Ubuntu 使用 iSCSI Initiator

鳥哥的 Linux 私房菜 — 網路磁碟裝置:iSCSI伺服器

ASP.NET MVC3 Project on Mono

剛才成功把一個 MVC3 的 Project 移植到 ubuntu 上運行。

  1.  Mono 需要到這3個 assemblies 組件。逐一按  “Add Reference” 加入到 Project 中。
    System.Web.WebPages
    System.Web.WebPages.Deployment
    System.Web.WebPages.Razor
  2.  Mono 沒有以下assemblies 組件,需要在Visual Studio滙出。按下列組件,在Properies 視窗中把 Copy Local 設成 “True”。
    System.Web.Helpers
    System.Web.Mvc
    System.Web.Razor
    System.Web.Routing
    System.Web.WebPages
    System.Web.WebPages.Deployment
    System.Web.WebPages.Razor
  3. Mono 對以下assemblies 組件不相容。按下列組件,再按Delete 刪除。
    EntityFramework
    System.Data.Entity
    System.Web.Entity
  4. 按build 進行編譯,然後把專案複製到 ubuntu 。
  5. 使用 MonoDevelop 開啓這個專案 。這時你需要把錯掉了的 References 刪除 (Step 2),然後重新加入 正確路徑的 References。按 “Edit References”  選  “.Net Assembly” ,進入 Output Directory 然後加入下列組件
    System.Web.Helpers.dll
    System.Web.Mvc.dll
    System.Web.Razor.dll
    System.Web.Routing.dll
    System.Web.WebPages.dll
    System.Web.WebPages.Deployment.dll
    System.Web.WebPages.Razor.dll

    按 build 進行編譯,沒錯的話代表移植成功

  6. xsp4 是 mono 的 development web server , 用來運行 ASP.NET 專案。在terminal 中輸入以下指令安裝 xsp4:
    sudo apt-get install mono-xsp4

    要啓動 Web Server,先把 termial 切換路徑至專案中 ,然後輸入 “xsp4″即可


相關連結:
ASP.NET – Mono
ASP.NET MVC3 Support
Deploying your ASP.NET MVC3 site on Mono

Tor for chrome in ubuntu

忙中偷閒寫個script. Tor for chrome in ubuntu
設好 TORPATH 基本上可馬上用

#!/bin/bash # >> ------------------------------- # tor for Chrome version 1.0 # By: SiuTin # Date: 26,April 2012 # Website: osiutino.wordpress.com # ------------------------------- << # set tor path TORPATH=~/tor-browser_en-US echo "=> TOR FOR CHROME" # set port.js path PREFSJS=$TORPATH/Data/profile/prefs.js # get dynamic port number PORT=$(cat $PREFSJS | grep network.proxy.socks_port | egrep -e '([0-9])+' -o) # combine address and port ADDR=127.0.0.1:$PORT echo "=> TOR ADDESS = $ADDR" echo "=> starting chrome ...." # run chrome /usr/bin/google-chrome --proxy-server="socks://$ADDR;socks4://$ADDR;socks5://$ADDR;" check.torproject.org

在 ubuntu 11.10 上安裝 Xspim/QtSpim – MIPS32 Simulator

學校裡某個課程是關於 MIPS Architecture, 邊使用 emulator 邊看書有助學習。Course page 提到了 Spim simluator 。在官方網站看了一遍,除了window version 外 spim 還支援 Linux / mac。並且提供了三個 GUI 版本:

  • Xspim (Xwindow)
  • PCSpim (Microsoft Window)
  • Qtspim  (Qt Library)

Xspim 和 PCSpim 都非常舊,官方推薦使用 Qtspim。不過在這裡我會把 Xspim 及Qtspim 的安裝過程都記錄下來。

花了點時間安裝,平台是 ubuntu 11.10。
 

XSpim

1. 在 Ubuntu Software Center 中搜尋 xspim 並且 按 install 安裝

2. 安裝完成後,在 terminal 鍵入:

xspim

會出現這句:

*-courier-medium-r-normal--12-*-75-*

然後什麼都沒有啟動,這是因為字型出了問題.

3. 成功啟動方法如下:

xspim -font 6x10

 

QtSpim

1. QtSpim 沒有 For Ubuntu 的套件,需要自已Build。利用SVN 下載 Source Code,在Terminal 上回到home directory ,然後鍵入 :

svn co https://spimsimulator.svn.sourceforge.net/svnroot/spimsimulator spimsimulator

2. 下載 Qt Sdk version 4.8 32bit 64bit for linux
切換到 Qt Sdk的目錄下輸入:

chmod u+x Qt_SDK_Lin32_offline_v1_2_en.run

然後執行安裝程式:

./Qt_SDK_Lin32_offline_v1_2_en.run

3. 安裝完成後,運行 QtCreator 並且開啟專案,路徑如下:

~/spimsimulator/QtSpim/QtSpim.pro

4. 按一下 build 按鈕,QtCreator 開始編譯。

5. 編譯了的程式放在這裏:

~/spimsimulator/QtSpim-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Release/

6. 在 Terminal 切換至上述路徑, 啟動Qtspim :

./QtSpim &

成功運行,可以專心學習了 !