Cubie Truck 測試 – 解決無法 ssh 連線的問題

亂玩了一下子,Lubuntu 系統被玩掛了,所以又重新使用 PhoenixSuit 燒錄系統到 Nand Flash。
系統開機後,發現竟然無法 ssh 連線
Pietty 錯誤訊息

Putty 錯誤訊息
[@more@]ssh 連線出現的錯誤訊息
# ssh linaro@192.168.1.110
Read from socket failed: Connection reset by peer

但系統 Ping 的到

ssh 服務有開放
# netstat -ant | grep :22 | grep LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

iptables 防火牆也沒有阻擋
# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

後來檢查 ssh server 的設定檔目錄
# cd /etc/ssh
# ls -latr
total 156
-rw-r–r–  1 root root   1669 Sep  6  2012 ssh_config
-rw-r–r–  1 root root 136156 Sep  6  2012 moduli
-rw-r–r–  1 root root      0 Oct  8 13:40 sshd_config
-rw-r–r–  1 root root      0 Oct  8 13:40 ssh_host_rsa_key.pub
-rw——-  1 root root      0 Oct  8 13:40 ssh_host_rsa_key
-rw-r–r–  1 root root      0 Oct  8 13:40 ssh_host_dsa_key.pub
-rw——-  1 root root      0 Oct  8 13:40 ssh_host_dsa_key
-rw-r–r–  1 root root      0 Oct  8 13:40 ssh_host_ecdsa_key.pub
-rw——-  1 root root      0 Oct  8 13:40 ssh_host_ecdsa_key
-rw——-  1 root root    980 Oct  8 13:42 ssh_host_key
drwxr-xr-x  2 root root   4096 Oct  8 13:42 .
-rw-r–r–  1 root root    645 Oct  8 13:42 ssh_host_key.pub
drwxrwxr-x 97 root root   4096 Oct  8 13:51 ..

發現 ssh_host_* key 的檔案大小都是 0

建立 ssh_host_key
# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
/etc/ssh/ssh_host_rsa_key already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
8c:0a:32:2a:1b:02:6f:70:a1:2e:44:50:ee:35:f3:de root@cubietruck
The key’s randomart image is:
+–[ RSA 2048]—-+
|…              |
|..               |
| .o +            |
|.o o + o         |
|*.+   o S        |
|=* . o .         |
|*.o . . E        |
|++               |
|.                |
+—————–+
# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
Generating public/private dsa key pair.
/etc/ssh/ssh_host_dsa_key already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
2f:36:fc:7c:5b:d4:d9:33:f4:2a:e6:fb:9b:5f:8c:81 root@cubietruck
The key’s randomart image is:
+–[ DSA 1024]—-+
|                 |
|                 |
|               . |
|             …+|
|        S   E o++|
|       . .   . =o|
|        = . o + o|
|       . = o.o ..|
|          o.+++o.|
+—————–+

建立完成後,就可以正常連線了!
# ssh linaro@192.168.1.110
The authenticity of host ‘192.168.1.110 (192.168.1.110)’ can’t be established.
RSA key fingerprint is 8c:0a:32:2a:1b:02:6f:70:a1:2e:44:50:ee:35:f3:de.
Are you sure you want to continue connecting (yes/no)? yes

Banana Pi 測試 – 使用 logwatch 寄送 Log 檔

在使用 CentOS Linux 時都會使用 logwatch,每天固定寄送系統 Log 給系統管理者,藉此了解目前系統使用的狀況。
在 Banana Pi 因為是拿來當做 Server 使用,所以也另外安裝了 logwatch
# apt-get install logwatch

設定寄給 root 的信件轉寄給會常收信的使用者
# echo “root: test@gmail.com” >> /etc/aliases

讓設定生效
# /usr/bin/newaliases[@more@]但設定完成之後,卻一直收不到相關的信件

查看一下系統 mail 的狀態
# /usr/bin/mailq
27h  4.3K 1XWuzr-0002FO-47 <> *** frozen ***
          root@bananapi
3h  6.5K 1XXHTP-0002×8-Q2 <> *** frozen ***
          root@bananapi
14m  6.6K 1XXKHz-0003cA-4m <> *** frozen ***
          root@bananapi
13m  1.2K 1XXKJ2-0003ca-Oa <> *** frozen ***
          root@bananapi

