顯示具有 Ubuntu 標籤的文章。 顯示所有文章
顯示具有 Ubuntu 標籤的文章。 顯示所有文章

2022年5月10日 星期二

Ubuntu SFTP設定

環境:
OS:Ubuntu 22.04 Server

目的:
  • 使用加密連線
  • 使用 SFTP 協定登入 OpenSSH 伺服器時,預設使用者可以任意的在檔案系統間遊走,因安全的理由透過 chroot() 的方法將使用者限制在(chroot) 特定目錄下,讓他不能跑到其他目錄瀏覽資料。
  • 設定完成之帳號,僅可使用SFTP 協定登入,無法使用ssh登入。

步驟:
1.確認openssh版本>4.8
$ ssh -V

2.建立SFTP用群組
$ sudo groupadd sftp_group

3.將使用者加入SFTP用群組
$ sudo usermod -G sftp_group <使用者帳號>

4.查帳號是否正確加入群組
$ grep sftp_group /etc/group

5.確認home目錄與有者權限為root:root,755。
$ ls -ld /home
drwxr-xr-x   8 root root  4096 May 10 01:08 home

6.確認使用者home目錄與有者權限為root,755。
$ ls -ld /home/<使用者帳號>
drwxr-xr-x 6 root <使用者帳號> 4096 Apr 10 03:47 <使用者帳號>

$ sudo chown root /home/<使用者帳號>

7.禁止使用者以ssh登入shell
$ sudo vim /etc/shells
加入/bin/false

$ sudo usermod -s /bin/false <使用者帳號>

8.設定SSH Config檔案
$ sudo vim /etc/ssh/sshd_config

#Subsystem       sftp    /usr/lib/openssh/sftp-server
Subsystem sftp internal-sftp

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server

Match Group sftp_group
        ChrootDirectory %h
        X11Forwarding no
        AllowTcpForwarding no
        PermitTTY no
        ForceCommand internal-sftp

9.測試config後重啟sshd
$ sudo sshd -t
$ sudo systemctl restart sshd


10使用FileZilla以22port連線測試

2021年5月25日 星期二

Ubuntu Apache 2.4 設定X-Frame-Options,避免點選劫持 (clickjacking) 的攻擊。

環境:
OS:Ubuntu 20.04 Server
Apache:2.4


設置此標頭將防止其他站點將來自該站點的頁面嵌入為框架。 這可以防禦點擊劫持攻擊,避免點選劫持 (clickjacking) 的攻擊。(防止釣魚網站透過iframe來籤入自己的網站。)


X-Frame-Options回應標頭可用值有:
DENY:瀏覽器拒絕當前頁面載入任何Frame頁面
SAMEORIGIN:frame頁面的地址只能為同源域名下的頁面
ALLOW-FROM:origin為允許frame載入的頁面地址


設定方式:
(1)啟用mod_headers
sudo a2enmod headers
sudo systemctl restart apache2

(2)設定
sudo vim /etc/apache2/conf-enabled/security.conf (預設已啟用此modules)

#將下列一行註解移除
Header set X-Frame-Options: "sameorigin"

(3)重啟服務
sudo systemctl restart apache2

2020年6月17日 星期三

Apache 2.4 新增HTTP VirtualHost

環境:
OS:Ubuntu 18.04 Server
Apache2.4
網站目錄:/www/example1
domain name:www.example1.com

1.增加網站目錄之權限設定
cd /etc/apache2/conf-available/
sudo vim example1.conf

#於example1.conf中加入下列設定,可參考apache2.conf中之<Directory /var/www/>之設定。
<Directory /www/example1>
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

2.啟用設定
sudo a2enconf example1
sudo systemctl reload apache2

3.新增HTTP之VirtualHost
cd /etc/apache2/sites-available
sudo cp 000-default.conf example1.conf
sudo vim example1.conf

#內容設定如下
<VirtualHost *:80>
    ServerName www.example1.com
    ServerAdmin admin@www.example1.com
    DocumentRoot /www/example1

    ErrorLog ${APACHE_LOG_DIR}/error_example1.log
    CustomLog ${APACHE_LOG_DIR}/access_example1.log combined
</VirtualHost>

