CentOS 7 Environment Setup

CentOS 7 Set Environment

Modify /home Space To /root

CentOS 7 預設的 Root 大小為 50GB,也就是說如果硬碟容量超過 50GB 或分配空間超過 50GB,剩餘空間都會分配給 /home。此節將介紹如何縮減 /home 並擴展 /root 空間。

Exclude /home

  1. 備份 /home 分區文件。

    1
    $ tar -czvf /tmp/home.tgz -C /home .
  2. 安裝 psmisc。

    1
    $ yum install psmisc
  3. 卸載 /home 檔案系統。

    1
    $ umount /home

    如果無法卸載,是因為有程序占用 /home,可以用下面的命令來停止占用的程序:

    1
    $ fuser -km /home/
  4. 删除 /home 所在的 lv

    1
    $ lvremove /dev/mapper/centos-home

Extend /root

  1. 擴展 /root 所在的 lv

    1
    2
    3
    4
    5
    # 指定 /root 為 100GB (lvextend 可用 lvresize 取代)
    $ lvextend -L 100G /dev/mapper/centos-root

    # 新增 100GB 給 /root (lvextend 可用 lvresize 取代)
    $ lvextend -L +100G /dev/mapper/centos-root
  2. 擴展 /root

    1
    $ xfs_growfs /dev/mapper/centos-root
  3. 檢查 /root 檔案系統的空間。

    1
    $ df -h

Recover /home

  1. 重新建立 /home 的 lv

    1
    2
    # 建立 /home lv 並給予所有剩餘空間 (lvcreate) 可用 lvresize 取代)
    $ lvcreate -l 100%FREE -n home centos
  2. 建立 /home 檔案系統

    1
    $ mkfs.xfs  /dev/mapper/centos-home
  3. 掛載 /home 檔案系統

    1
    $ mount /dev/mapper/centos-home /home
  4. 復原 /home 檔案系統

    1
    2
    $ tar -xzvf /tmp/home.tgz -C /home
    $ rm -f /tmp/home.tgz

SSH

在本節中,將安裝 OpenSSH 使主機可透過 SSH 連線。

OpenSSH

OpenSSH(OpenBSD Secure Shell)是使用 SSH 透過電腦網路加密通訊的實現。它是取代由 SSH Communications Security 所提供的商用版本的開放原始碼方案。目前 OpenSSH 是 OpenBSD 的子計畫。

OpenSSH 常常被誤認以為與 OpenSSL 有關聯,但實際上這兩個計畫有不同的目的,不同的發展團隊,名稱相近只是因為兩者有同樣的軟體發展目標──提供開放原始碼的加密通訊軟體。

Wikipedia ── OpenSSH

首先透過 yum 安裝 OpenSSH Server:

1
$ yum install -y openssh-server

安裝完畢後,視需求編輯 OpenSSH 的組態檔,若要修改則取消註解後修改值即可:

請注意,在預設的檔案內,只要是被註解(#)的設定值即為預設值。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ vi /etc/ssh/sshd_config

# 修改 Port
# 選擇埠號時則盡量使用 1024 以上的埠號
Port <port>
# 限定登入者
AllowUsers <User1> <User2> ...
# 允許空密碼登入
PermitEmptyPasswords no
# 登入密碼驗證
PasswordAuthentication yes
# 限制 root 登入
PermitRootLogin no

若有更改預設 Port,記得要連 SELinux 一起設定:

1
$ semanage port -a -t ssh_port_t -p tcp <port>

接著重新啟動 SSH 以載入設定值並設定為開機自動啟用:

1
2
3
4
# 重新啟動 SSH
$ systemctl restart sshd.service
# 設定開機自動啟用
$ systemctl enable sshd.service

最後開啟防火牆使 SSH 能順利被使用:

1
2
3
4
5
# 開放防火牆
$ firewall-cmd --permanent --zone=public --add-port=<port>/tcp

# 重載防火牆規則
$ firewall-cmd --reload

若是想要查看 Port 有沒有被監聽,可以輸入 netstat -ant 來檢查。

需要注意的是,CentOS 7 的最小安裝並未把 netstat 安裝進去,所以如果執行失敗請輸入:

1
$ yum install net-tools

References

XRDP

XRDP is a free and open-source implementation of Microsoft RDP (Remote Desktop Protocol) server that enables operating systems other than Microsoft Windows (such as Linux and BSD-style operating systems) to provide a fully functional RDP-compatible remote desktop experience.

According to the developers, the protocol works with rdesktop, FreeRDP and as well as Microsoft’s own Remote Desktop Client.[5]

The project requires a X Window System installation in order to function.

Wikipedia ── Xrdp

首先透過 yum 安裝 XRDP 與 TigerVNC Server:

1
$ yum install -y xrdp tigervnc-server

預設的 XRDP Port 為 3389,使用者可依需求修改位於 /etc/xrdp/xrdp.ini 的設定檔,將 port=3389 更改為自己想要的 Port。修改完畢後記得重啟 XRDP 使設定值生效:

1
$ systemctl restart xrdp.service

若要設定 XRDP 為開機自啟:

1
$ systemctl enable xrdp.service

最後將 Port 加入至防火牆規則中:

1
2
$ firewall-cmd --permanent --zone=public --add-port=<port>/tcp
$ firewall-cmd --reload

References

Samba / CUPS

在本節中,將透過 Samba 配合 CUPS 建立網路印表機以及共享磁碟機。

Samba

Samba,是種用來讓 UNIX 系列的作業系統與微軟 Windows 作業系統的 SMB/CIFS(Server Message Block/Common Internet File System)網路協定做連結的自由軟體。第三版不僅可存取及分享 SMB 的資料夾及印表機,本身還可以整合入 Windows Server 的網域,扮演為網域控制站(Domain Controller)以及加入 Active Directory 成員。簡而言之,此軟體在 Windows 與 UNIX 系列作業系統之間搭起一座橋樑,讓兩者的資源可互通有無。

Wikipedia ── Samba

首先透過 yum 安裝 Samba:

1
$ yum install -y samba

安裝完畢後,開始編輯 Samba 的組態檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ vi /etc/samba/smb.conf

[global]
workgroup = SAMBA # 類似微軟網路芳鄰所採用的工作群組
security = user # 預設安全等級,使用者必須先輸入帳號與密碼,經 SAMBA 伺服器驗證通過後,才可登入使用分享的資源

passdb backend = tdbsam # 代表使用 pdbedit 方式管理帳號密碼

printing = cups
printcap name = cups
load printers = Yes
cups options = raw

[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes

[public]
comment = Lab 802 Public
path = /var/samba_share/public
public = Yes
browseable = Yes
writeable = Yes
guest ok = No

[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No

[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775

接著設定一些 Samba 的布林值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 查看 Samba 相關的布林值
$ getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off

# 開啟 SELinux 的 samba_enable_home_dirs 布林值,關閉 SELinux 對 Samba 的限制,-P 選項設定不只現在開啟,開機時也開啟此布林值
$ setsebool -P samba_enable_home_dirs on

# 開啟 SELinux 的 samba_export_all_ro 布林值,允許 Samba 讀取,-P 選項設定不只現在開啟,開機時也開啟此布林值
$ setsebool -P samba_export_all_ro on

# 開啟 SELinux 的 samba_export_all_rw 布林值,允許 Samba 讀取及寫入,-P 選項設定不只現在開啟,開機時也開啟此布林值
# 此布林值若設為 on 將覆寫 samba_export_all_ro
$ setsebool -P samba_export_all_rw on

# 查詢 samba_export_all_rw 布林值是否已開啟
$ getsebool samba_export_all_rw
samba_export_all_rw --> on

為了讓 Samba 能正確被訪問,因此也必須開啟防火牆:

1
2
3
$ firewall-cmd --permanent --zone=public --add-service=samba
success
$ firewall-cmd --reload

最後,將現有的使用者加入至 Smaba 帳戶:

1
2
3
4
5
6
7
8
# Add user to samba
$ smbpasswd -a <username>
New SMB password:
Retype new SMB password:
Added user <username>

# Check user list
$ pdbedit -L -v

至此完成 Samba 設定,接著開始設定印表機。

請注意,如果有共用資料夾(例如 public),請記得為資料夾透過 chmodchown 指令加上權限。

以下指令為示範建立一使用者 holey,並加入 lab 群組,最後將剛才所建立的 public 資料夾設定群組 lab 成員與擁有者可讀可寫可執行權限:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Add user
$ useradd holey

# Add group
$ groupadd lab

# Add user to group
$ usermod -a -G lab holey

# Set folder owner
$ chown :lab802 /var/samba_share/public/

# Set folder permission
$ chmod ug+rwx /var/samba_share/public/

References

CUPS

CUPS(前為 Common Unix Printing System,即 UNIX 通用列印系統的縮寫,但現無官方全名)是一個類 Unix 操作系統的組合式印刷系統,允許一台電腦作為列印伺服器。CUPS 接受一個客戶端的電腦進程,並送到相應的印表機。
CUPS 是自由軟體,使用GNU通用公眾授權條款和GNU較寬鬆公共許可證的第2版。

Wikipedia ── CUPS

Before setup CUPS, make sure that the printer SUPPORTED Linux (with driver).

  1. 安裝 Printer Driver

  2. 開啟瀏覽器,輸入 http://localhost:631

  3. 點擊標題列 Administration,選擇 Printers 內的 Add Printer

  4. 輸入驗證資料(User Name / Password)

  5. 選擇印表機

  6. 設定印表機資訊

    1
    2
    3
    4
    5
    Name        : 印表機名稱(必填)
    Description : 印表機說明(選填)
    Location : 印表機位置(選填)
    Connection : 印表機連線(系統設定)
    Sharing : 分享印表機(請勾選)
  7. 接著 Select Model 選擇印表機的 Model,最後按下 Add Printer 完成設定。

  8. 設定完畢後可能會導向至 Set Default Options for <Printer>,可以忽略不設定使用預設值。

  9. 最後可點擊標題列 Printers 檢視印表機是否已新增至 CUPS。

若要開啟遠端 Web GUI 連線請依循下列步驟:

  1. 設定允許 IP

    1. 開啟 /etc/cups/cupsd.conf
    2. 註解 Listen localhost:631 並在次行加入 Port 631
    3. 等標籤內加入允許之 IP。
    4. 修改完後的 conf 檔案應類似於下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
      $ vi /etc/cups/cupsd.conf

    ...

    #Listen localhost:631
    Port 631
    Listen /var/run/cups/cups.sock

    ...

    # Restrict access to the server...
    <Location />
    Order allow,deny
    allow 192.168.0.101
    </Location>

    # Restrict access to the admin pages...
    <Location /admin>
    Order allow,deny
    allow 192.168.0.101
    </Location>

    # Restrict access to configuration files...
    <Location /admin/conf>
    AuthType Default
    Require user @SYSTEM
    Order allow,deny
    allow 192.168.0.101
    </Location>
    1. 將 Port 631 加入至防火牆規則:
    1
    2
    $ firewall-cmd --permanent --zone=public --add-port=631/tcp
    $ firewall-cmd --reload

最後若要在 Windows 上連線至網路印表機,請依循下列步驟:

  1. 安裝印表機驅動程式。
  2. 開啟 控制台\硬體和音效\裝置和印表機 選擇新增印表機。
  3. 點選 我要的印表機未列出
  4. 選擇 依名稱選取共用的印表機,填入 \\<Server IP>\<Printer Name>
  5. 接著輸入驗證資訊,並選擇驅動程式。
  6. 完成設定。

Custom CSH color

在預設模式下,CentOS 7 執行 bash 模式,若在 bash 模式下輸入 csh 即可切換為 csh 模式,但兩者因 prompt 長的一樣使使用者難以辨識當前模式,故此節將更改 csh 模式下的 prompt

請注意,當前使用者環境設置優先權大於全局使用者環境。

Modify the Current User Environment

開啟或建立 ~/.tcshrc 檔案:

1
$ vi ~/.tcshrc

接著新增一行:

1
set prompt='[%n@%m %c %{^[[31m%}CSH%{^[[m%}]%# '

Modify the Global User Environment

首先先開啟 /etc/csh.cshrc 檔案:

1
$ vi /etc/csh.cshrc

接著在所有 if ($?tcsh) then 中將:

1
set prompt='[%n@%m %c]%# '

修改為:

1
set prompt='[%n@%m %c %{^[[31m%}CSH%{^[[m%}]%# '

之後存檔並結束 csh 模式並重新啟用。

之後存檔並結束 csh 模式並重新啟用。

References

EDA Tools Environment

為了方便使用者不需要在每次使用 EDA Tools 重新 source 授權,因此建置使用者環境。此節將介紹兩種 source 方式。

Create and Auto-Load the Source File

  1. 建立檔案 /usr/cad/env.csh,並輸入所有需要 source 的授權或是指令,例如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # Incisive Enterprise Simulator
    $ source /usr/cad/cadance/CIC/incisiv.cshrc
    # Innovus Implementation System
    $ source /usr/cad/cadance/CIC/innovus.cshrc
    # JasperGold
    $ source /usr/cad/synopsys/CIC/jasper.cshrc
    # ModelSim
    $ source /usr/cad/mentor/CIC/modelsim.cshrc
    # Design Compiler
    $ source /usr/cad/synopsys/CIC/synthesis.cshrc
    # IC Compiler
    $ source /usr/cad/synopsys/CIC/icc.cshrc
    # Verdi Automated Debug System
    $ source /usr/cad/synopsys/CIC/verdi.cshrc
    # riscv-tools
    $ setenv PATH "/usr/cad/RISCV/bin:$PATH"
  2. 建立檔案 /usr/cad/.tcshrc

    1
    source /usr/cad/env.csh
  3. 建立檔案 /usr/cad/env.sh

    1
    cp /usr/cad/.tcshrc ./
  4. 建立完畢後移動到 User 目錄,並透過 ls -al 查看有無 .tcshrc 檔案,若有便刪除(rm -rf .tcshrc),接著下 sh /usr/cad/env.sh 即可完成使用者環境建置。

Create menu List for User to Load

  1. 建立檔案 /usr/cad/menu.sh,並鍵入以下內容或是直接下載本站提供之檔案:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    #!/bin/csh

    # Set colors
    set red = "\033[1;31m"
    set green = "\033[0;32m"
    set yellow = "\033[1;33m"
    set blue = "\033[1;34m"
    set magenta = "\033[1;35m"
    set cyan = "\033[1;36m"
    set white = "\033[0;37m"
    set end = "\033[0m"

    # Display menu
    echo "**************************************************"
    echo "* SOUECE MENU *"
    echo "**************************************************"
    echo "* ----- Cadence ----- *"
    echo "*\t${red}1${end}) Incisive Enterprise Simulator *"
    echo "*\t${red}2${end}) Innovus Implementation System *"
    echo "*\t${red}3${end}) JasperGold *"
    echo "* *"
    echo "* ----- Mentor ----- *"
    echo "*\t${red}4${end}) ModelSim *"
    echo "* *"
    echo "* ----- Synopsys ----- *"
    echo "*\t${red}5${end}) Design Compiler *"
    echo "*\t${red}6${end}) IC Compiler *"
    echo "*\t${red}7${end}) PrimeTime-PX *"
    echo "*\t${red}8${end}) Verdi Automated Debug System *"
    echo "* *"
    echo "* ----- RISCV ----- *"
    echo "*\t${red}9${end}) riscv-tools *"
    echo "* *"
    echo "* ----- Others ----- *"
    echo "*\t${red}a${end}) Source ALL *"
    echo "**************************************************"

    # Set variable `sel and get input
    echo -n "Enter the tools code: "
    set sel = $<

    # Set variable for checking is sourced or not
    set isSourced = 0

    # Source tools
    if ("$sel" =~ *"a"*) then
    # A statement variable means already sourced or not
    set isSourced = 1
    # Cadence
    source /usr/cad/cadence/CIC/incisiv.cshrc
    source /usr/cad/cadence/CIC/innovus.cshrc
    source /usr/cad/cadence/CIC/jasper.cshrc
    # Mentor
    source /usr/cad/mentor/CIC/modelsim.cshrc
    # Synopsys
    source /usr/cad/synopsys/CIC/synthesis.cshrc
    source /usr/cad/synopsys/CIC/icc.cshrc
    source /usr/cad/synopsys/CIC/primetime.cshrc
    source /usr/cad/synopsys/CIC/verdi.cshrc
    # riscv-tools
    setenv PATH "/usr/cad/RISCV/bin:$PATH"
    endif
    if ("$sel" =~ *"1"*) then
    set isSourced = 1
    source /usr/cad/cadence/CIC/incisiv.cshrc
    endif
    if ("$sel" =~ *"2"*) then
    set isSourced = 1
    source /usr/cad/cadence/CIC/innovus.cshrc
    endif
    if ("$sel" =~ *"3"*) then
    set isSourced = 1
    source /usr/cad/cadence/CIC/jasper.cshrc
    endif
    if ("$sel" =~ *"4"*) then
    set isSourced = 1
    source /usr/cad/mentor/CIC/modelsim.cshrc
    endif
    if ("$sel" =~ *"5"*) then
    set isSourced = 1
    source /usr/cad/synopsys/CIC/synthesis.cshrc
    endif
    if ("$sel" =~ *"6"*) then
    set isSourced = 1
    source /usr/cad/synopsys/CIC/icc.cshrc
    endif
    if ("$sel" =~ *"7"*) then
    set isSourced = 1
    source /usr/cad/synopsys/CIC/primetime.cshrc
    endif
    if ("$sel" =~ *"8"*) then
    set isSourced = 1
    source /usr/cad/synopsys/CIC/verdi.cshrc
    endif
    if ("$sel" =~ *"9"*) then
    set isSourced = 1
    setenv PATH "/usr/cad/RISCV/bin:$PATH"
    endif

    # Check is sourced or not
    if ("$isSourced" != "1") then
    echo "${red}CAN NOT MATCH ANY TOOL CODE FROM [$sel]${end}"
    endif

    # Unset variable `sel
    unset sel
    # Unset colors
    unset red
    unset green
    unset yellow
    unset blue
    unset magenta
    unset cyan
    unset white
    unset end

    echo
  2. 在使用者 /home 目錄下建立檔案 .tcshrc

    1
    2
    # menu script
    alias menu 'source /usr/cad/menu.sh'
  3. 建立完畢後,使用者可在 Terminal 輸入 csh 並開始使用 menu 來選擇 source。