因為 Debian 的 mail server 是使用 exim4,所以查看一下 mail server 的相關記錄
# head -5 /var/log/exim4/mainlog
2014-09-26 06:32:22 Start queue run: pid=11655
2014-09-26 06:32:22 1XWuzr-0002FO-47 Message is frozen
2014-09-26 06:32:22 1XWYWS-0001UY-F3 Unfrozen by errmsg timer
2014-09-26 06:32:22 1XWYWS-0001UY-F3 ** test@gmail.com <root@bananapi> R=nonlocal: Mailing to remote domains not supported
2014-09-26 06:32:22 1XWYWS-0001UY-F3 test@gmail.com <root@bananapi>: error ignored

發現有 Mailing to remote domains not supported 的訊息,主要是 exim4 在沒有特別設定時,是只能在 local 端運作,無法遠端寄信

設定 exim4
#
# sed -i ‘/configtype/s/local/internet/’ /etc/exim4/update-exim4.conf.conf

# /usr/sbin/exim4 -bV
Exim version 4.80 #3 built 02-Jan-2013 19:15:12
Copyright (c) University of Cambridge, 1995 – 2012
(c) The Exim Maintainers and contributors in ACKNOWLEDGMENTS file, 2007 – 2012
Berkeley DB: Berkeley DB 5.1.29: (October 25, 2011)
Support for: crypteq iconv() IPv6 GnuTLS move_frozen_messages DKIM
Lookups (built-in): lsearch wildlsearch nwildlsearch iplsearch cdb dbm dbmjz dbmnz dnsdb dsearch nis nis0

passwd
Authenticators: cram_md5 plaintext
Routers: accept dnslookup ipliteral manualroute queryprogram redirect
Transports: appendfile/maildir/mailstore autoreply lmtp pipe smtp
Fixed never_users: 0
Size of off_t: 8
Configuration file is /var/lib/exim4/config.autogenerated

# /etc/init.d/exim4 restart
[ ok ] Stopping MTA for restart: exim4_listener.
[ ok ] Restarting MTA: exim4

echo “Hello ! ” | mail -s “My Testing Mail”

Linux 基礎 – 使用 cat 建立檔案

# cat shell/catfile.sh
#!/bin/bash
cat <<EOF >log.txt
LOG FILE HEADER
This is a test log file
Function: System statistics
EOF

更改權限
# chmod 700 shell/catfile.sh

執行
# shell/catfile.sh

看成果
# cat shell/log.txt
LOG FILE HEADER
This is a test log file
Function: System statistics

Cubie Truck 測試 – 從 SATA 硬碟上的 root 分割區來開機

參考網頁:
tutorials:ct1:installation:moving_rootfs_from_nandflash_to_hard_drive [Cubieboard Docs]

查看一下 SATA 硬碟的硬碟分割表資訊
# fdisk -l /dev/sda

格式化硬碟為 ext4 格式
# mkfs.ext4 /dev/sda1

使用 dd 指令,將 /dev/nandb 複製到 /dev/sda1
# dd if=/dev/nandb of=/dev/sda1 bs=1M
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 205.5 s, 10.5 MB/s

掛載 /dev/nanda 開機分割區
# mount /dev/nanda /mnt

修改開機參數,從 SATA 硬碟上的 root 分割區來開機
# sed -i ‘s|nand_root=/dev/nandb|nand_root=/dev/sda1|’ /mnt/uEnv.txt

 取消掛載
# umount /mnt

重新啟動電腦
# reboot

Banana Pi 測試 – ArchLinux 安裝 Samba Server

安裝 Samba Server
# pacman -S samba

修改設定檔
# cp /etc/samba/smb.conf.default /etc/samba/smb.conf
# egrep -v ‘^$|^#|^;’ /etc/samba/smb.conf
[global]
   workgroup = HOME
   server string = Samba Server
   printcap name = /etc/printcap
   load printers = yes
   log file = /var/log/samba/%m.log
   max log size = 50
   security = user
   dns proxy = no
[homes]
   comment = Home Directories
   browseable = no
   writable = yes
[printers]
   comment = All Printers
   path = /var/spool/samba
   browseable = no
   guest ok = no
   writable = no
   printable = yes
[@more@]設定使用者 Samba 密碼
# /usr/bin/pdbedit -a bananapi