4.啟用網站
sudo a2ensite example1.conf
sudo systemctl reload apache2

5.試試瀏覽網頁 http://www.example1.com

2018年5月9日 星期三

php7.2連Sybase資料庫

環境:
OS:Ubuntu 18.04 Server
Apache2.4
PHP7.2

因PHP7.0以後無sybase相關function,需透過freetds為連線介接,再使用PDO相關function進行連線。
1. 安裝相關套件
sudo apt install php7.2-sybase
2.設定freetds
sudo vim /etc/freetds/freetds.conf [global]
        client charset = UTF-8
        charset = utf8

[sybase_db]
        host = db.com.tw
        port = 5000
        tds version = 5.0

3. 使用PDO function測試是否連線成功。
<?php
$db_host = "sybase_db";
$db_user = "帳號";
$db_pass = "密碼";
$db_select = "dbname";

$dbconnect = "dblib:host=".$db_host.";dbname=".$db_select;
$pdo_sybase = new PDO($dbconnect, $db_user, $db_pass);

$sql = "select * from sybase_table where 1=1";
$res = pdo_sybase->query($sql);
        while ($row = $res->fetch()){
                print_r($row);
        }
?>

Ubuntu 停用IPv6或以IPv4連線優先

環境:
OS:Ubuntu 18.04 Server

在預設狀況下,ipv4、ipv6是同時啟用的,但常發現會優先使用ipv6連線時連線很慢,等到ipv6無法連線時,才會改為ipv4連線,在這過程中已經浪費一些時間。改善此狀況有下列兩種方式。

方式一:停用ipv6,此方法網路上很多說明,但個人並不建議用此方法。
1. 編輯 sudo vim /etc/default/grub
GRUB_CMDLINE_LINUX="ipv6.disable=1"     #修改此行
2. 執行 sudo update-grub2
3. 重開機後以ifconfig 檢查是否關閉ipv6

方式二:以ipv4優先,此方法較理想,不需停用ipv6。
1. 編輯 sudo vim /etc/gai.conf
#For sites which prefer IPv4 connections change the last line to
precedence ::ffff:0:0/96  100      #移除此行註解

2. ping google.com.tw 檢查是否以ipv4連線

2018年4月18日 星期三

Ubuntu Apache 2.4 正確使用conf、mods、sites等設定檔。

環境: 
OS:Ubuntu 14.04 Server 
Apache:2.4

一、目錄結構
/etc/apache2/
|-- apache2.conf
|-- conf-available
|       |-- *.conf
|-- conf-enabled
|-- mods-available
|       |-- *.conf
|-- mods-enabled
|-- sites-available
|       |-- *.conf
|-- sites-enabled


