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

2017年8月7日 星期一

Linux 常用資料庫介紹

    1-1 一些資料庫和緩存伺服器的特性與功能
    名稱
    類型
    資料存儲選項
    查詢類型
    附加功能
    Redis
    使用記憶體存儲(in-memory 的非關聯式資料庫
    字串、清單、集合、散清單、有序集合
    每種資料類型都有自己的專屬命令, 另外還有批量操作(bulk operation)和不完全(partial)的事務支援
    發佈與訂閱, 主從複製(master/slave replication), 持久化, 腳本(存儲過程,stored procedure
    memcached
    使用記憶體存儲的鍵值緩存
    鍵值之間的映射
    創建命令、讀取命令、更新命令、刪除命令以及其他幾個命令
    為提升性能而設的多執行緒伺服器
    MySQL
    關聯式資料庫
    每個資料庫可以包含多個表, 每個表可以包含多個行; 可以處理多個表的視圖(view); 支援空間(spatial)和協力廠商擴展
    SELECT  INSERT  UPDATE  DELETE 、函數、存儲過程
    支援ACID性質(需要使用InnoDB), 主從複製和主主複製 master/master replication
    PostgreSQL
    關聯式資料庫
    每個資料庫可以包含多個表, 每個表可以包含多個行; 可以處理多個表的視圖; 支援空間和協力廠商擴展;支援可定制類型
    SELECT  INSERT  UPDATE  DELETE 、內置函數、自訂的存儲過程
    支援ACID性質,主從複製, 由協力廠商支援的多主複製 multi-master replication
    MongoDB
    使用硬碟存儲(on-disk)的非關係文檔存儲
    每個資料庫可以包含多個表, 每個表可以包含多個無schema schema-less)的BSON文檔
    創建命令、讀取命令、更新命令、刪除命令、條件查詢命令,等等
    支援map-reduce操作,主從複製,分片, 空間索引(spatial inde

2017年7月13日 星期四

Linux OS Class [20170712x2] SUID/ SGID ACL

[root@server0 ~]# cd /srv
[root@server0 srv]# ll
total 64
-rwxr-xr-x. 1 root root 54048 Jul 12 19:46 cat
drwxrwxrwT. 4 root root    47 Jul 12 20:33 share
-rw-rw----+ 1 root root    16 Jul 12 19:46 test
[root@server0 srv]# mkdir doc
[root@server0 srv]# ll
total 64
-rwxr-xr-x. 1 root root 54048 Jul 12 19:46 cat
drwxr-xr-x. 2 root root     6 Jul 12 20:58 doc
drwxrwxrwT. 4 root root    47 Jul 12 20:33 share
-rw-rw----+ 1 root root    16 Jul 12 19:46 test
[root@server0 srv]# groupadd manager
[root@server0 srv]# usermod -aG  manager mary
[root@server0 srv]# usermod -aG  manager jerry
[root@server0 srv]# id mary
uid=1001(mary) gid=1001(mary) groups=1001(mary),1003(manager)
[root@server0 srv]# id jerry
uid=1002(jerry) gid=1002(jerry) groups=1002(jerry),1003(manager)
[root@server0 srv]# getfacl doc
# file: doc
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

[root@server0 srv]# setfacl -m g:manager:rwx doc
[root@server0 srv]# getfacl doc
# file: doc
# owner: root
# group: root
user::rwx
group::r-x
group:manager:rwx
mask::rwx
other::r-x

[root@server0 srv]# setfacl -m u:jerry:r-x doc
[root@server0 srv]# getfacl doc
# file: doc
# owner: root
# group: root
user::rwx
user:jerry:r-x
group::r-x
group:manager:rwx
mask::rwx
other::r-x

Linux OS Class [20170712x1] gnupg ssh-keygen use pgp

[mary@server0 ~]$ ll -a
total 24
drwx------. 5 mary mary 4096 Jul 12 21:00 .
drwxr-xr-x. 5 root root   43 Jul 12 19:47 ..
-rw-------. 1 mary mary  174 Jul 12 21:00 .bash_history
-rw-r--r--. 1 mary mary   18 Jan 29  2014 .bash_logout
-rw-r--r--. 1 mary mary  193 Jan 29  2014 .bash_profile
-rw-r--r--. 1 mary mary  231 Jan 29  2014 .bashrc
drwxrwxr-x. 3 mary mary   17 Jul 12 20:01 .cache
drwxr-xr-x. 3 mary mary   67 Jul 12 20:01 .config
drwxr-xr-x. 4 mary mary   37 Jan  7  2015 .mozilla
-rw-rw-r--. 1 mary mary 1395 Jul 12 20:10 secret
[mary@server0 ~]$ gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: directory `/home/mary/.gnupg' created
gpg: new configuration file `/home/mary/.gnupg/gpg.conf' created
gpg: WARNING: options in `/home/mary/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/home/mary/.gnupg/secring.gpg' created
gpg: keyring `/home/mary/.gnupg/pubring.gpg' created
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
Requested keysize is 4096 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 2w
Key expires at Wed 26 Jul 2017 09:32:04 PM CST
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: Mary Chen
Email address: mary@server0.example.com
Comment: Mary the Queen
You selected this USER-ID:
    "Mary Chen (Mary the Queen) <mary@server0.example.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.
****open a dialog window


gpg: cancelled by user
gpg: Key generation canceled.

#####################################
ssh pgp login
pi@pi0:~ $ssh-keygen
pi@pi0:~ $ls -a

pi@pi0:~ $ cd .ssh
pi@pi0:~/.ssh $ ls
id_rsa   #is pgp key
id_rsa.pub      #is public key
known_hosts

$pi@pi0:~/.ssh $ ssh-copy-id pi@192.168.0.155
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
pi@192.168.0.155's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'pi@192.168.0.155'"
and check to make sure that only the key(s) you wanted were added.

pi@pi0:~/.ssh $
pi@pi0:~/.ssh $
pi@pi0:~/.ssh $
pi@pi0:~/.ssh $
pi@pi0:~/.ssh $ ssh pi@192.168.0.155

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Jul 13 02:44:22 2017 from 192.168.0.152

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
$passwd -d pi  #remove the pi login passwd

2017年7月11日 星期二

用 dm-crypt 建立 Linux 加密檔案系統

Link => https://wiki.centos.org/FrontPage
Link => https://wiki.centos.org/zh-tw/HowTos/EncryptedFilesystem


用 dm-crypt 建立 Linux 加密檔案系統

一個加密的檔案系統可以保障硬盤不受硬件層面的攻擊。任何人將硬盤弄到手後,依然要用蠻力猜測加密金鑰,構成奪取你資料的一個重大阻礙。

Windows 及 Mac OS X 各自提供了它們的標準加密檔案系統工具,但 Linux 當然提供了大量工具來完成這個工作。現時盛行的工具似乎是 dm-crypt。透過 cryptsetup 這個工具程式,dm-crypt 為 Linux 提供了一個頗為簡潔及易用的加密檔案系統工具。

此外,CentOS 5 包括了一個支援 LUKS 的改良版 dm-crypt。LUKS 是一個即將面世的標準,指定有關加密磁碟區的資訊如何放置在硬盤上。有關加密資料的中繼資料是放置在分割區的標頭,好讓它能兼容不同系統及支援多個用戶密碼。此外,GNOME 及 HAL 擁有處理 LUKS 磁碟區的支援,而且當帶有 LUKS 磁碟區的卸除式媒體被插入時,會自動要求輸入密碼。如果你不需要兼容較舊版的 CentOS 或未支援 LUKS 的系統,我們建議你使用 LUKS 的方案。這份文章內的範例亦有描述用來設置加密 LUKS 磁碟區的指令。

這裡有些腳本是採用了下面描述的方法,可將建立、卸下、及重新掛載 LUKS 加密檔案系統自動化。

所需套件

在開始之前,請確定所有必須的套件都已經安裝了:

cryptsetup(CentOS-5 是 cryptsetup-luks)
device-mapper
util-linux
然而,它們很可能都已經存在於你的系統上,除非你進行了一個極度基本的安裝。

建立初期檔案系統

我多數將檔案加密,而不是整個分割區,因此我將 dm-crypt 與 losetup 回路裝置管理工具合併。下面是用 Unix 指令殼的原始語言描述如何建立及掛載一個加密檔案系統。


# 建立一個適合你需要的空白檔案。這個範例將會建立一個 8GB 的稀疏
# 檔案,意即沒有真正的資料塊會被寫入。由於我們稍後將會強制分派資
# 料塊,現在做的並沒有意義,因為這些資料塊將會被重新寫入。
dd of=/path/to/secretfs bs=1G count=0 seek=8
# 限制檔案的存取權
chmod 600 /path/to/secretfs
# 將檔案與回路裝置掛鈎
losetup /dev/loop0 /path/to/secretfs
# 加密裝置內的資料。在這個例子裡,cryptsetup 會利用 Linux 的裝置
# 對映器來建立 /dev/mapper/secretfs。-y 這個選項指定你將會被問及
# 密碼兩次(有一次作檢查之用)。
cryptsetup -y create secretfs /dev/loop0
# 又或者,如果你想使用 LUKS,你應該使用以下兩個(與及額外的)指
# 令參數。首個指令將磁碟區初始化,並設定一條原始金鑰。第二個指令開
# 啟分割區並建立一個對映(在範例中是 /dev/mapper/secretfs)。
cryptsetup -y luksFormat /dev/loop0
cryptsetup luksOpen /dev/loop0 secretfs
# 檢查它的狀況(選擇性)
cryptsetup status secretfs
# 現在我們將新加密的裝置填滿零。這會強制分派資料塊。由於這些零已
# 被加密,它們對外就看似隨機的資料,即管有人將藏有加密檔案系統的
# 檔案弄到手上,要找尋被加密的資料塊差不多是沒可能。
dd if=/dev/zero of=/dev/mapper/secretfs
# 建立一個檔案系統及檢查它的狀況
mke2fs -j -O dir_index /dev/mapper/secretfs
tune2fs -l /dev/mapper/secretfs
# 將新檔案系統掛載在一個方便的位置
mkdir /mnt/cryptofs/secretfs
mount /dev/mapper/secretfs /mnt/cryptofs/secretfs
卸下及保障檔案系統

要手動地卸下及保障加密檔案系統,你基本上要反向執行尾部份的指令。


# 卸下檔案系統
umount /mnt/cryptofs/secretfs
# 移除裝置對映
cryptsetup remove secretfs
# 或者,如果是一個 LUKS 磁碟區
cryptsetup luksClose secretfs
# 將檔案與回路裝置脫鈎
losetup -d /dev/loop0
重新掛載加密檔案系統

一旦你已經建立了一個加密檔案系統,重新掛載它的程序是相對地簡單:


# 將檔案與回路裝置掛鈎
losetup /dev/loop0 /path/to/secretfs
# 將對映裝置加密。你將會被問及密碼
cryptsetup create secretfs /dev/loop0
# 或者,如果是一個 LUKS 磁碟區
cryptsetup luksOpen /dev/loop0 secretfs
# 掛載檔案系統
mount /dev/mapper/secretfs /mnt/cryptofs/secretfs
請注意,就算你打錯了密碼,cryptsetup 亦不會提供有用的錯誤信息。你只會從 mount 得到一個不大有用的信息:

mount: you must specify the filesystem type
如果出現這個情況,請反覆用 cryptsetup 嘗試重新掛載那個檔案系統:


cryptsetup remove secretfs
cryptsetup create secretfs /dev/loop0
mount /dev/mapper/secretfs /mnt/cryptofs/secretfs
這點並不適用 LUKS 磁碟區,因為 cryptsetup 在 luksOpen 這一步將會提供有用的錯誤信息。

在一個 LUKS 磁碟區內加入額外的金鑰

如上面所提及,LUKS 這個格式容許使用多條金鑰。意思是你可以加入多於一條用來開啟加密裝置的金鑰。要新增金鑰,你只須:


cryptsetup luksAddKey <裝置>
舉個例說,你使用 /dev/loop0 這個回路,你可以執行:


cryptsetup luksAddKey /dev/loop0
cryptsetup 要求你輸入現有的密碼兩次。之後,你將會被問及新的密碼兩次。當這一步完成後,你便可以使用現有的金鑰,或者新增的金鑰來開啟這個磁碟區。

在系統開機時設置加密磁碟區

有些時候你或許會想在系統開機時設置加密磁碟區,例如為一台筆記型電腦設置一個加密了的 home 分割區。在 CentOS 5 上,這件事情可以透過 /etc/crypttab 輕易地完成。/etc/crypttab 描述那些須要在開機時建立對映的加密磁碟區及分割區。記錄是以新行分隔,而當中包含了以下資料欄:


對映名稱        裝置名稱        密碼檔案路徑        選項
當然,你一般不會需要全部四欄:

LUKS 磁碟區會忽視選項欄內的多數選項,因為 LUKS 磁碟區在磁碟區標頭內已收錄了一切關於密碼、密碼長度、及散列的必須資訊。另外,
一般來說,你不會想將一個純文字的密碼檔儲存在根分割區內。你當然可以將它儲存在其它地方,但是 rc.sysinit 在開機的這一個階段多數只會以唯讀式掛載了根目錄。如果密碼欄並不存在,或者數值是 none,系統在開機時會要求你輸入密碼。
因此,如果你使用 LUKS 磁碟區,而且想系統在開機時詢問密碼,你只需頭兩欄。讓我們看一個簡短的範例:


cryptedHome        /dev/sdc5
這個做會為 /dev/sdc5 上的加密磁碟區建立一個名叫 cryptedHome 的對映,而這個加密磁碟區先前是用 crypsetup luksFormat /dev/sdc5 來建立的。如果你亦在加密磁碟區上建立了一個檔案系統,你可以在 /etc/fstab 內新增一個記錄,好讓檔案系統在開機時被掛載:


/dev/mapper/cryptedHome       /home        ext3    defaults        1 2
有兩個選頁是不會被 LUKS 磁碟區所忽視的:

swap:當映像被設置後,這個磁碟區將會被格式化成為調換分割區。
tmp:這個磁碟區將會被格式化成為 ext2 檔案系統,並設定合適的權根作為暫存檔案用的檔案系統。
這兩個選項要求 /etc/fstab 內含有使用相關映像的記錄,而且這些選項都有破壞力。一個為加密調換分割區而設的記錄可能會看似:


cryptedSwap        /dev/sda2        none        swap
或者如果你不想在每次開機時為調換分割區輸入密碼:


cryptedSwap        /dev/sda2        /dev/urandom        swap
請注意,如果 /dev/sd2 已經是一個 LUKS 分割區,這個方法並不可行,因為 LUKS 分割區須要非隨機的金鑰。