測試設定檔是否正確
# /usr/bin/testparm

啟動 Samba Server
# system start smbd
# systemctl start nmbd

設定開機時啟動 Samba Server
# systemctl enable smbd
Created symlink from /etc/systemd/system/multi-user.target.wants/smbd.service to /lib/systemd/system/smbd.service.
# systemctl enable nmbd
Created symlink from /etc/systemd/system/multi-user.target.wants/nmbd.service to /lib/systemd/system/nmbd.service.

檢查是否有正常啟動
# netstat -antulp | egrep  ‘:445|:139|:137|:138’
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      15106/smbd
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      15106/smbd
tcp        0      0 192.168.1.103:445       192.168.1.6:2283        ESTABLISHED 15138/smbd
tcp6       0      0 :::445                  :::*                    LISTEN      15106/smbd
tcp6       0      0 :::139                  :::*                    LISTEN      15106/smbd
udp        0      0 192.168.1.255:137       0.0.0.0:*                           15112/nmbd
udp        0      0 192.168.1.103:137       0.0.0.0:*                           15112/nmbd
udp        0      0 0.0.0.0:137             0.0.0.0:*                           15112/nmbd
udp        0      0 192.168.1.255:138       0.0.0.0:*                           15112/nmbd
udp        0      0 192.168.1.103:138       0.0.0.0:*                           15112/nmbd
udp        0      0 0.0.0.0:138             0.0.0.0:*                           15112/nmbd

在 Windows 7 下測試
在檔案總管下輸入 SambaServer’IPusername

輸入使用者帳號及密碼

可以正常讀寫

其它分享的目錄

Cubie Truck 測試 – Lubuntu 外接 SATA 硬碟

外接 SATA 硬碟的方式
[@more@]

安裝 ntfs-3g 程式
$ sudo apt-get install ntfs-3g

查看一下 SATA 硬碟掛載資訊
$  sudo fdisk -l /dev/sda 

建立掛載的目錄及掛載
$ sudo mkdir /mnt/sda1
$ sudo mount -t ntfs /dev/sda1 /mnt/sda1

因為一般使用者似乎不能正常讀寫,所以我切換到 root 身份
$ su –
# cd /mnt/sda1
# mkdir 新增資料夾

檔案管理程式可以看到建立的資料夾

刪除資料夾
# rm -rf 新增資料夾

在檔案管理程式中,可以看到資料夾已經刪除了!

Banana Pi 測試 – 安裝 vsftpd Server

安裝 vsftpd FTP Server
# apt-get install vsftpd
進行設定,vsftpd FTP Server 的設定檔在 /etc/vsftpd.conf
取消匿名者登入
# sed -i ‘s/anonymous_enable=YES/#anonymous_enable=YES/’ /etc/vsftpd.conf

讓系統中的使用者可以登入
# sed -i ‘s/#local_enable=YES/local_enable=YES/’ /etc/vsftpd.conf

可以寫入
# sed -i ‘s/#write_enable=YES/write_enable=YES/’ /etc/vsftpd.conf[@more@]
系統記錄檔
# sed -i ‘s|#xferlog_file=/var/log/vsftpd.log|xferlog_file=/var/log/vsftpd.log|’ /etc/vsftpd.conf

vsftpd FTP Server 登入時顯示的 Banner
# sed -i ‘s/#ftpd_banner=Welcome to blah FTP service./ftpd_banner=Welcome to Banana Pi FTP service./’ /etc/vsftpd.conf

chroot 系統的使用者
# sed -i ‘120s/#chroot_local_user=YES/chroot_local_user=YES/’ /etc/vsftpd.conf
啟用 chroot 的功能
# sed -i ‘s/#chroot_list_enable=YES/chroot_list_enable=YES/’ /etc/vsftpd.conf
建立 /etc/vsftpd.chroot_list 設定檔
# touch /etc/vsftpd.chroot_list

讓 root 可以登入
# sed -i ‘s/root/#root/’ /etc/ftpusers

啟動 vsftpd FTP Server
# /etc/init.d/vsftpd start

在防火牆中開啟 vsftpd FTP Server
iptables -A INPUT -i eth0 -p tcp –dport 21 -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp –dport 21 -j ACCEPT

設定 fail2ban 的 ftp 阻擋功能
# vim /etc/fail2ban/jail.conf
[vsftpd]

