I have spent a lot of time to find out how to create a swap file in my Ubuntu 18.04
However, it is still fail until I found the following article.
Swapon failed: Invalid argument on a Linux system with Btrfs filesystem
There is a limitation in Btrfs filesystem.
Why did I choose Btrfs system, that is because I can use snapshot in my NAS only when I choose Btrfs for my QNAP's filesystem.
Anyway, I can use a swapfile in my Ubuntu with Btrfs filesystem.
please reference the following article.
Swap
Reference:
Swapon failed: Invalid argument on a Linux system with Btrfs filesystem
Swap
2020年4月12日 星期日
create a swap file in Btrfs filesystem
2020年3月8日 星期日
open the firewall in Ubuntu
開啟防火牆
sudo ufw enable允許MySQL連線
sudo ufw allow mysql允許SSH連線
sudo ufw allow ssh或
sudo ufw allow 22
參考資料:
UFW Essentials: Common Firewall Rules and Commands
Install MySQL Server on the Ubuntu operating system
安裝light Ubuntu environment package
最近要安裝MySQL,
選擇了在QNAP上的virtual station安裝Ubuntu,
並且在上面安裝MySQL,
為了節省資源,
打算安裝輕量的work environment,
首選就是 Xfce
可以參考下面的資料:
The 8 Best Ubuntu Desktop Environments (18.04 Bionic Beaver Linux)
Install Xfce desktop on Ubuntu 18.04 Bionic Beaver Linux
2015年5月11日 星期一
ip6tables
ip6tables跟iptables極為相似~
而ip6tables是專門在處理IPv6的封包所使用的。
這裡是收集使用過的ip6tables指令
ip6tables -t filter -L -v
顯示filter table的「詳細」設定
因為有加入-v
因此會顯示較為詳細的資料
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
15 3579 ACCEPT all any any anywhere anywhere state RELATED,ESTABLISHED
38 2736 ACCEPT icmpv6 any any anywhere anywhere ipv6-icmp neighbour-solicitation
45 3104 ACCEPT icmpv6 any any anywhere anywhere ipv6-icmp neighbour-advertisement
175 22368 ACCEPT icmpv6 any any anywhere anywhere ipv6-icmp router-advertisement
2 112 ACCEPT icmpv6 any any anywhere anywhere ipv6-icmp router-solicitation
6 958 ACCEPT udp any any anywhere anywhere udp dpt:546
2 316 ACCEPT all br0 any anywhere anywhere
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1079 493K ACCEPT all any any anywhere anywhere state RELATED,ESTABLISHED
371 38824 ACCEPT all br0 any anywhere anywhere
0 0 ACCEPT all eth2.2 map0 anywhere anywhere
Chain OUTPUT (policy ACCEPT 278 packets, 30774 bytes)
pkts bytes target prot opt in out source destination而流程圖如下

