# Linux Bash Command ## 常用指令(2022HQIT作業) `man + <command>`: 查看某個command 的help文件 Firewall (ufw) * `sudo ufw allow 5432/tcp`: 啟用5432 tcp port * `sudo ufw status`: 查看防火牆狀態 * `sudo ufw reset`: 重設防火牆後,會看到ufw disabled,要再次開啟 * `sudo ufw enable`: 啟動防火牆 * `sudo ufw limit SSH`: 限制ssh連線,如果某個ip在30秒內登入失敗6次,就會自動拒絕該ip連線 * `sudo ufw limit 5432/tcp`: 限制某個特定的port * `sudo ufw limit ftp`: 限制ftp連線 * `sudo ufw allow from 192.168.1.100 to any port 22`: 只允許ip 192.168.1.100 透過 port22 連線 * `sudo ufw limit from 10.105.28.0/24 to 10.105.29.1 port 22 proto tcp comment 'allow SSH'`: 允許10.105.28.0/24 (client ip) 透過port 22以受限的tcp連線到10.105.29.1(server ip) * `sudo ufw delete <rule>`: 刪除一個規則 e.g.sudo ufw delete limit sshsud ## 關機指令 正確關機步驟:`sync -> shutdown` 先儲存再關機 `sync`: 數據從內存存到硬碟 `shutdown -h now` 立刻關機 `shutdown -h 20:20`20:20關機 `shutdown -h +10` 10分鐘後關機 `shutdown -r` = `reboot` `halt` = `shutdown -h now` = `poweroff` ## 忘記root密碼(Ubuntu) 1. 重新開機進入 recovery mode menu * 開機後進入`grub menu` -> * `Advanced options for ubuntu` -> * `Ubuntu...(recovery mode)` * 如果沒自動進到grub menu(e.g. vmware, virtualbox),當virtualbox logo 出現時按著shift 2. 在 recovery mode menu 當中選擇 `root - drop to root shell prompt`選項,畫面底下會出現可以輸入指令的 shell prompt 3. 在shell prompt輸入下列指令,重新掛載root,使其有write access * `mount -rw -o remount /` * root 預設只有 read access, 不能寫入檔案,所以要提供 write access 才能修改密碼 4. 找到所有的使用者名稱 * `ls /home` 5. 重設特定使用者的 password * `passwd <username> * 然後會出現 `Enter new UNIX password:`, `Retype new UNIX password:` 這個時候輸入新密碼,注意,打密碼時,為了保證隱私,螢幕上不會出現任何變化 6. 離開root shell prompt 輸入:`exit` -> 回到 recovery mode menu -> 選 resume 繼續開機 ## 遠程登入 Remote access Linux 伺服器一般都是放在機房內,所以一定要知道怎麼遠程登入~ ### PuTTY (Windows -> Linux) 先Enable SSH on linux 1. `ssh localhost`: 確認ssh連線 * Connection refused 表示 ssh server還沒啟用繼續2. 2. `sudo apt-get install openssh-server`: 安裝openssh server 3. `sudo service ssh status`: 查看ssh狀態,應該要看到 `active (running)` * OR `sudo systemctl status ssh` 5. `ssh localhost`:再次確認ssh連線 6. `sudo ufw allow ssh`: 開啟ssh的防火牆,通常不會這樣設定,因為不太安全 * 比較安全的做法: * port會設定>1024(e.g. 5432) * 指定連線兩端的ip * 限制連線次數 * 設定方式請參考Firewall的command Disable SSH (會開也要會關啊) * Disable ssh: `sudo systemctl disable --now ssh` * Re-enable ssh: `sudo systemctl enable --now ssh` 使用PuTTY遠端連線 1. 到官網下載 PuTTY: https://www.putty.org/ * 一定要在官網下載喔,別的地方下載的可能被開後門,切記 2. 安裝 PuTTY:都維持預設設定 3. 開啟 VM terminal: `ifconfig` 找到ip address 開頭是192.168.XXX.XXX的那個address,記下來 4. 在 Windows 開啟PuTTY(兩台電腦中間有閃電的那個) 5. PuTTY設定當中Basic options for your PuTTY session * Host Name (or IP address): 輸入剛剛在vm上記下的ip * port: 如果沒有另外設定就維持22 (SSH 默認的port) * Connection type: SSH 6. 按連線之後,如果跳出一個有黃色三角形注意圖案的對話框,選no 7. Windows上出現Terminal上面有`login as:`輸入預計登入的使用者名稱,密碼 8. 如果成功連線就可以遠端操作虛擬機了 * 更多設定和知識請參考:https://www.virtualbox.org/manual/ch06.html ### Set up netplan * Host OS: Windows * Guest OS: Ubuntu 20.04 (Virtual box VM) 需要 2 Network adapters: 1. Host only network adapter: 設定static IP address (host to guest access) 2. NAT network adapter: access to outside world 1. 到 VM: 更新、升級、關機 * `sudo apt update` * `sudo apt upgrade` * `sudo shutdown -h now` 2. VM 設定 * Settings -> Network -> * `Adapter1` -> * Enable network adpater * Attached to: NAT * `Adapter2` -> * Enable network adpater * Attached to: Host-only Adapter * Name: VirtualBox Host-Only Ethernet Adapter * 選 OK 完成設定 3. Netplan 設定 * Netplan 是 Ubuntu 使用的 network management tool (18.04 之後) * 以下是操作流程 * `ip a`: 查看network interface的狀態,設定完之後可以看到改變 * `cd /etc/netplan`: 到netplan資料夾 * `ls -la`: 列出所有的檔案 * `sudo cp 01-netcfg.yaml 01-netcfg.yaml.copy0413`: 備份一份netplan設定檔,備份檔用一個不同的名字,我是在檔名後面加上.copy<日期>,之後還原時改回原本的檔名即可 **注意01-netcfg.yaml是ubuntu 20.04 server netplan 設定檔的檔名,如果使用其他OS版本請自行上網查詢** * `nano 01-netcfg.yaml`: 用文字編輯器修改設定檔,可以用任何一種文字編輯器,這裡用nano,打入以下設定(#開頭的是我的註解,可以不用打): ```yaml network: version: 2 renderer: networkd # On desktop version # renderer: NetworkManager ethernets: enp0s3: dhcp4: yes enp0s8: # 192.168.56.108 is the ip we chosen, # 24 is the network mask addresses: [192.168.56.108/24] dhcp4: no # you can also provide gateway ip # gateway4: 192.168.56.1 ``` * `sudo netplan try`: 確認設定正確可用,如果錯了會在120秒後自動還原設定 * `sudo netplan apply`: 套用新設定 * `sudo netplan -d apply`: 如果套用設定錯誤,想要看錯誤訊息 * `sudo systemctl restart network-manager` or Ubuntu`sudo systemctl restart system-networkd`: 重新啟動網路服務 * `ip a`: 看看設定完成後的network interface ### OpenSSH client (Linux -> Linux) `sudo apt-get install openssh-server` `sudo service ssh status` ### 更簡單的方法 #### VNC (Virtual Network Computing) #### Anydesk
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up