enabled  = true
port     = ftp,ftp-data,ftps,ftps-data
filter   = vsftpd
logpath  = /var/log/vsftpd.log
# or overwrite it in jails.local to be
# logpath = /var/log/auth.log
# if you want to rely on PAM failed login attempts
# vsftpd’s failregex should match both of those formats
maxretry = 3
bantime  = 86400

重新啟動 fail2ban
# /etc/init.d/fail2ban restart

目前已經有二個阻擋的規則 ssh, vsftpd
# fail2ban-client status
Status
|- Number of jail:      2
`- Jail list:           vsftpd, ssh

可以在 vsftpd 列出有偵測到的連線攻擊
# fail2ban-client status vsftpd
Status for the jail: vsftpd
|- filter
|  |- File list:        /var/log/vsftpd.log
|  |- Currently failed: 0
|  `- Total failed:     0
`- action
   |- Currently banned: 0
   |  `- IP list:
   `- Total banned:     0

在防火牆規則上的規則
# iptables -t filter -L fail2ban-vsftpd -n
Chain fail2ban-vsftpd (1 references)
target     prot opt source               destination
RETURN     all  —  0.0.0.0/0            0.0.0.0/0

執行 Script 時檢查是否為 root

Shell Script
$ cat shell/CheckRootUser.sh
#!/bin/bash
if [ $UID -ne 0 ]; then
   echo Non root user. Please run as root.
else
   echo “Root user”
fi

改變檔案執行權限
$ chmod 700 shell/CheckRootUser.sh

執行
$ ./shell/CheckRootUser.sh
Non root user. Please run as root.

目前使用者的 UID
$ echo $UID
1000

Cubie Truck 測試 – Lubuntu Desktop 中文化及設定

Cubie Truck Lubuntu Desktop 中文化的步驟和 Banana Pi 的步驟都是一樣的。
介面是英文的
[@more@]
安裝文泉驛中文字形
$ sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

設定中文環境
$ sudo locale-gen zh_TW.UTF-8
$ sudo update-locale LANG=zh_TW.UTF-8
$ sudo update-locale LANGUAGE=zh_TW.UTF-8
$ cp /etc/default/locale $HOME/.pam_environment

修正時區
$ date
Tue Oct  7 14:29:31 UTC 2014
執行時區設定
$ sudo dpkg-reconfigure tzdata
選擇 Asia

選擇 Taipei

$ date
Tue Oct  7 22:31:25 CST 2014

安裝中文輸入法架構 gcin
$ sudo apt-get install gcin

gcin 的一些設定
$ cd /etc/X11/xinit/xinput.d/
$ ls -l
lrwxrwxrwx 1 root root  32 Oct  7 22:32 all_ALL -> /etc/alternatives/xinput-all_ALL
-rw-r–r– 1 root root 185 Apr  7  2012 gcin
lrwxrwxrwx 1 root root  30 Oct  7 22:32 ja_JP -> /etc/alternatives/xinput-ja_JP
lrwxrwxrwx 1 root root  30 Oct  7 22:32 zh_CN -> /etc/alternatives/xinput-zh_CN
lrwxrwxrwx 1 root root  30 Oct  7 22:32 zh_TW -> /etc/alternatives/xinput-zh_TW

$ cat /etc/alternatives/xinput-zh_TW
XIM=gcin
XIM_PROGRAM=/usr/bin/gcin
XIM_ARGS=
GTK_IM_MODULE=gcin
if [ -e /usr/lib/qt4/plugins/inputmethods/im-gcin.so ]; then
    QT_IM_MODULE=gcin
else
    QT_IM_MODULE=xim
fi
DEPENDS=

安裝 Firefox 及 Chromium 中文語系
$ sudo apt-get install firefox firefox-locale-zh-hant chromium-browser-l10n

重新啟動系統後,就中文了!

個人感覺,Cubie Truck  Lubuntu Desktop 中文顯示部分看起來很舒服,是我用過這幾個板子中(Raspberry Pi / Banana Pi),預設不用調整看起來就覺得很不錯的,只是它的系統版本實在是有些舊 Ubuntu 12.10。

另外還有一個問題是,Cubie Truck 不像 Banana Pi 一樣,系統關機之後,電源似乎不會自動關掉,板子上還是會閃著燈。