而實際的rule則是找到一個符合的,就會去執行相對應的動作,剩下的rule就不會去管了。
參考資料:
Iptables 指南 1.1.19
Chapter 18. Firewalling
iptables 設定入門
iptables 的用法
iptable使用笔记
2015年2月12日 星期四
送IPv6的RS(Router Solicitation)到指定的interface
IPv6的網路中,
client端會依據RA(Router Advertisement)的封包來設定IPv6 address.
詳情可以參考第十篇 IPv6 Path MTU及Router Advertisement
而RA的發送有兩個時機點,
1. 固定時間發送RA
2. 當收到client端送出RS(Router Solicitation)時,立刻以RA封包回應
現在遇到的問題是,router的發送RA的間隔設定很長的話,而網路又沒有其它client端送出RS封包,如此一來,剛接上去的client端網路要很久後才會被設定,這個是使用者所不能接受的。
因此,現在要嘗試來寫一個user mode的執行檔, 可以讓我針對指定的interface送出RS封包,
在參考busybox的ping6與radvd的source code後,總算完成了。
參考了IPv6 Essentials這本書, 裡面有講到一段話, 非常清楚的說明了RS的條件
「In the IP header of a Router Solicitation message, you will usually see the all-routers multicast address of ff02::2 as a Destination address. The hop limit is set to 255. The ICMP Type field is set to 133, which is the value for the Router Solicitation message. The Code field is unused and set to 0. The following two bytes are used for the Checksum. The next four bytes are unused and reserved for future use. The sender sets them to 0, and the receiver ignores those fields. For a Router Solicitation message, a valid option is the link-layer address of the sending host, if the address of the sending host is known. If the Source address on the IP layer is the unspecified (all-zeros) address, this field is not used. More options may bedefined in future versions of ND. If a host cannot recognize an option, it should ignore the option and process the packet.
Routers that receive this Solicitation message reply with a Router Advertisement message. Routers also issue those messages periodically.」
參考radvd裡面的send_ra
把其改寫成send_rs
但是,結果確把RS封包往LAN port送, 而我是想往WAN port送,
這個時候想到ping6可以指定傳送的介面,例:
# ping6 ff02::2 -I eth2.2
在改寫完後,可以確實的把RS往指定的interface送, 但是,router確不會回傳RA.
在比對完可以正確回傳RA的RS封包與我自己傳送的RS封包後, 跟上面那一段文字比對後,發現有可能是Hop limit我設定為1,
而正常的必需要是255
而這個部分必需要用setsockopt來完成~
int sockopt;
sockopt = 255;
setsockopt(pingsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &sockopt, sizeof(sockopt));
因為我們要送的是multi-cast的封包, 因為設定的Hop limit也是for multi-cast的,必需使用IPV6_MULTICAST_HOPS
uni-cast則是使用IPV6_UNICAST_HOPS
接下來則是sample code了
這中間使用到if_nametoindex→把eth2.2轉成index
getopt32(argc, argv, "qvc:s:I:", &opt_c, &opt_s, &opt_I);
會連動到option_mask32這一個變數,
當有發現到q則option_mask32的bit 0會被舉起來
當有發現到c的option時, 則option_mask32的bit 2會被舉起來
因-c後面要接參數, 所以opt_c接的是第一個參數
2014年12月22日 星期一
2014年12月9日 星期二
get ip address by DNS
目前要解析DNS去解析IP,
但是gethostbyname並不支援IPv6,
但有找到getaddrinfo可以達到同樣的功能,
而且同時有支援IPv4與IPv6
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);/*error*/
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);/*error*/
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}參考資料:
Sample getaddrinfo Code Compiles in Linux but not FreeBSD
getaddrinfo(3) - Linux man page
gethostbyname(), gethostbyaddr()
Porting IPv4 applications to IPv6
Porting IPv6 -- examples
2014年11月20日 星期四
linux signal相關設定
目前在寫daemon,
寫到了處理signal的程式
signal (SIGHUP, sighup_handler);
表示當這一個process收到一個SIGHUP時,
sighup_handler這一個函式會被執行。
SIGHUP
一般是用在參考檔案更新時。
可以直接透過下指令去驗証此function是否正常
kill -HUP 3077
killall -HUP a.out
SIGTERM, SIGINT
參考資料:
Sending signal to Processes
signal 小知識
Daemon函数的用法
寫 linux daemon 的注意事項
2014年11月17日 星期一
建立samba
因為compiler環境在linux,
而產生出來的image檔案必需要傳到windows上做燒錄的動作。
找了一下,有幾個方式可以完成,
FTP
WEB
網路芳鄰
目前看起來最簡單的方式應該是網路芳鄰samba
在多天的嘗試後,可以正常的使用帳號的方式連線到linux的空間,
可惜的是還沒有辦法試成功可以用匿名的方試登入(Guest)
========== 開始 ==========
0. 查詢是否有安裝samba
rpm -q samba
1. 查詢防火牆的狀態
systemctl status firewalld
2. 關掉防火牆
systemctl disable firewalld
systemctl stop firewalld
3. 新增samba使用者
smbpasswd -a caspar
若遇到以下錯誤訊息,表示系統沒有相對應的帳號。
目前的設置是linux系統有的帳號,samba才可以追加其帳號。
4. 停用samba使用者
smbpasswd -d caspar
5. 刪除samba使用者
smbpasswd -x caspar
6. 啟用samba服務
service smb start
7. 重新啟動samba服務
service smb restart
8. 查詢目前samba的狀態
service smb status
基本上新增samba使用者後,就可以由windows進入,但是,目前成功的方試,仍然要使用帳號跟密碼。
smbpasswd這一個指令可以做一些簡單的使用者帳號的操作,
但是更深入的操作方式就必需要使用pdbedit這一個指令
========== pdbedit 指令說明 ==========
pdbedit -L
列出使用者列表
pdbedit -Lv
列出詳細的使用者列表
pdbedit -Lw
列出同 smbpasswd 格式的使用者列表
pdbedit -a jerry
新增 jerry帳號
pdbedit -x jerry
刪除 jerry帳號
pdbedit -c "[D]" -u jerry
暫時停用 jerry 這個帳號
pdbedit -c "[]" -u jerry
恢復使用 jerry 這個帳號
========== 其它資訊 ==========
linux上登入window或是samba的電腦
smbclient //主機/分享的資料匣資源 -U 使用者名稱
輸入密碼就可以執行類似ftp的指令了
mount 別台機器分享的資源到本台電腦
mount -t smbfs -o username=使用者名稱 ,password=密碼 //主機名稱/分享的資源 /主機目錄
查詢使用的port
netstat -anp
查詢目前執行的程式
ps aux | grep mbd
在設定smb.conf時, 若嘗試要設定Guest登入
設定security為share
會出現以下錯誤
WARNING: Ignoring invalid value 'share' for parameter 'security'
改為以下就可以正常的
security = user
map to guest = Bad User
========== 自己安裝 samba-4.1.13 ==========
./configure
會出現以下錯誤訊息
Could not find the python development headers
安裝以下套解可以解除問題
yum install libacl-devel libblkid-devel gnutls-devel readline-devel python-devel gdb pkgconfig
參考資料:
如何檢查linux中的服務是否正常?
Samba 設定USER等級分享目錄[3]
Samba 下 Ubuntu與Win7共享資料夾設定,利用 host-only網路
Samba-4.1.13
安裝samba系統
fedora 10/11 安裝samba || windows 與 linux 的網芳設定
第十六章、檔案伺服器之二: SAMBA 伺服器
2014年11月3日 星期一
2014年10月30日 星期四
linux script
1. 判斷檔案是否存在
#!/bin/bash
file="/etc/hosts"
if [ -f "$file" ]
then
echo "$file found."
else
echo "$file not found."
fi -b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted參考資料:
Bash Shell: Check File Exists or Not
2014年10月23日 星期四
預設不啟動x window
為了節省資源,
在fedora可以預設把x window關掉。
打以下指令
# mv /etc/systemd/system/default.target /etc/systemd/system/default.target.bak
# ln -s /lib/systemd/system/runlevel2.target /etc/systemd/system/default.target
這樣下一次開機就不會啟動x window了
不過,若要啟動的話,
只要打
# startx
指令即可
參考資料:
Fedora 下设置系统不启动X window
Linux 的開機啟動流程
2014年10月22日 星期三
putty藍色字體看不清楚的問題
用putty連到linux server會出現藍色字體看不清楚的問題。
原本的顏色

