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

2020年2月6日 星期四

Ubuntu DHCP Server

sudo apt install isc-dhcp-server
修改 DHCP 設定檔
nano /etc/dhcp/dhcpd.conf
default-lease-time 3600;
max-lease-time 28800;

subnet 192.168.1.0 netmask 255.255.255.0 {
 range 192.168.1.121 192.168.1.220;
 option subnet-mask 255.255.255.0;
 option routers          192.168.1.254;
}
重啟 DHCP SERVER
sudo systemctl restart isc-dhcp-server isc-dhcp-server6
檢查是否有啟動成功
sudo systemctl status isc-dhcp-server isc-dhcp-server6
sudo netstat -tulnp | grep dhcp

2020年1月31日 星期五

ubuntu 19.10 建立 Sftp Server 設定

記錄如何在 Ubuntu Linux 上建立一個 SFTP Server,需滿足以下幾點條件:

使用者不可經由 SSH 登入
使用者可經由 SFTP 登入
使用者只可在指定目錄活動
使用者可上傳下載指定目錄內的檔案

環境
Ubuntu 19.10
以下操作皆默認 root 操作,如果你不是使用 root 操作,請在前面加上 sudo。

ubuntu 19.10本身就內建了 Sftp Server,所以只要做一些設定即可。
sftp 群組:sftp-users
sftp 使用者:sftp_user_XXX

新增 SFTP 群組
addgroup sftp-users
確認 group 有正確被建立
grep 'sftp-users' /etc/group

建立 SFTP 使用者
useradd -g sftp-users -s /sbin/nologin sftp_user_XXX
-g: 預設 group
-s: 預設 shell, nologin shell 一般用於系統帳號, 表示不可經由 ssh 登入
確認使用者有正確被建立
grep sftp_user_XXX /etc/passwd
如果使用者早已建立, 可直接更改使用者設定即可
usermod -g sftp-users -d /upload -s /sbin/nologin sftp_user_XXX
建立使用者的目錄
install -d -o sftp_user_XXX -g sftp-users /home/sftp_user_XXX/www
變更使用者的密碼
passwd sftp_user_XXX

SSH 設定
nano /etc/ssh/sshd_config

註解並新增
#Subsystem sftp /usr/lib/openssh/sftp-server 《==加#
Subsystem sftp internal-sftp

PasswordAuthentication yes
PermitRootLogin no
Match Group sftp-users
 ChrootDirectory /home/%u
 X11Forwarding no
 AllowTcpForwarding no
 PermitTTY no
 ForceCommand internal-sftp

重新啟動 sss 服務
systemctl restart ssh

PS:如有建立 web 目錄,記得要變更 user 目錄底下的權限
chown -R sftp_user_XXX:sftp-users www
chmod -R 755 www

2019年12月27日 星期五

ubuntu 使用 timedatectl 更改 Timezone 時區以及

在 ubuntu 裡內建了時間同步指令 timedatectl 及 timesyncd,來取代 ntp 和 ntpdate ,達到系統時間網路同步。
可以用 date 命令查看系統的當前時區:
date
或在 /etc/timezone 文件中查找當前時區。
cat /etc/timezone
若想更改時區,可先用以下指俴取得 timezone 列表:
timedatectl list-timezones
但 Timezone 列表十分長, 用 grep 過濾會較方便, 以下只會印出亞洲區的時區:
timedatectl list-timezones | grep Asia
找到要設定的時區後,便可以設定: 台北時區:
timedatectl set-timezone Asia/Taipei
香港時區:
timedatectl set-timezone Asia/Hong_Kong
設定好後, 可以用 timedatectl 指令檢查時區是否設定正確,時鐘是否與網際網路同步。只需運行:
timedatectl
如你所見,timedatectl 命令顯示本地時間、世界時、時區以及系統時鐘是否與網際網路伺服器同步,以及 systemd-timesyncd.service 是處於活動還是非活動狀態,等等。
如果 System clock synchronized: no 則表示時鐘不同步,那麼 timesyncd 服務可能處於非活動狀態。因此,只需重啟服務並看下是否正常。

sudo systemctl restart systemd-timesyncd.service
然後檢查 timesyncd 服務狀態:
sudo systemctl status systemd-timesyncd.service
如果此服務已啟用並處於活動狀態,那麼系統時鐘應與網際網路時間伺服器同步。 你可以使用命令驗證是否啟用了時間同步:
timedatectl
如果仍然不起作用,請運行以下命令以啟用時間同步:
sudo timedatectl set-ntp true

Ubuntu 開啟開放 root 登入 ssh

基於安全理由,Ubuntu 預設不能使用 root 登入 ssh,但如果是內部測試的主機,為了方便使用,可以用以下方法修改解除 root 登入限制:

首先要重新設定 Ubuntu 的 root 密碼, 輸入以下指令:

sudo passwd root
修改了 root 密碼後,開啟 sshd 的設定檔:
nano /etc/ssh/sshd_config
找到以下一行:
PermitRootLogin prohibit-password
改為:
PermitRootLogin yes

