Skip to content

Latest commit

 

History

History
208 lines (174 loc) · 6.1 KB

File metadata and controls

208 lines (174 loc) · 6.1 KB

0924

設定文字編輯器 vim 環境

# 移動到 root 的家目錄
$ cd

# 確認在root 的家目錄
$ pwd
# /root

# 建立與編輯 vim 環境組態
$ yum install vim (CentOS 7)

$ vim .vimrc
set ts=4 # tab space
set nu # number 行號

vi(vim) 編輯器

# 由 命令模式 進入 編輯模式
i       插入文字
a       會跳到游標後面一個字元的位置,才進入『-- INSERT --』狀態
o       在目前游標所在位置的下一行會新增一行空行 
esc     由 編輯模式 回到 命令模式

g       直接跳到檔案的第一行
G       直接跳到檔案的最後一行
:<line> 跳到第 <line> 行
dd      剪下整行
dd 8    剪下八行
yy      複製整行
yy 5    複製五行
p       貼上到游標的後面
u       復原,可連續復原

:w      儲存檔案
:q      離開程式
:q!     強制離開
:set nu �顯示行號

# 字串取代
:<start line>, <end line> s/<search string>/<replace string>/g
/g        全部取代
/gc       全部取代,取代前詢問
/<number> 取代 <number> 個
# 從第一行到最後一行,搜尋 centos,全部取代成 ntcb1001
:1, $ s/centos/ntcb1001/g

/<string> 往下搜尋 <string> 字串
?<string> 往上搜尋 <string> 字串
n         依原本方向繼續搜尋下一個 
N         以相反方向繼續搜尋下一個

# 刪除行
:<start line>, <end line> d
:12, 36 d   # 將第12行至36行刪除

# 比較少用
h, j, k, l  方向鍵(左下上右)
ctrl f      往下翻一個螢幕
ctrl b      往上翻一個螢幕

[練習] vi(vim) 編輯器

$ cd /home

$ groupadd -g 500 student
# 如果 500 已經被使用,請改用其他數字,並將下方出現 500 的地方,一起改為自訂的 id

$ tail -n 3 /etc/passwd > user.txt
$ cat user.txt # 修改前
# sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
# tcpdump:x:72:72::/:/sbin/nologin
# centos:x:500:500:centos:/home/centos:/bin/bash

$ vim user.txt
dd * 2  # 留下 centos:x:500:500:centos:/home/centos:/bin/bash
:1,$s/centos/ntcb1001/g # 把 centos 都取代成 ntcb1001
# 手動把 500 改成 1001 # ntcb1001:x:1001:500:ntcb1001:/home/ntcb1001:/bin/bash
yy 4    # 複製四行
p       # 貼上
:2, 2 s/1001/1002/g # 將第二行的 1001 改為 1002,後面幾行請類推
:wq     # 寫入並離開

$ cat user.txt # 修改後
# ntcb1001:x:1001:500:ntcb1001:/home/ntcb1001:/bin/bash 
# ntcb1002:x:1002:500:ntcb1002:/home/ntcb1002:/bin/bash 
# ntcb1003:x:1003:500:ntcb1003:/home/ntcb1003:/bin/bash 
# ntcb1004:x:1004:500:ntcb1004:/home/ntcb1004:/bin/bash 
# ntcb1005:x:1005:500:ntcb1005:/home/ntcb1005:/bin/bash 

# 使用 user.txt 新增使用者
$ newusers < user.txt
# 新增使用者後,會在家目錄下建立使用者專屬的資料夾,故可以此確認新增是否成功
$ ls -l /home
# 確認 新增使用者 影響的三個設定檔
$ tail -n 5 /etc/passwd
$ tail -n 5 /etc/shadow
$ tail -n 5 /etc/group

$ vim userpw.txt
# 手輸第一行 # ntcb1001:centos
yy 4 # 複製四行
p    # 貼上
# 手動 修改 # ntcb1002 ~ ntcb1005

$ cat userpw.txt
# ntcb1001:centos 
# ntcb1002:centos 
# ntcb1003:centos 
# ntcb1004:centos 
# ntcb1005:centos

# 使用 userpw.txt 修改使用者密碼
$ chpasswd < userpw.txt
# 確認修改密碼是否成功
$ tail -n 5 /etc/passwd
# 測試登入,username = ntcb1001 ~ ntcb1005
$ ssh <username>@<hostname>
# CentOS 7 新增的五個使用者可能皆無法登入,為正常現象
# 因為缺少了 bash 相關的檔案 (.bash_logout、.bash_profile、.bashrc)
# 此方法新增使用者並不正規,僅供熟悉 vim 使用

設定主機名稱

# 網路設定
$ vim /etc/sysconfig/network (CentOS 6)
HOSTNAME=CentOS6 # 主機名稱
# 主機名稱設定
$ vim /etc/hostname (CentOS 7)
CentOS7 # 主機名稱

# 重新開機後,設定才會生效
$ reboot

設定主機名稱對應檔

$ ip a
# 確認 <CentOS 6 ip> & <CentOS 7 ip>

$ vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomai    n4
::1         localhost localhost.localdomain localhost6 localhost6.localdomai    n6

10.0.2.4 CentOS6 # <CentOS 6 ip> <CentOS 6 hostname>
10.0.2.5 CentOS7 # <CentOS 7 ip> <CentOS 7 hostname>
# 10.0.2.4 centos6.ntub.edu.tw
# 10.0.2.5 centos7.ntub.edu.tw
# 10.0.2.4 wordpress6.ntub.edu.tw
# 10.0.2.5 wordpress7.ntub.edu.tw

修改主機名稱對應檔

可能忘記使用 ip a 檢查 ip,或是手殘打錯 hostname,而再次修改 /etc/hosts 可能產生類似下方錯誤

# 請先將錯誤的 ip 及 hostname 修正
$ vim /etc/hosts

# 先嘗試重新連線
$ ssh <username>@<CentOS ip/CentOS hostname>

# 若持續發生類似下方錯誤
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
c3:fa:c1:84:bf:ac:d8:d2:44:7b:aa:3d:25:f0:a0:41.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:1
RSA host key for 10.0.2.15 has changed and you have requested strict checking.
Host key verification failed.

# 將錯誤產生的 KEY 移除
$ vim /root/.ssh/known_hosts

# (暴力解,簡直慘不忍睹 QQ)
# 直接把整個 KEY 檔刪掉,重新連線時會再次產生新的 KEY檔
$ rm -rf /root/.ssh/known_hosts

防止遠端直接使用 root 連線

# 設定遠端連線服務,為了安全,SSH 服務預設無法使用 root 登入,先以一般使用者登入後再切換到 root
# 編輯 SSH 組態
$ vim /etc/ssh/sshd_config
#PermitRootLogin yes # default
PermitRootLogin no

#重新啟動 SSH 服務,設定才會生效

# CentOS 6
$ service sshd restart
$ chkconfig httpd on # 設定開機啟動 SSH

# CentOS 7  
$ systemctl restart sshd.service
$ systemctl enable sshd.service # 設定開機啟動 SSH