選擇Colours的ANSI Blue,並且設定為以下數值
Red: 56
Green: 109
Blue: 199
這樣比較可以看的清楚了。

參考資料:
在putty 中设置字体的颜色和背景
2014年9月1日 星期一
linux下停用IPv6
直接寫入1到
/proc/sys/net/ipv6/conf/all/disable_ipv6
就可以關掉整個IPv6的支援。
可以填到
/usr/etc/rcS裡面寫入
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
其它方式可以參考
Linux 下關閉支援 IPv6 提升效能
2014年6月4日 星期三
pipe的用法
使用 pipe() 可建立一組雙向的管線,
通常會使用一個陣列來建立pipe
例:
int pfd[2];
這是一條水管,
從 pfd[0] 進入的東西會從 pfd[1] 出來,
反之亦然,
除此之外,
它也是可以跨 Process,
達成兩個程式溝通的目的。
實做上必須注意以下幾個重點
1.有複製,就要有關閉,一個dup2搭一個close,保持file descriptor的單純性
(此為血淚經驗談,自己實做就知道…)
2.fork出去的程式,沒用到的file descriptor一定要記得關
舉例來說,本來shell主程式裡面有一個pipe,若是此時fork出子程序來exec指令,主程序下wait指令等待子程序執行完畢且主程序沒有關閉pipe,則主程序會卡住。因為當fork的時候,一個pipe實際上會變成兩個pipe,當指派子程序接收pipe資料的時候,如果還有其他pipe沒有關閉,則子程序會判定pipe資料流尚未結束,而繼續癡癡等待輸入,若是主程序有pipe沒關閉的話,結果就是主程序在等子程序結束,子程序又在等待主程序的pipe輸入資料,造成死結。
參考:
C/C++, Linux, Web 相關技術, 程式心得筆記, 系統研發手札
pipe @ Linux Kernel
[程設] Linux C 的file descriptor以及pipe操作相關筆記(上)
[程設] Linux C 的file descriptor以及pipe操作相關筆記(下)
2014年5月21日 星期三
Linux C - gcc 預先定義的巨集
在Linux下使用gcc編譯,
有幾個內建的變數可以非常好用,
在Debug的時候,
占非常重要的角色。
1. __BASE_FILE__
完整的原始檔案路徑
2. __cplusplus
表示該檔案由 g++ 所編譯,當成 C++ 的檔案
3. __DATE__
編譯的日期
4. __TIME__
編譯的時間
5. __FILE__
原始檔名
6. __LINE__
所在行數
7. __VERSION__
gcc 版本
8. __func__
替代 __FUNCTION__,__FUNCTION__ 已被 GNU 不推薦使用
參考資料:
LinuxC – gcc 預先定義的巨集