2018年6月27日 星期三

ssh使用key(pageant)登入失敗 : Server refused our key


使用PuTTYgen,Parameter選DSA,將Key貼到/home/user/.ssh/authorized_keys內存檔

PuTTY內設定 Connection --> SSH --> Auth --> Private key file for authentication選好了private key檔案

設定完,測試PuTTY連線,失敗!!!

視窗內出現 "Server refused our key"




透過Google很快查到是SSH daemon對於key的安全性有嚴格限制

1.server上儲存key的目錄必須是700
chmod 700 /home/user/.ssh
ls -la /home/user/.ssh確認一下: rwx------

2.authorized_keys這個檔案必須是600
chmod 600 /home/user/.ssh/authorized_keys
ls -la /home/user/.ssh/確認: rw-------

重新測試,bingo!!!

2018年2月16日 星期五

[Python 3.6] pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

venv建立新環境後,安裝套件又發生問題

[root@python newproject]# ./bin/pip3 install requestspip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.Collecting requests  Could not fetch URL https://pypi.python.org/simple/requests/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping  Could not find a version that satisfies the requirement requests (from versions: )No matching distribution found for requests

參考以下網站,為Python 3.6增加SSL能力(https://stackoverflow.com/questions/41489439/pip3-installs-inside-virtual-environment-with-python3-6-failing-due-to-ssl-modul)

(1) 安裝openssl套件
yum install -y openssl openssl-devel (CentOS 7.4)

(2) 修改Python編譯設定檔
cd /usr/src/Python-3.6.4
./configure
vi ./Module/Setup (uncomment below lines #209~212)

SSL=/usr/lib64/openssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto

make
make altinstall


[Python 3.6] Error: Command '['/home/newproject/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1


python3.6下使用venv建立新專案環境時發生錯誤:

[root@python Home]# python3.6 -m venv /home/newproject

Error: Command '['/home/newproject/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

經搜尋,了解python3.6預設未安裝pip,且缺了ensurepip模組
(https://github.com/ContinuumIO/anaconda-issues/issues/6917)

實際執行模組確認,發生以下問題
[root@python Home]# python3.6 -m ensurepip.....(略).....
zipimport.ZipImportError: can't decompress data; zlib not available

再經過一番搜尋,
確認必須重編python3.6支援zlib解壓縮
(網路上有其他論壇寫到./configure --with-zlib-dir=... ,此參數試過不支援)
參考下方網頁Alex Zhou的作法,重新編譯
(http://zhoujianghai.iteye.com/blog/1521993):

注意:最後步驟我改為make altinstall,保留MacOS的Python2.7,

cd /usr/src/Python-3.6.4 (我的source解開後的位置)
./configurevi Modules/Setup
(找到下面這段,將註解符號去除讓它生效)
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
make
make altinstall
完成!




2017年6月11日 星期日

Windows內建FTP client與passive mode

國內外論壇都說 Windows 內建 FTP client 不支援 passive mode


實際下指令 pasv,果然不行



這麼一來,FTP server 放在 NAT 路由器後方就不好處理了

幸運查到 可透過本地下 literal 指令將 pasv 指令遞給 server 處理:

literal pasv 5000

如果 FTP server 支援 passive mode
收到這指令後會嘗試以 TCP:5000 建立資料傳輸連線

(路由器須啟用 Virtual Server 或 Port Forwarding 功能,加一筆允許外部電腦連線至 路由器 TCP port 5000 對應至內網 FTP server IP 位址的設定。跟當初為 FTP server 服務加的那筆 TCP port 21 設定一樣)

成功後會看到訊息:

227 Entering Passive Mode (192,168,1,100,19,136).

括號內的資訊這樣讀:

192,168,1,100 就是 FTP server 位址 192.168.1.100
後方接著 19,136 是埠號 19*256 + 136 = 5000

沒認真查是 FTP 定義的寫法或是 Microsoft 獨創...

反正結果是傳檔OK!

PostgresSQL將table內容匯出為csv格式

PostgreSQL version : 9.2.18-1


COPY mytable TO '/tmp/mytable.csv' DELIMITER ',' CSV HEADER;

簡易說明:

1.須以管理者身分登入postgresql
2.將 mytable 換成你要匯出的 實際table名
3./tmp/mytable.csv 換成匯出後儲存的csv檔名即可(注意寫入權限)
4.最後的 CSV HEADER 拿掉,csv檔就不會有標題列

2017年5月18日 星期四

PHP PDO無法存取PostgreSQL在CentOS的問題

作業環境如下-

   CentOS Linux 7.3
   Apache/2.4.6 (CentOS)
   PostgreSQL 9.2.18

一支簡單的PHP程式,連線本機的PostgreSQL資料庫發生錯誤

   ....could not connect to server: Permission denied.... 


Google查到可能是SELinux保護功能阻擋了連線

確認SELinux狀態:

指令: 
   /usr/sbin/sestatus

   SELinux status:               enabled
   SELinuxfs mount:              /sys/fs/selinux
   SELinux root directory:       /etc/selinux
   Loaded policy name:           targeted
   Current mode:                 enforcing
   Mode from config file:        enforcing
   Policy MLS status:            enabled
   Policy deny_unknown status:   allowed
   Max kernel policy version:    28



確實SELinux是作用中,不太懂,只好查查設定值:

指令: 
   /usr/sbin/getsebool -a | grep httpd

   httpd_can_network_connect --> off


果然! 預設不允許httpd主動發起連線



修改設定:

指令: 
/usr/sbin/setsebool -P httpd_can_network_connect on


等了大約30秒,shell prompt終於回來了(上面getsebool指令可確認結果)


再測試PHP網頁,成功連線讀取PostgreSQL資料!

ciao~




  
   

2017年5月11日 星期四

Debian 6.0 VM 複製到 ESXi 6.0 網路卡問題

Debian VM原運作於VMWare Player,因匯入失敗,乾脆將整個目錄(含vmdk檔等)直接上傳到ESXi資料存放區

由於檔案格式不同,先用以下指令轉換為ESXi相容格式

   vmkfstools -i <原vmdk檔> <新vmdk檔>


建立VM後成功啟動系統,發現網路卡都沒出現



Google搜尋結果:

正確運作的系統會由udev於開機時偵測到網路卡,自動建立設定檔為/etc/udev/rules.d/70-persistent-net.rules。但我的/etc/udev/rules.d/下沒這個檔案!

執行 ifconfig -a 有看到 eth0 裝置,不過 ifup eth0 失敗

重新開機數次,確認系統不會自動找回網路卡



一番瞎搞後,終於成功救活,紀錄如下:

(此非正規作法,請斟酌參考,專家勿罵,留言批評者請順便提供正確教學)
(作法參考某論壇國外先進討論串,但已搜尋不到該文,對不起~)



1.手動讓它產生正確的 70-persistent-net.rules 檔

   a.下指令 ifconfig -a 找出正確的 eth0 mac 位址如(00:a0:11:22:33:44)

   b.複製原script(幫不上忙的那個)準備手動執行

      cp /lib/udev/write_net_rules /lib/udev/my_net_rules


   c.編輯手動版script (vi /lib/udev/my_net_rules),加上


      INTERFACE=eth0

      MATCHADDR="00:a0:11:22:33:44" << 步驟a 抄下來的 mac位址
 

   d.修改權限

      chmod 744 /lib/udev/my_net_rules

   e.執行!

      /lib/udev/my_net_rules

   f.檢查結果

      /etc/udev/rules.d/70-persistent-net.rules  出現!!!




 

2.測試新的設定檔

   a.重新載入設定檔

      /sbin/udevadm control --reload-rules

   b.手動啟動 eth0

      ifup eth0

   c.看看結果

      ifconfig
   (此時應該要有eth0了,沒有就是.....失敗)


 
其實還有些細節,寫多了偏離主題,下方網路設定檔供參考

/etc/network/interfaces 檔案內容

auto eth0

iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.254
dns-nameserver 168.95.192.1

如果 ifup eth0 得到 don't seem to have all the variables... 錯誤,檢查上面這個檔的內容