apache2.conf 是主要的設定檔。
conf-available/*.conf 是「可用的」config檔。
conf-enabled/*.conf 是「已啟用的」的config檔(皆為超連結)。
mods-available/*.conf 是「可用的」config檔。
mods-enabled/*.conf 是「已啟用的」的config檔(皆為超連結)。
sites-available/*.conf 是「可用的」站台設定檔。
sites-enabled/*.conf 是「已啟用的」的台設定檔(皆為超連結)。

注意!conf-enabled、mods-enabled、sites-enabled內的檔案只是連結至所對應「*-available」目錄中的設定檔,所以要設定檔案請在「*-available」目錄中設定,最後再用啟用設定檔的指令,在「*-enabled」目錄中建立連結。

二、啟用及關閉設定
1. conf啟用及關閉範例
啟用 sudo a2enconf charset
關閉 sudo a2disconf charset

2. mod啟用及關閉範例

啟用 sudo a2enmod userdir
關閉 sudo a2dismod userdir

3. site啟用及關閉範例


啟用 sudo a2ensite ssl

關閉 sudo a2dissite ssl

三、重啟Apache
1. 測試 Apache 設定檔有沒有錯誤,看到 Syntax OK, 就可以啟動apache2。
apache2ctl configtest

2. 重新啟動。
sudo service apache2 restart



2015年8月1日 星期六

Ubuntu Apache 2.4 啟動SSL

環境:
OS:Ubuntu 14.04 Server
Apache:2.4

Ubuntu Apache 2.4 是以啟用SSL虛擬主機之方式設定SSL服務。
本設定是以自行發行金鑰及憑證。


1. 安裝openssl。
    $sudo apt-get install openssl

2. 建立SSL資料夾,存放私鑰及憑證。
    $sudo mkdir /etc/apache2/ssl

2. 產生私鑰及憑證,使用自己的私鑰替自己的憑證簽章。
1. 切換目錄
$cd  /etc/apache2/ssl

2. 產生RSA Private Key,建議不要設密碼,否則 Apache 伺服器程式啟動時,讀取憑證和 Private Key 時就會詢問一次密碼。(key)
$sudo openssl genrsa -out apache.key 2048
$sudo chmod og-rwx apache.key

3. 產生憑證申請書。(csr)
憑證申請書,是把您的資料,和這個 Public Key 夾在一起,以便認證中心審核,簽上簽名用的。
$sudo openssl req -new -key apache.key -out apache.csr

Country Name (2 letter code) [AU]:TW
State or Province Name (full name) [Some-State]:Taiwan R.O.C
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:  //憑證的名稱(*重要*)
Email Address []:                                 //申請單位的聯絡信箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:按 Enter 直接跳過           //申請書的密碼
An optional company name []:按 Enter 直接跳過   //憑證代辦公司的名稱

檢查csr,Signature Algorithm是不是sha256WithRSAEncryption。
$openssl req -text -in apache.csr -noout

4. 產生有效期1年的伺服器憑證,繞過 CA 自己簽名的證書。(crt)
$sudo openssl x509 -req -days 365 -in apache.csr -signkey apache.key -out apache.crt

檢查crt,Signature Algorithm是不是sha256WithRSAEncryption。
$openssl x509 -text -in apache.crt -noout

5. 簽完憑證,憑證申請書就不用了,可以刪掉。
$sudo rm apache.csr

3. Apache啟動SSL模組,並重新啟動apache服務。
    $sudo a2enmod ssl
    $sudo service apache2 restart

4. 產生虛擬主機設定檔(複製預設檔)
    $cd /etc/apache2/sites-available
    $sudo cp default-ssl.conf ssl.conf

5. 修改sites-available/ssl.conf,設定憑證位置。
    $sudo vim /etc/apache2/sites-available/ssl.conf
SSLCertificateFile        /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile        /etc/apache2/ssl/apache.key

6. 修改mods-available/ssl.conf,停用SSLv3協定、停用RC4加密套件。(SSLv3 加密協定存在中間人攻擊弱點,RC4加密套件有漏洞,如果網站有使用HTTPS,強烈建議關閉)
    $nmap --script ssl-enum-ciphers -p 443        檢查SSL、加密套件資訊
$sudo vim /etc/apache2/mods-available/ssl.conf

SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!RC4 (在此屬性後面增加「:!RC4」)
SSLProtocol all -SSLv3

7. 啟用SSL虛擬主機,並重新啟動apache服務。
    $sudo a2ensite ssl
    $sudo service apache2 restart

8. 檢查port開放狀態,檢查/etc/apache2/ports.conf檔案,是否已設定port 443。    
    $netstat -atulnp
    $sudo vim /etc/apache2/ports.conf
Listen 80
<IfModule ssl_module>
       Listen 443
</IfModule>

2015年7月31日 星期五

Ubuntu Apache 啟動時出現ServerName錯誤

在Ubuntu啟動啟動Apache2時,Apache 可以正確被啟動,但如果出現下列訊息:
AH00558:apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message

在以前,我們會在/etc/apache2/apache2.conf中,手動增加「ServerName localhost」。但 Ubuntu 長久以來預設 Apache 安裝都沒加上 ServerName 的設定絕對有他的道理。

其實 Apache 在開啟的時候會將主機 hostname 設定為 ServerName ,所以 hostname 需為FQDN,如 hostname 不符 FQDN規範,就會出現上述錯誤訊息。
先使用 hostname 指令查出下列資訊:
  • hostname 取得目前本機設定好的 Hostname
  • hostname –i 取得目前本機 Hostname 對應的 IP
  • hostname –I 取得目前本機設定好的所有 IP 位址(會排除 loopback 介面)
例如我們取得下列資訊:
hostname => ubuntu
hostname -i => 127.0.1.1
hostname -I => 192.168.0.1


修改方式:
1. 透過 hostname 指令取得本機之hostname。

2. 修改 /etc/hosts 檔案中的 hostname 為標準FQDN名稱。
     $sudo vim /etc/hosts
     127.0.0.1 localhost
     127.0.1.1 原ubuntu 修改為 ubuntu.mydomain

3. 修改 /etc/hostname 檔案中的 hostname 名稱。
     $sudo vim /etc/hostname
     原 ubuntu 修改為 ubuntu.mydomain

4. 最後設定hostname完整的 FQDN 主機名稱 。(重開機也可)
     $sudo hostname ubuntu.mydomain

註:FQDN (fully qualified domain name,完整網域名稱), FQDN 包含兩部分﹕主機名稱和網域名稱。例如 mycomputer.mydomain.com。

2013年6月3日 星期一

Ubuntu 清理舊版核心流程

說明:\boot 空間不足時,會造成Linux 核心無法升級
原因:歷經多次升級和更新後,會有一堆的 Linux 核心版本被儲存在硬碟
解決:移除舊版核心

步驟:
1. 檢查有那些已安裝的 Linux 核心版本。(記下來)
    dpkg --get-selections | grep linux-image

2. 檢查目前正在用的版本(記下來)
    uname -r

3. 移除舊版本核心(目前正在用的版本與linux-image-generic兩者千萬不可移除)
    sudo apt-get purge linux-image-x.x.x-x-generic

4. 更新Grub2的選單
    sudo update-grub2

5. 完成

2011年5月22日 星期日

ubuntu安裝amule-daemon

1. 安裝amule-daemon
$sudo apt-get install amule-daemon

2.安裝amule-gnome-support
$sudo apt-get install amule-gnome-support

3. 設定amule-daemon
$sudo vim /etc/default/amule-daemon
# Configuration for /etc/init.d/amule-daemon

# The init.d script will only run if this variable non-empty.
AMULED_USER="使用者"
# You can set this variable to make the daemon use an alternative HOME.
# The daemon will use $AMULED_HOME/.aMule as the directory, so if you
# want to have $AMULED_HOME the real root (with an Incoming and Temp
# directories), you can do `ln -s . $AMULED_HOME/.aMule`.
AMULED_HOME="/home/使用者"
 
4. 產生MD5編碼後之密碼
$echo -n 你的密碼 |  md5sum | cut -d ' ' -f 1
 
5. 設定remote.conf
$cd /home/使用者目錄/.aMule/
$amuleweb -w
$vim remote.conf
Locale=
[EC]
Host=localhost
Port=4712
Password=編譯後的密碼

[Webserver]
Port=4711
UPnPWebServerEnabled=0
UPnPTCPPort=50001
Template=php-default
UseGzip=0
AllowGuest=0
AdminPassword=編譯後的密碼
GuestPassword=編譯後的密碼
6. 設定vim amule.conf

$vim amule.conf [ExternalConnect]
UseSrcSeeds=0
ShowPercent=1
ShowProgressBar=1
AcceptExternalConnections=1
ECPassword=編譯後的密碼
ECAddress=
ECPort=4712
UPnPECEnabled=0
IpFilterClients=1
IpFilterServers=1
UseSecIdent=1

[WebServer]
Enabled=1
Port=4711
UPnPWebServerEnabled=0
WebUPnPTCPPort=50001
PageRefreshTime=120
UseGzip=1
UseLowRightsUser=0
Password=編譯後的密碼
PasswordLow=
Template=default

7. 重新啟動amule-daemon
$sudo /etc/init.d/amule-daemon restart

8.連線
http://主機IP:4711

2010年12月15日 星期三

Ubuntu php用freetds連sybase

1. 先安裝php5-sybase
    sudo apt-get install php5-sybase


2. 設定freetds.conf

    sudo vim /etc/freetds/freetds.conf
    修改(增加)以下內容

    [global]
            client charset = UTF-8
            charset = utf8
    [sybase_db]
            host = sybase host
            port = 5000
            tds version = 5.0
3.於php中,以sybase_connect("sybase_db","user","pass")連線