重新啟動 sshd:
sudo systemctl restart sshd
重新啟動 sshd 後,便可以用 root 帳號發入 ssh。

*注意:如果有設定防火牆,記得把 SSH port:22/TCP (限定叢集中所有節點與管理者連線來源 IP)打開。

ubuntu 19.1 安裝 mysql 8 服務

一如往常,先更新 apt-get
sudo apt-get update
安裝 MySQL 8.0
sudo apt-get install mysql-server
這邊使用 netstat 指令來查看, netstat 的功用是顯示通訊協定統計資料以及目前的TCP/IP 網路連線,所以要使用它來查看已安裝好的 MySQL 是否有連線監聽。此指令要另外安裝,安裝指令如下:
sudo apt-get install net-tools
安裝完成後就能透過 netstat 指令來查看 MySQL 是否安裝成功:
sudo netstat -tap | grep mysql
設置 MySQL 允許遠端訪問
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir  = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address  = 127.0.0.1 <== 註解起來
#
# * Fine Tuning
#
key_buffer_size  = 16M
# max_allowed_packet = 64M
# thread_stack  = 256K
進入 MySQL 服務,一開始密碼為空值
mysql -u root -p
CREATE USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '12345678';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';

CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '12345678';

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '12345678';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

FLUSH PRIVILEGES;
其他相關

ubuntu mysql 8 的資料庫預設路徑為 /var/lib/mysql
若變更或掛載了新路徑, 記得要改權限
chmod -R 700 /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
查詢mysql資料庫檔案的存放位置
在mysql資料庫中,有時候並不是很容易找出mysql資料庫檔案data的存放位置,這時就可以使用mysql的命令列工具進行查詢.
show variables like '%datadir%';

2019年12月24日 星期二

Ubuntu19.1 安裝 LEMP(Linux+Nginx+PHP)環境建立


Step 1 : 安裝 Nginx Web Server

sudo apt-get update

sudo apt-get install nginx

在瀏覽器輸入 http://server_ip 出現下圖,即表示安裝成功

Step 2 : 安裝php及設定php.ini
sudo apt install php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-gd php8.1-curl

2. 修改php.ini (Modifying php.ini file) :
sudo nano /etc/php/8.1/fpm/php.ini

找到
;cgi.fix_pathinfo=1
改成
cgi.fix_pathinfo=0

存檔並重啟 php 服務
sudo service php8.1-fpm restart

然後要設定讓nginx能夠執行PHP腳本 輸入以下指令修改nginx設定
sudo nano /etc/nginx/sites-available/default

我們將其改成如下 (紅色表示變動部分)
server {
    listen 80 default_server;
    listen [::]:80 default_server;
 
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
 
    server_name _;
 
    location / {
        try_files $uri $uri/ =404;
    }
 
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }
 
    location ~ /\.ht {
        deny all;
    }
}

修改完後儲存 並輸入以下指令檢查設定檔格式是否沒有錯誤
sudo nginx -t

檢查沒有錯誤就輸入指令重新載入nginx設定
sudo systemctl reload nginx

Ubuntu 19.1 安裝 samba 並使用 SMBv2 版本

1. 安裝samba服務
sudo apt update

sudo apt install samba
2. 修改samba設定檔
nano /etc/samba/smb.conf
[global]

## Browsing/Identification ###

# Change this to the workgroup/NT-domain name your Samba server will part of
   workgroup = WORKGROUP
   security = user 
# 最小支援的SMBv2版本
   server min protocol = SMB2 (or NT1)
# server string is the equivalent of the NT Description field
   server string = %h server (Samba, Ubuntu)

#### Networking ####

# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
;   interfaces = 127.0.0.0/8 eth0
interfaces = 127.0.0.0/8 192.168.0.0/16
# Cap the size of the individual log files (in KiB).
;   max log size = 1000
max log size = 100
#======================= Share Definitions =======================
[root]
   comment = Root Directories
   path = /
   writeable = yes
   browseable = yes
   valid users = root

[www]
   comment = www
   path = /var/www
   writeable = yes
   browseable = yes
   valid users = root

# Un-comment the following (and tweak the other settings below to suit)
# to enable the default home directory shares. This will share each
# user's home directory as \\server\username

並把以下註解掉
;[printers]
;   comment = All Printers
;   browseable = no
;   path = /var/spool/samba
;   printable = yes
;   guest ok = no
;   read only = yes
;   create mask = 0700

# Windows clients look for this share name as a source of downloadable
# printer drivers
;[print$]
;   comment = Printer Drivers
;   path = /var/lib/samba/printers
;   browseable = yes
;   read only = yes
;   guest ok = no
3. 重啟 samba 服務
sudo systemctl restart smbd nmbd
4. 建立 Samba 使用者
sudo smbpasswd -a root
其他相關用法

若有修改設定檔 smb.conf,讓 samba 重新載入設定檔,可以使用以下指令,不需重啟 samba 服務
sudo systemctl reload smbd nmbd
檢查設定檔是否有誤
testparm
查看 samba 連線狀態
sudo smbstatus