2016/04/19
二進位的負數如何表示
這是一個二進位的小問題. 在電腦的世界裡, 使用二進位表示法來存放數字是再單純不過的.
不過數字為能有更佳的效率, 有一些設計是很重要的, 例如用二的補數來表示負數, 以下為一個例子.
假設用一個 byte 來代表數字的話, 則共有8個bit可用, 最高字元保留給符號,
而數字就用二進位法來放, 所以8個bit表示如下:
7-6-5-4-3-2-1-0
其中第0個bit最右側, 也就是最低位元, 最左邊是第7個bit, 也就是最高位元.
接下來要說明數字囉
00000001 (b)=1(d)
00000010(b)=2(d)
00000100(b)=4(d)
00010101(b)=21(d)
…. 以此類推, 而最大正數可以用這個表示, 除了第7個bit不動, 其餘都是1的話:
01111111(b)=127(d)
好, 接下來看負數怎麼表示. 先來看所謂二的補數,
二的補數(two’s complement)就是將位元反置後再加一, 來看一個例子:
25(d)=00011001(b)
而要代表-25的二進位數, 則用 00011001(b)反置後再加一
1. 反置 11100110
2. 再加1 得 11100111,
3. 所以得 11100111(b)=-25(d)
再舉一個例子, 2(d)=00000010(b),
1. 反置 11111101,
2. 再加1得 11111110,
3. 所以得11111110(b)=-2(d)
接下來看如何反過來做, 若今天有個值是 110001010(b) 怎麼算回來呢?
1. 先減1, 110001001
2. 再反置得 001110110
3. 所以得 001110110(b)=118(d)
4. 所以 110001010(b)=-118(d)
這樣就行囉!
所以 8個bit的範圍最小到最大就是 -128~127共256個數字.
-----
轉載自 : http://tim.diary.tw/2009/08/24/twos-complement/
2010/04/15
Pietty (Putty) 問題彙整
- 連線到 CentOS (Linux) ,使用 setup 系統設定指令時,排版會亂掉 (非文字亂碼)
A :
請到 "選項" -> "詳細設定" -> "Window" -> "Translation" -> "Treat CJK ambiguous characters as full width" 取消打勾
參考 : CJK characters 的 wiki 解釋
2009/08/06
MySQL 的匯入匯出, 使用 mysqldump
如果可以直接在 console 介面下操作 mysql, 使用 mysqldump 是最好的備份方法
假設我們要備份的資料庫名稱是 db_name , 帳號是 username
直接打入 :
2009/08/04
putty + FreeBSD 免輸入密碼登入系統,實戰教學
經過多次的 try error 終於給我找到一個可以用的教學文件
原文出處 : 行政院國家資通安全會報-技術服務中心-資安論壇
這裡只不過是轉載原文
至於 ssh 連線的運作邏輯,可以參考名人鳥哥的網站 SSH連線加密技術簡介
礙於篇幅這裡就不貼內容了
========================================================================
以下為教學原文內容
========================================================================
How to use Authorized Key by ssh for login?
如何使用公鑰/私鑰登入 Linux 系統?
====[Root篇---開始]=====================================================
以下的 config 檔,為 FC4 的 /etc/ssh/sshd_config 的內容
--------------------------------------------------------------------------------------
# $OpenBSD: sshd_config,v 1.70 2004/12/23 23:11:00 djm Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.
Port 22
#Protocol 2,1
#Because Portocol 1 is not safe ,so I don't use, and avoid attack.
Protocol 2
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 768
# Logging
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
MaxAuthTries 3
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# ChallengeResponseAuthentication=no
#UsePAM no
UsePAM yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression yes
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10
#ShowPatchLevel no
# no default banner path
#Banner /some/path
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
---------------------------------------------------------------------------------------
以上內容為 FC4 的 /etc/ssh/sshd_config 的內容,但僅供參考,我不敢保證完全對,
所以照抄不一定 OK,所以請依您系統的實際狀況及週遭的情況去調整。
=======================================================
0.事前準備:
Puttygen.exe → http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe
Putty.exe → http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
Pietty.exe(建議使用,有支援中文輸入,SCP 檔案上傳.)
→ http://ntu.csie.org/~piaip/pietty/stable/pietty0327.exe
WinSCP3 → http://winscp.net/eng/
==============================================
1.修改 /etc/ssh/sshd_config
#在 sshd_config 裡,找到與下列相符的選項,就將選項前的 # 號拿掉:
Protocol 2
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PermitRootLogin yes
#找到 ChallengeResponseAuthentication yes 的選項,改成 no ,如下:
#(選項解釋:密碼登入選項,一定要改成 no,這樣子沒有擁有私鑰的人就會無法登入了)
# Change to no to disable s/key passwords
#修改成 no 來停用 s/key 密碼
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
#以下為非必要選項
SyslogFacility AUTHPRIV(或 SyslogFacility AUTH)
LogLevel INFO
接下來就存檔離開
重新啟動 sshd 服務.
#service sshd restart
2.在 Server 端,使用 ssh-keygen 來建立 DSA Private Key 和 Public Key.
先檢查 /root 下有沒有 .ssh 的資料夾,如果有就略過建立資料夾的步驟,
在 /root 下建立資料夾 .ssh,
#mkdir .ssh
#cd .ssh
#ssh-keygen -b 2048 -t dsa
然後,輸入 Private Key(私鑰)檔名:id_dsa,和 Public Key(公鑰)檔名:id_dsa.pub
這個時候會問你 key passphrase,
(可以自由選擇輸入與否,即使現在不輸入也沒關係,
後面再用 puttygen.exe 轉換格式時再輸入,也是可以的,
但是不可為了省卻輸入密碼,而只用 KEY 就登入 LINUX 系統,
此舉很危險,萬一 Key 檔案被有心人士偷取,就糟糕了.>"<)
再來把公鑰的內容導入到 authorized_keys
#cat id_dsa.pub > authorized_keys
3.因為 Openssh 的私鑰格式和 putty 使用的格式截然不同,
所以需要由 puttygen.exe 轉換格式後才能使用,不然可能會有兩種錯誤的情況:
--------------------------------------------------------------
可能出現的幾種問題:
(a)、Server refused our key
公鑰和私鑰不匹配,或者沒有 authorized_keys 文件
(b)、Unable to use key file "id_dsa.ppk" (SSH2 private key)
私鑰檔案的格式不正確
--------------------------------------------------------------
3.1把 server 上的私鑰:id_dsa 拷貝到 PC 上
將 id_dsa 的內容顯示在螢幕上,再複製下來.
#cat id_dsa
把複製的內容貼到記事本裡存成 id_dsa_by_ssh-keygen.ppk
再來就是開啟用 puttygen.exe → Conversions → Import Key
匯入後,在 Key passphrase 和 Confirm passphrase 輸入保護私鑰的密碼後,
(不想打密碼的人,就保留成空白也可以,不過萬一私鑰掉了被撿到,那就慘了>"<),
然後,再從 File → Save Private Key 把私鑰另存新檔即可使用了。
4.Pietty/Putty 使用方法:
啟動 Pietty/Putty ,從 SSH → Auth 去指定私鑰的檔案路徑即可。
5.強烈建議:私鑰(Private Key)一定要用 Key Passphrase 來保護,
密碼(至少 8 位數以上,每三個月更換一次公/私鑰)千萬不要跟 root 的密碼一樣.
另外,當你在 Server 上用 ssh-keygen 公/私鑰時,會詢問公/私鑰的檔名和
保護 KEY 用的 Passphrase 的密碼,在 Server 有輸入的話,在用 puttygen.exe
做轉換時也會叫你輸入,所以要小心輸入喔!
====[Root篇---結束]=====================================================
有些人也想說,我也想替我的使用者也提供類似的服務,不要再用密碼登入系統,想要用 Key 來登入,但是,
因為目前密碼的服務,可能已經關閉了,怎麼辦?
====[User篇---開始]=====================================================
0.修改 sshd 的組態檔
/etc/ssh/sshd_config 要修改一行的內容
找到 AuthorizedKeysFile .ssh/authorized_keys
改成 AuthorizedKeysFile %h/.ssh/authorized_keys
存檔離開,並重新啟動 sshd 服務
#service sshd restart
1.建立 splin 這個使用者帳號,並修改密碼.
#useradd splin
#passwd splin
#cd /home/splin/
#mkdir .ssh
#cd .ssh
#ssh-keygen -b 2048 -t dsa
以下為 ssh-keygen 執行所會發問的問題:
Generating public/private dsa key pair.
當你用 root 登入時,執行 ssh-keygen 會出現下列顯示的路徑
Enter file in which to save the key (/root/.ssh/id_dsa):
當你用 splin 登入時,執行 ssh-keygen 會出現下列顯示的路徑
Enter file in which to save the key (/home/splin/.ssh/id_dsa):
所以以上不管出現何種,如果出現的是預設的路徑(/home/splin/.ssh/id_dsa)就按 Enter 跳過,
則不用輸入→ /home/splin/.ssh/id_dsa (要含路徑和檔名),以上跟你用那個 ID 登入系統有關係。
Enter passphrase (empty for no passphrase):
輸入要保護私鑰的密碼,這裡跟系統的密碼不用一樣,而且密碼千萬是不要一樣。
再來接下來和 Root 篇的公鑰內容導入至 authorized_keys
#cat id_dsa.pub > authorized_keys
但是要確認在 /home/[users]/.ssh/ 路徑和檔名有沒有錯誤。
[users]→表示為 user 的帳號名稱。
2把 server 上的私鑰:id_dsa 拷貝到 PC 上
將 id_dsa 的內容顯示在螢幕上,再複製下來.
#cat id_dsa
把複製的內容貼到記事本裡存成 id_dsa_by_ssh-keygen.ppk
(檔名命名方法沒有一定,但是為不讓自己搞不清楚,這樣子做最好)
再來就是開啟用 puttygen.exe → Conversions → Import Key
(匯入時,因為我們在用 ssh-keygen 產生公/私鑰時,就有用密碼了,所以在你使用 puttygen.exe,
做匯入的動作時,自然會要求你輸入密碼了)
匯入後,在 Key passphrase 和 Confirm passphrase 輸入保護私鑰的密碼後,
(不想打密碼的人,就保留成空白也可以,不過萬一私鑰掉了被撿到,那就慘了>"<),
然後,再從 File → Save Private Key 把私鑰另存新檔即可使用了。
3.強烈建議:私鑰(Private Key)一定要用 Key Passphrase 來保護,
密碼(至少 8 位數以上,每三個月更換一次公/私鑰)千萬不要跟 root 的密碼一樣.
4.因為我是用 root 來替 splin 這個 user 建立公/私鑰,
所以我要把檔案權限和屬性稍微修改一下。
#chown -R splin.splin /home/splin/.ssh/
#chmod -R 755 /home/splin/.ssh/
5.Pietty/Putty 使用方法:
啟動 Pietty/Putty ,從 SSH → Auth 去指定私鑰的檔案路徑即可。
====[User篇---結束]=====================================================
PS.
1.以上有任何看不懂的,歡迎在此發問,或來信詢問皆可。
2.在您用 putty 測試私鑰是否可以使用之前,請先不要把原先的連線直接關閉,
請先測試用私鑰可以登入後,再把停用密碼登入的選項設定由 yes 改成 no.
***** Contact Email: *****
***** 本文由 splin 撰寫,歡迎轉載,但請保留本行文字*****
[教學原文結束]
========================================================================
總結
========================================================================
所有的過程可以簡述如下:
- 用 Unix server 產生 public / private keys
- 將 public key 放到該帳號的家目錄底下
- 複製 private key 內容,貼到 Windows 個人工作 PC 當中保存。
為了符合 putty 的私鑰格式,還要用 puttygen.exe 轉檔 - 修改 sshd_config 改成不用輸入密碼,並重新啟動 sshd
- putty 登入方式改成使用 private key
/etc/rc.d/sshd 不靈光?
今天一直想 try 不使用密碼的 ssh login 方法,也就是使用 public / private key 結果每次修改完 /etc/ssh/sshd_config 執行 /etc/rc.d/sshd restart 總是沒有讀取到新的設定
執行 /etc/rc.d/sshd stop 也無法停止 sshd
所以我直接用 kill 砍掉目前正在執行的 sshd process
再試著用 /etc/rc.d/sshd start 啟動,結果發現 sshd 並沒有啟動
用 "/etc/rc.d/sshd" 關鍵字去 酷!學園 搜尋答案
發現原來要在 /etc/rc.conf 中要加入一行 sshd_enable="yes" 才能使用 /etc/rc.d/sshd
去開啟 /etc/rc.conf 沒有看到這一行..... 加入了之後果然可以用了
FreeBSD 沒有幫 user 先設定好啟動 ssh 的環境
對於一開始只有接觸過 RedHat Linux 7.X 以上的人來說,果然是很不習慣
FreeBSD安裝日誌
因為工作上需要,所以不得不自行摸索 FreeBSD,大部分的基本教學可以在這裡找得到twbsd.org (台灣中文站)
以下內容就是紀錄安裝過程中遇到的問題,以及解決的方法
按照 twbsd.org 的指示,從 交大資工下載了FreeBSD 6.0 Release 共兩張 CD 的內容
(我以為 FreeBSD 應該是小巧精緻的,怎麼需要兩張CD? 算了先抓下來,以後再說)
硬碟的分割規劃沒什麼問題,除了有一些小調整
硬碟共有80GB,詳細規劃如下
| 目錄 | 大小 | 說明 |
| / | 1GB | |
| /home | 2GB | 因為架設這個 server 不是當作個人網站用的,所以 /home 我只給了 2G |
| /swap | 512MB | |
| /tmp | 1GB | |
| /usr | 15GB | 所有的網頁程式還有安裝的軟體,都放在 /usr/local 底下 |
| /var | 10GB | 資料庫會放在這裡 |
| /var/log | 剩下的空間 | 記錄檔會很龐大,所以剩下的空間都給它 |
因為一開始安裝忘了設定網路,所以在裝完了登入以後,打sysinstall進去設定。
sysinstall -> Configure -> Networking,其餘的在 twbsd.org 都寫得很詳細,請看 這裡
SSH 一開始 FreeBSD 並沒有幫你啟動,說的更明白點,它根本沒有幫你裝完整。
執行 /usr/sbin/sshd 的時候,會告訴你有錯誤。
| Could not load host key: /etc/ssh/ssh_host_dsa_key Disabling protocol version 2. Could not load host key |
如果是安裝 RedHat Linux 會幫你把這些都做好,可是FreeBSD不會,所以我只好自行搜尋答案。
從 Google 搜尋到的答案如下: (原文網址)
| 建立金鑰的3個步驟 : # usr/bin/ssh-keygen -t rsa1 -N '' -f /etc/ssh/ssh_host_key # usr/bin/ssh-keygen -t rsa -N '' -f /etc/ssh/ssh_host_rsa_key # usr/bin/ssh-keygen -t dsa -N '' -f /etc/ssh/ssh_host_dsa_key 然後執行sshd 即可 (或於rc.conf 加入 sshd_enable="YES" 後reboot) |
ssh-keygen 的參數,-t 就是 type,-f 就是 file,這兩個沒什麼好說的。
至於那個 -N,使用 man ssh-keygen 查詢的結果是 new passphrase 的意思。
passphrase 一開始我用Yahoo字典去查,結果是啥都沒有....
不得已又請出大神,從www.cdt.org找到了以下的解釋
| Passphrase - similar to a password but can be made up of any number of characters. A passphrase is generally thought to be stronger than a password, although not many programs support the use of a passphrase. |
2007/08/18
為 Fedora Core 安裝 GD 函數庫
來源: 阿杰老師的部落格
Fedor Core 本身並不會自行安裝 PHP 的 GD 函數庫!
這樣就無法透過 PHP 去建立圖檔縮圖。
請開啟 終端機 後 執行
如果沒有取得 GPG Key 請參考下面文章再執行
[linux] 如何在Fedora Core 3 中為yum安裝GPG Key
yum install php-gd
就會自動進行相關的安裝及設定
此外你可以到
/usr/lib/php4 中看到 gd.so 這個檔案
這部份的運作方式跟 windows 平台完全不同
同理如果要安裝可以分割雙位元文字的 mbstring
可以用 yum install php-mbstring
最後要重新啟動 Fedora 才會生效哦 !
Web 技術中的 Session 是什麼?
轉載原文: Web 技術中的 Session 是什麼? 本文目標 在 Web 的世界裡,Session 已經是被廣泛使用的一種技術,大多數的 Web 開發者,肯定都能運用自如。在現今的各類 Web 應用程式的開發框架和工具中,Session 也已經被包裝成容易使用的模...