使用雙重指標標示數個字串的位址:
char *a="test1";
char *b="test2";
char **pt=(char**)malloc(sizeof(char*)*2);//宣告array of pointer,每個pointer會用來標示一個字串位址
pt[0]=a;
pt[1]=b;
printf("%s\n%s\n",pt[0],pt[1]);
使用雙重指標存放多個字串
char **pt=(char**)malloc(sizeof(char*)*2);//array of pointer
for(i=0;i<2;i++)
{
*(pt+i)=(char*)malloc(sizeof(char)*256);
}
strcpy(*pt,"test1");
strcpy(*(pt+1),"test2");
printf("%s\n%s\n",pt[0],pt[1]);
傳入一個空的雙重指標到func內,在其內初始化並填入字串
void store_data(char ***pt)//需多加一個星號,才能控制外部**pt內含的值
{
int i=0;
printf("%x\n",pt);//double pointer's address
*pt=(char**)malloc(sizeof(char*)*2);
for(i=0;i<2;i++)
{
*(*pt+i)=(char*)malloc(sizeof(char)*256);
}
strcpy(**pt,"test1");
strcpy(*(*pt+1),"test2");
return;
}
int main()
{
int i=0;
char **pt=NULL;
printf("%x\n",&pt);//double pointer's address
store_data(&pt);
printf("%s\n%s\n",pt[0],pt[1]);
return 0;
}
2018年2月2日 星期五
2018年1月30日 星期二
ubuntu 安裝特定版本package
有兩種方法
第一種)查詢所有ubuntu版本可裝的版本package
sudo apt install devscripts
第一種)查詢所有ubuntu版本可裝的版本package
sudo apt install devscripts
rmadison gcc(ex:查詢gcc可用版本)
範例輸出:
gcc | 4:4.6.3-1ubuntu5 | precise | amd64, armel, armhf, i386, powerpc
gcc | 4:4.8.2-1ubuntu6 | trusty | amd64, arm64, armhf, i386, powerpc, ppc64el
gcc | 4:5.3.1-1ubuntu1 | xenial | amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
gcc | 4:6.3.0-2ubuntu1 | zesty | amd64, arm64, armhf, i386, ppc64el, s390x
gcc | 4:7.2.0-1ubuntu1 | artful | amd64, arm64, armhf, i386, ppc64el, s390x
gcc | 4:7.2.0-1ubuntu1 | bionic | amd64, arm64, armhf, i386, ppc64el, s390x
範例輸出:
gcc | 4:4.6.3-1ubuntu5 | precise | amd64, armel, armhf, i386, powerpc
gcc | 4:4.8.2-1ubuntu6 | trusty | amd64, arm64, armhf, i386, powerpc, ppc64el
gcc | 4:5.3.1-1ubuntu1 | xenial | amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
gcc | 4:6.3.0-2ubuntu1 | zesty | amd64, arm64, armhf, i386, ppc64el, s390x
gcc | 4:7.2.0-1ubuntu1 | artful | amd64, arm64, armhf, i386, ppc64el, s390x
gcc | 4:7.2.0-1ubuntu1 | bionic | amd64, arm64, armhf, i386, ppc64el, s390x
第二種)查詢目前系統版本可裝的版本package
sudo apt-cache madison gcc(ex: 查詢可裝的gcc版本)
範例輸出:
gcc | 4:5.3.1-1ubuntu1 | http://us.archive.ubuntu.com/ubuntu xenial/main amd64 Packages
gcc | 4:4.8.2-1ubuntu6 | http://dk.archive.ubuntu.com/ubuntu trusty/main amd64 Packages
2017年12月28日 星期四
2017年12月26日 星期二
FreeBSD nfsv4設定
分為server(192.168.140.128)與client(192.168.140)端
於最下方加入
nfs_server_enable="YES"
nfsv4_server_enable="YES"
mountd_enable="YES"
nfsuserd_enable="YES"
編輯/etc/exports(需自行新增該檔案)
編寫範例如下:
/net
/net/admin -mapall=user
V4: / -sec=sys -network 192.168.140 -mask 255.255.255.0
設定zfs share
zfs set sharenfs=off zroot/net/data
zfs set sharenfs='-mapall=1000:1000 -network 192.168.140.130' zroot/net/admin
啟動nfs server服務
service nfsd restart
重新開機
restart -r now
server端設定:
編輯/etc/rc.conf於最下方加入
nfs_server_enable="YES"
nfsv4_server_enable="YES"
mountd_enable="YES"
nfsuserd_enable="YES"
編輯/etc/exports(需自行新增該檔案)
編寫範例如下:
/net
/net/admin -mapall=user
V4: / -sec=sys -network 192.168.140 -mask 255.255.255.0
zfs set sharenfs=off zroot/net/data
zfs set sharenfs='-mapall=1000:1000 -network 192.168.140.130' zroot/net/admin
啟動nfs server服務
service nfsd restart
重新開機
restart -r now
client端設定:
編輯/etc/rc.conf
於最下方加入
nfsuserd_enable="YES"
nfscbd_enable="YES"
啟動nfs client服務
service nfsuserd start
重新開機
restart -r now
mount -t nfs -o nfsv4 192.168.140.128:/net/admin /net/admin
重新開機
restart -r now
mount -t nfs -o nfsv4 192.168.140.128:/net/admin /net/admin
2017年12月25日 星期一
FreeBSD ZFS 筆記
檢視資料集列表:
zfs list
建立資料集:
zfs create -o mountpoint=/net zroot/net
刪除資料集:
zfs destroy zroot/net
快照資料集:
zfs snapshot zroot/net@snap_name
檢視快照資料集列表:
zfs list -t snapshot
還原資料集:
zfs rollback zroot/net@snap_name
zfs list
建立資料集:
zfs create -o mountpoint=/net zroot/net
刪除資料集:
zfs destroy zroot/net
快照資料集:
zfs snapshot zroot/net@snap_name
檢視快照資料集列表:
zfs list -t snapshot
還原資料集:
zfs rollback zroot/net@snap_name
2017年12月24日 星期日
FreeBSD NIS設定
場景為三台server分別為sahome(NIS master xxx.xxx.xxx.1), saduty(NIS slave xxx.xxx.xxx.2), sabsd(NIS client xxx.xxx.xxx.3)
修改
hostname="sahome.com"
並加入
nisdomainname="test-domain"
nis_server_enable="YES"
nis_yppasswdd_enable="YES"
nis_yppasswdd_flags="-t /var/yp/master.passwd"
rpcbind_enable="YES"
nis_client_enable="YES"
編輯/etc/hosts
再最下方加入
xxx.xxx.xxx.1 sahome.com
xxx.xxx.xxx.2 saduty.com
xxx.xxx.xxx.3 sabsd.com
編輯/var/yp/Makefile
將NOPUSH="True"註解掉(前面加#)
cp /etc/master.passwd /var/yp/master.passwd
根據需要需求編輯/var/yp/master.passwd
此檔案內紀錄的user資訊,之後會分享到各個NIS client下作為登入驗證檔案使用(此場景即為sabsd server)
ypinit -m test-domain
依照提示設定:
設定sahome server:
編輯/etc/rc.conf修改
hostname="sahome.com"
並加入
nisdomainname="test-domain"
nis_server_enable="YES"
nis_yppasswdd_enable="YES"
nis_yppasswdd_flags="-t /var/yp/master.passwd"
rpcbind_enable="YES"
nis_client_enable="YES"
編輯/etc/hosts
再最下方加入
xxx.xxx.xxx.1 sahome.com
xxx.xxx.xxx.2 saduty.com
xxx.xxx.xxx.3 sabsd.com
編輯/var/yp/Makefile
將NOPUSH="True"註解掉(前面加#)
cp /etc/master.passwd /var/yp/master.passwd
根據需要需求編輯/var/yp/master.passwd
此檔案內紀錄的user資訊,之後會分享到各個NIS client下作為登入驗證檔案使用(此場景即為sabsd server)
ypinit -m test-domain
依照提示設定:
non-fatal errors? [y/n: n] n
next host to add: saduty.com
next host to add: ^D
Is this correct? [y/n: y] y
等待設定完成即可
設定saduty server:
編輯/etc/rc.conf
修改
hostname="sahome.com"
並加入
nisdomainname="test-domain"
nis_server_enable="YES"
nis_client_enable="YES"
編輯/etc/hosts
再最下方加入
xxx.xxx.xxx.1 sahome.com
xxx.xxx.xxx.3 sabsd.com
ypinit -s sahome.com test-domain
依照提示設定:
修改
hostname="sahome.com"
並加入
nisdomainname="test-domain"
nis_server_enable="YES"
nis_client_enable="YES"
編輯/etc/hosts
再最下方加入
xxx.xxx.xxx.1 sahome.com
xxx.xxx.xxx.3 sabsd.com
ypinit -s sahome.com test-domain
依照提示設定:
non-fatal errors? [y/n: n] n
等待設定完成即可
設定sabsd server:
編輯/etc/rc.conf修改hostname="sahome.com"並加入nisdomainname="test-domain"nis_client_enable="YES"編輯/etc/hosts再最下方加入xxx.xxx.xxx.1 sahome.comxxx.xxx.xxx.2 saduty.com編輯/etc/master.passwd (輸入vipw指令)再最下方加入+:::::::::編輯/etc/group (輸入vigr指令)再最下方加入+:*::編輯/etc/nsswitch.conf修改為hosts: files nis dns進入該sabsd server後shutdown -r now重新開機
輸入ypcat passwd即可看到sahome中,由/var/yp/master.passwd產生出/var/yp/passwd其內的資訊,
用以作為登入sabsd server
2017年12月4日 星期一
FreeBSD 安裝與設定apache+php 作業筆記
安裝apache
cd /usr/ports/www/apache24下 make all設定並編譯,最後make install安裝完後,開始設定:
使用 ip 和 Domain 瀏覽網站根目錄會看到不同內容
編輯/usr/local/etc/apache24/httpd.conf
ServerName 改為自己的domain name
預設有一個Directory structure,這個設為使用者用Domain方式進入的Directory
DocumentRoot "/usr/local/www/apache24/data"
<Directory "/usr/local/www2/apache24/data">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
多新增一個Directory structure,這個設為使用者用IP方式進入的Directory
DocumentRoot "/usr/local/www/apache24/data"
<Directory "/usr/local/www2/apache24/data">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
將此行的註解拿掉
Include etc/apache24/extra/httpd-vhosts.conf
接著設定 /usr/local/etc/apache24/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/www/apache24/data"
ServerName your.domain.name
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/usr/local/www2/apache24/data"
ServerName xxx.xxx.xxx.xxx
</VirtualHost>
service apache start 啟動apache
此時在瀏覽器上分別輸入自己server的Domain name和ip瀏覽,應會看到不同資料夾下的html
啟用HTTPS
申請SSL憑證,這邊我是到SSL FOR FREE申請,點Manual Verification照他的官方說明,下載他為你網站產生的憑證檔,其中包含ca_bundle.crt, certificate.crt, private.key
將它們放在/usr/local/etc/apache24/ssl資料夾下,並編輯/usr/local/etc/apache24/httpd.conf
以下幾行取消註解
LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so
LoadModule ssl_module libexec/apache24/mod_ssl.so
Include etc/apache24/extra/httpd-ssl.conf
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
編輯/usr/local/etc/apache24/extra/httpd-ssl.conf
確認VirtualHost 標籤下有以下設定
<VirtualHost _default_:443>
DocumentRoot "/usr/local/www/apache24/data"
ServerName your.domain.name:443
ServerAdmin you@example.com
SSLEngine on
SSLCertificateFile "/usr/local/etc/apache24/ssl/certificate.crt"(your file path)
SSLCertificateKeyFile "/usr/local/etc/apache24/ssl/private.key"(your file path)
SSLCACertificateFile "/usr/local/etc/apache24/ssl/ca_bundle.crt"(your file path)
service apache24 restart
在瀏覽器輸入https://自己的網址/index.html 理應可以連上
HTTP rediect to HTTPS
編輯/usr/local/etc/apache24/extra/httpd-vhosts.conf
在VirtualHost結構下加入Redirect 設定如下:
<VirtualHost *:80>
DocumentRoot "/usr/local/www/apache24/data"
ServerName your.domain.name
Redirect / https://your.domain.name/
</VirtualHost>
service apache24 restart
在瀏覽器輸入http://your.domain.name/index.html ,會自動導至https://your.domain.name/index.html
啟用HSTS
編輯/usr/local/etc/apache24/extra/httpd-vhosts.conf與/usr/local/etc/apache24/extra/httpd-ssl.conf
在VirtualHost結構下加入Redirect 設定如下:
<VirtualHost *:80>
DocumentRoot "/usr/local/www/apache24/data"
ServerName your.domain.name
Redirect / https://your.domain.name/
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
</VirtualHost>
service apache24 restart
curl -Ik your.domain.name,應為出現strict-transport-security: max-age=63072000; includeSubdomains; preload
啟用HTTP2
編輯/usr/local/etc/apache24/httpd.conf
設定
LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so
註解掉
LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so
在檔案最底端加入
<IfModule http2_module>
LogLevel http2:info
Protocols h2 h2c http/1.1
H2Direct on
</IfModule>
service apache24 restart
curl --http2 -I your.domain.name,應會看到改連線改為HTTP/2
也可利用此網站https://tools.keycdn.com/http2-test
測試自己的網站是否已支援HTTP/2
也可利用此網站https://tools.keycdn.com/http2-test
測試自己的網站是否已支援HTTP/2
設定Rewrite
編輯/usr/local/etc/apache24/httpd.conf
取消註解此行
LoadModule rewrite_module libexec/apache24/mod_rewrite.so
/usr/local/etc/apache24/extra/httpd-ssl.conf
在VirtualHost結構下加入Redirect 設定如下:
RewriteEngine on
RewriteRule /home/([a-zA-Z0-9]*)/(.*)$ http://your.domain.name/~$1/$2 [L]
安裝php
cd /usr/ports/lang/php70
make all設定並編譯,最後make install
編輯/etc/rc.conf
新增php_fpm_enable="YES"
編輯/etc/rc.conf
新增php_fpm_enable="YES"
到/usr/local/etc/下將php.ini-production複製一份命名為php.ini,作為php讀取初始化設定的設定檔
編輯php.ini,將short_open_tag 改為On
short_open_tag = On
編輯php.ini,將short_open_tag 改為On
short_open_tag = On
在/usr/local/etc/apache24/Includes/ 新增php-fpm.conf並編輯
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
SetHandler application/x-httpd-php
</FilesMatch>
編輯/usr/local/etc/apache24/extra/httpd-ssl.conf
在virtualhost結構下新增
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/www/apache24/data/$1
(讓apache 可請求正在監聽 port 9000 的 php-fpm 執行 php 檔)
編輯/usr/local/etc/apache24/httpd.conf
將必要的proxy_xx取消註解
在www目錄下新增index.php用來測試php是否設定成功
檔案內容:
<?php
phpinfo();
?>
service php-fpm restart
service apache24 restart
這時在瀏覽器上輸入自己的網址/index.php,就會看到php資訊頁面
根據想裝的module到對應的資料夾下
例如:
欲安裝php70 opernssl module,該module在/usr/ports/security/php70-openssl/下
cd /usr/ports/security/php70-openssl/
make deinstall
make clean
make all install
service php-fpm restart
在重啟時若無顯示錯誤訊息,及代表安裝成功
也可用利用phpinfo()或php -m 確認是否有安裝成功
安裝php module
find /usr/ports -type d -iname "php70-*" 列出所有php70 module所在資料夾,根據想裝的module到對應的資料夾下
例如:
欲安裝php70 opernssl module,該module在/usr/ports/security/php70-openssl/下
cd /usr/ports/security/php70-openssl/
make deinstall
make clean
make all install
service php-fpm restart
在重啟時若無顯示錯誤訊息,及代表安裝成功
也可用利用phpinfo()或php -m 確認是否有安裝成功
Fake server token and hide php info
使用curl -Ik my.server.name 確認目前Server token
應為apache名稱+版本+php資訊
隱藏php資訊:
編輯 /usr/local/etc/php.ini,將expose_php改為Off
expose_php = Off
偽裝server token:
cd /usr/ports/www/mod_sercurity
make all,並make install
此時/usr/local/etc/apache24/modules.d下會多出一名為280_mod_security.conf的檔案,編輯該檔
將檔案最下方的三行設定取消註解
LoadModule unique_id_module libexec/apache24/mod_unique_id.so
LoadModule security2_module libexec/apache24/mod_security2.so
Include /usr/local/etc/modsecurity/*.conf
編輯 /usr/local/etc/apache/httpd.conf,在最下方加入:
ServerTokens Full
SecServerSignature fake_server_token (ex:Microsoft-IIS/6.0)
service apache24 restart
此時再用curl -Ik my.server.name 確認Server token
理應為fake server token,且沒有php相關資訊
安裝mysql
安裝phpMyAdmin
先安裝composer,可到官網依照提示指令安裝https://getcomposer.org/download/
若過程中出現缺少php extension錯誤訊息,須另行安裝對應php module
composer create-project phpmyadmin/phpmyadmin
將裝好的phpmyadmin 複製到/usr/loca/www/下並命名為phpMyAdmin
編輯/usr/local/etc/apache/httpd.conf
Alias /phpmyadmin/ "/usr/local/www/phpMyAdmin/"
將裝好的phpmyadmin 複製到/usr/loca/www/下並命名為phpMyAdmin
編輯/usr/local/etc/apache/httpd.conf
Alias /phpmyadmin/ "/usr/local/www/phpMyAdmin/"
<Directory "/usr/local/www/phpMyAdmin/">
Options Indexes FollowSymLinks MultiViews
DirectoryIndex index.php
AllowOverride all
Require all granted
</Directory>
編輯/usr/local/etc/apache24/extra/httpd-ssl.conf
在virtual host結構下新增
ProxyPassMatch ^/phpmyadmin/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/www/phpMyAdmin/$1
service php-fpm restart
service apache24 restart
在瀏覽器輸入https://yourdomain.name/phpmyadmin/
應會看到phpmyadmin登入畫面
訂閱:
文章 (Atom)




