內容選單標籤

2025年3月27日 星期四

Ubuntu24.04.2

系統時間呈現秒數 
設定-->系統-->日期與時間-->秒數

安裝chrome
下載/google-chrome-stable_current_amd64.deb
~$ ls 下載/google-chrome-stable_current_amd64.deb 
~$ ls ./下載/google-chrome-stable_current_amd64.deb
~$ sudo apt install ./下載/google-chrome-stable_current_amd64.deb 

 

 下載/google-chrome-stable_current_amd64.deb
-->右鍵-->使用AppCenter開啟-->安裝

 

更新
顯示程式-->軟體更新

 $ sudo apt update

$ sudo apt upgrade

 

APT、 dpkg、snap

apt 是 Ubuntu 中常用的套件管理指令    //Advanced Package Tool

dpkg 是直接管理 Debian 套件的低階指令

snap 是 Ubuntu 引入的新一代套件管理系統 

-----------------------------------------------------------------------

# sudo apt update    //更新軟體庫清單

$ apt list --upgradable        

# sudo apt upgrade     //升級系統或軟體

 # sudo apt search vlc    //搜尋特定應用程式

$ sudo apt install vlc    //安裝應用程式

$ apt list    //列出系統中的套件資訊
$ apt list --installed    //只顯示已安裝的套件

 $ sudo apt show vlc   //列出應用程式的詳細資訊
[sudo] kk 的密碼:
Package: vlc
Version: 3.0.20-3build6
Priority: optional
...

 $ sudo apt remove vlc    //移除應用程式

 $ sudo apt purge vlc    //移除應用程式及所有設定檔


                                                                                                                            

For Python IDLE

~$ python3 --version
Python 3.12.3

~$ python3
Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(3+6)
9

CTRL+D    or  CTRL+Z   //離開
 
 
$ sudo apt install idle3
 

 $ apt list -a idle
正在列出... 完成
idle/noble-updates,now 3.12.3-0ubuntu2 all [已安裝,自動]
idle/noble 3.12.3-0ubuntu1 all

$ apt list --installed | grep idle


~$ apt show -a idle
Package: idle
Version: 3.12.3-0ubuntu2
Priority: optional
Section: universe/python
Source: python3-defaults
...
 
顯示程式-->IDLE


切換admin

$ sudo su
[sudo] kk 的密碼:

root@kk-VMware-Virtual-Platform:/home/kk# exit 或 CTRL+D
exit


查看cpu、ram、process
$ top

CTRL+Z    //離開

 

查看記憶體
~$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       3.4Gi       1.8Gi       176Mi       2.9Gi       4.3Gi
置換:            0B          0B          0B


查看硬碟
~$ df -h
檔案系統        容量  已用  可用 已用% 掛載點
tmpfs           790M  2.1M  788M    1% /run
/dev/sda2        20G  8.9G  9.7G   48% /
tmpfs           3.9G   92M  3.8G    3% /dev/shm
tmpfs           5.0M  8.0K  5.0M    1% /run/lock
tmpfs           790M  148K  790M    1% /run/user/1000
/dev/sr0        5.8G  5.8G     0  100% /media/kk/Ubuntu 24.04.1 LTS amd64


打包 tar
 //c:create建立壓縮檔案 v:verbose輸出處理的檔案列表 f:archive file指定壓縮檔案
$ tar -cvf aa.tar a1.txt a2.txt   
a1.txt
a2.txt

//列出 .tar 壓縮檔案的內容
$ tar -tf aa.tar
a1.txt
a2.txt

//解壓縮 .tar 壓縮檔案
$ cp aa.tar 文件

$ cd 文件

$ ls
aa.tar
 

//x:extract
$ tar -xvf aa.tar
a1.txt
a2.txt
$ ls
a1.txt  a2.txt  aa.tar


 
----------------------------------------------------------------------------
Disk Mount

 ~$ sudo fdisk -l                # manipulate disk partition table
...
Disk /dev/sdb: 149.05 GiB, 160041885696 bytes, 312581808 sectors
Disk model: WDC WD1600JS-22M
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: F40B36E3-5EC5-8642-B955-AEC41EB0B612

所用裝置   Start      結束      磁區  Size 類型
/dev/sdb1   2048 312581774 312579727  149G Linux filesystem
...


$ sudo blkid /dev/sdb1                # locate/print block device attributes
/dev/sdb1: UUID="f4f0b2ec-3e02-408d-bc5f-62b8a2612403" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="05c312c1-a6d8-634b-a30e-02c147ae7890"

~$ sudo mkdir /mnt/wdc01
~$ ls -l /mnt/wdc01
總用量 0

~$ sudo mount /dev/sdb1 /mnt/wdc01
~$ ls -l /mnt/wdc01
總用量 892600

 ~$ sudo umount /mnt/wdc01
~$ ls -l /mnt/wdc01
總用量 0
 

 

 列出掛載

~$ mount
...
/dev/sdb1 on /mnt/wdc01 type ext4 (rw,relatime)

 

~$ lsblk
...
sdb      8:16   0 149.1G  0 disk 
└─sdb1   8:17   0   149G  0 part /mnt/wdc01

 

~$ df -h
檔案系統        容量  已用  可用 已用% 掛載點
tmpfs           765M  1.9M  764M    1% /run
/dev/sda3       219G   43G  166G   21% /
tmpfs           3.8G     0  3.8G    0% /dev/shm
tmpfs           5.0M  8.0K  5.0M    1% /run/lock
/dev/sda2       512M  6.2M  506M    2% /boot/efi
tmpfs           765M  128K  765M    1% /run/user/1000
/dev/sdb1       146G  872M  138G    1% /mnt/wdc01

 

~$ cat /etc/mtab
...
/dev/sdb1 /mnt/wdc01 ext4 rw,relatime 0 0 

 


----------------------------------------------------------------------------
永久掛載磁碟(開機自動掛載)

$ sudo blkid
... 
/dev/sdb1: UUID="f4f0b2ec-3e02-408d-bc5f-62b8a2612403" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="05c312c1-a6d8-634b-a30e-02c147ae7890"

~$ lsblk -f
...
sdb                                                                             
└─sdb1 ext4     1.0         f4f0b2ec-3e02-408d-bc5f-62b8a2612403  137.3G     1% /mnt/wdc01

 

~$ ls -l /etc/fstab
-rw-rw-r-- 1 root root 665 12月 18  2022 /etc/fstab
~$ sudo cp /etc/fstab /etc/fstab.bk
~$ ls -l /etc/fstab*
-rw-rw-r-- 1 root root 665 12月 18  2022 /etc/fstab
-rw-r--r-- 1 root root 665  7月 11 11:11 /etc/fstab.bk

~$ sudo nano /etc/fstab
...
UUID=f4f0b2ec-3e02-408d-bc5f-62b8a2612403  /mnt/wdc01  ext4  defaults  0  0

 #最底下加入

 

UUID=<磁碟UUID>  <掛載點>  <文件系統類型>  <選項>  <備份>  <檢查>
 UUID:    磁碟的唯一識別碼    用 blkid 查詢
掛載點:    磁碟要掛載到的目錄    如 /mnt/data
文件系統類型:    磁碟的格式    ext4, ntfs, xfs, fat32
選項:    掛載參數    defaults(包含 rw,suid,dev,exec,auto,nouser,async)
備份:    是否用 dump 備份    0(不備份)
檢查:    開機時是否用 fsck 檢查    0(不檢查)、1(根目錄)、2(其他分區)

# 測試 fstab 是否正確,如果沒有錯誤訊息,表示 fstab 配置正確。
~$ sudo mount -a

 

重啟驗證
sudo reboot

開機後檢查是否自動掛載:
~$ df -h  # 查看掛載狀態
~$ lsblk -f  # 確認分區掛載情況 

 

卸載磁碟
~$ sudo umount /mnt/wdc01
並刪除 /etc/fstab 中的對應行。 

 

 
----------------------------------------------------------------------------
How to Delete All Partitions and Create a New Single Partition in Ubuntu

 ~$ lsblk
...
sdb      8:16   0 465.8G  0 disk 
├─sdb1   8:17   0   563M  0 part 
└─sdb2   8:18   0    59G  0 part 

~$ sudo gdisk /dev/sdb
[sudo] kk 的密碼: 
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: present

Found valid MBR and GPT. Which do you want to use?
 1 - MBR
 2 - GPT
 3 - Create blank GPT


Command (? for help): ?
b    back up GPT data to a file
c    change a partition's name
d    delete a partition
i    show detailed information on a partition
l    list known partition types
n    add a new partition
o    create a new empty GUID partition table (GPT)
p    print the partition table
q    quit without saving changes
r    recovery and transformation options (experts only)
s    sort partitions
t    change a partition's type code
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu

Command (? for help): p
Disk /dev/sdb: 976773168 sectors, 465.8 GiB
Model: ST500DM002-1BD14
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 4B1314EA-9E88-4AF9-8462-947882E67B13
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 976773134
Partitions will be aligned on 2048-sector boundaries
Total free space is 976773101 sectors (465.8 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

Command (? for help): x

Expert command (? for help): z
About to wipe out GPT on /dev/sdb. Proceed? (Y/N): y
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
Blank out MBR? (Y/N): y

~$ lsblk
...
sdb      8:16   0 465.8G  0 disk

~$ sudo gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries in memory.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-976773134, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-976773134, default = 976773119) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): p
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 976773168 sectors, 465.8 GiB
Model: ST500DM002-1BD14
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 6A4DEE9A-E958-4951-B9A9-9D733941AD14
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 976773134
Partitions will be aligned on 2048-sector boundaries
Total free space is 2029 sectors (1014.5 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048       976773119   465.8 GiB   8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

~$ lsblk
...
sdb      8:16   0 465.8G  0 disk 
└─sdb1   8:17   0 465.8G  0 part 

~$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 122096384 4k blocks and 30531584 inodes
Filesystem UUID: 694e2a5e-c728-4b41-8913-6b032e7bd22d
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done     

~$ lsblk -f
...
sdb                                                                         
└─sdb1
     ext4   1.0         694e2a5e-c728-4b41-8913-6b032e7bd22d  

~$ blkid
/dev/sda3: UUID="93c62a33-7ed4-4360-8b04-039d04dd41cb" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="4efee950-a548-4e1d-ae82-f113bc1f310f"


----------------------------------------------------------------------------
Gdisk create partition and formmat filesystem ext4 and delete

 ~$ sudo gdisk /dev/sdb
[sudo] kk 的密碼: 
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): ?
b    back up GPT data to a file
c    change a partition's name
d    delete a partition
i    show detailed information on a partition
l    list known partition types
n    add a new partition
o    create a new empty GUID partition table (GPT)
p    print the partition table
q    quit without saving changes
r    recovery and transformation options (experts only)
s    sort partitions
t    change a partition's type code
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-976773134, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-976773134, default = 976773119) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 976773168 sectors, 465.8 GiB
Model: ST500DM002-1BD14
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 6A4DEE9A-E958-4951-B9A9-9D733941AD14
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 976773134
Partitions will be aligned on 2048-sector boundaries
Total free space is 2029 sectors (1014.5 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048       976773119   465.8 GiB   8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

~$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
/dev/sdb1 contains a ext4 file system
    last mounted on Tue Jul 15 11:42:07 2025
Proceed anyway? (y,N) y
Creating filesystem with 122096384 4k blocks and 30531584 inodes
Filesystem UUID: a760efbc-223b-4654-bc51-a7024b6d9c1b
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done  

~$ lsblk -f
...
sdb                                                                             
└─sdb1 ext4     1.0         a760efbc-223b-4654-bc51-a7024b6d9c1b 

               
~$ blkid
/dev/sda3: UUID="93c62a33-7ed4-4360-8b04-039d04dd41cb" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="4efee950-a548-4e1d-ae82-f113bc1f310f"

/dev/sdb1: UUID="694e2a5e-c728-4b41-8913-6b032e7bd22d" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="36b27912-d9bd-4b70-8f22-1c5deab4e49f"
 

~$ sudo gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): d
Using 1

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.


~$ lsblk -f
...
sdb                                                                         
 

~$ blkid
/dev/sda3: UUID="93c62a33-7ed4-4360-8b04-039d04dd41cb" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="4efee950-a548-4e1d-ae82-f113bc1f310f"
 

 

----------------------------------------------------------------------------
檢查USB隨身碟是GPT還是MBR及轉換為MBR並格式化為FAT且複製大量資料

~$ lsblk -l
...
sdd      8:48   1   3.8G  0 disk 
sdd1     8:49   1   3.8G  0 part /media/kk/CarMusic
 

~$ sudo parted -l
...
型號:USB 2.0 USB Flash Drive (scsi)
磁碟 /dev/sdd:4043MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點    終點    大小    檔案系統  名稱              旗標
 1    1049kB  4042MB  4041MB  fat32     Linux filesystem
 

~$ sudo gdisk /dev/sdd
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Command (? for help): ?
b    back up GPT data to a file
c    change a partition's name
d    delete a partition
i    show detailed information on a partition
l    list known partition types
n    add a new partition
o    create a new empty GUID partition table (GPT)
p    print the partition table
q    quit without saving changes
r    recovery and transformation options (experts only)
s    sort partitions
t    change a partition's type code
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu
 

Command (? for help): r

Recovery/transformation command (? for help): ?
b    use backup GPT header (rebuilding main)
c    load backup partition table from disk (rebuilding main)
d    use main GPT header (rebuilding backup)
e    load main partition table from disk (rebuilding backup)
f    load MBR and build fresh GPT from it
g    convert GPT into MBR and exit
h    make hybrid MBR
i    show detailed information on a partition
l    load partition data from a backup file
m    return to main menu
o    print protective MBR data
p    print the partition table
q    quit without saving changes
t    transform BSD disklabel partition
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu

Recovery/transformation command (? for help): g

MBR command (? for help): ?
a    toggle the active/boot flag
c    recompute all CHS values
l    set partition as logical
o    omit partition
p    print the MBR partition table
q    quit without saving changes
r    set partition as primary
s    sort MBR partitions
t    change partition type code
w    write the MBR partition table to disk and exit

MBR command (? for help): w

Converted 1 partitions. Finalize and exit? (Y/N): y
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.

 

~$ sudo parted -l
...
型號:USB 2.0 USB Flash Drive (scsi)
磁碟 /dev/sdd:4043MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點    終點    大小    類型     檔案系統  旗標
 1    1049kB  4042MB  4041MB  primary  fat32
 

~$ lsblk -l
...
sdd      8:48   1   3.8G  0 disk 
sdd1     8:49   1   3.8G  0 part /media/kk/CarMusic
 

~$ sudo mkfs.fat /dev/sdd1
mkfs.fat 4.2 (2021-01-31)
mkfs.fat: /dev/sdd1 contains a mounted filesystem.


~$ lsblk -l
...
sdd      8:48   1   3.8G  0 disk 
sdd1     8:49   1   3.8G  0 part /media/kk/CarMusic


~$ sudo mount /dev/sdd1 /mnt/disk01

~$ df -Th /mnt/disk01
檔案系統       類型  容量  已用  可用 已用% 掛載點
/dev/sdd1      vfat  3.8G   12M  3.8G    1% /mnt/disk01
 

 

~$ rsync -avh --progress /mnt/disk02/New/kkMusic_202507/2025/ /mnt/disk01/2025/
sending incremental file list
created directory /mnt/disk01/2025
./
 

 

----------------------------------------------------------------------------
複製大量資料資料夾
$ rsync -avh --progress kkMusic_202008/ new02/kkMusic-02
$ cd new02/
$ ls -al
總用量 148
drwxrwxr-x 4 kk   kk    4096  7月 22 15:14 .
drwxrwxrwx 8 root root  4096  7月 22 15:04 ..
drwxr-xr-x 3 kk   kk   73728  7月 17 15:34 kkMusic-02


----------------------------------------------------------------------------
刪除大量資料資料夾
$ ls -al kkMusic-02/
總用量 3855908
drwxr-xr-x 3 kk kk    73728  7月 17 15:34  .
drwxrwxr-x 4 kk kk     4096  7月 22 15:14  ..
drwxr-xr-x 2 kk kk    36864  7月 17 15:35  0919
$ rm -r kkMusic-02/0919/


----------------------------------------------------------------------------
刪除大量資料中*.jpg
$ rm *.jpg
 

----------------------------------------------------------------------------
大量資料中檔名含有空白的部份刪除
$ ls
'0_Piano Relajante Music.mp3'
...

 $ for f in *; do mv "$f" $(echo "$f" | tr -d ' '); done
$ ls
 0_PianoRelajanteMusic.mp3
...


----------------------------------------------------------------------------
ubuntu刪除大量檔案的檔案名稱中的"("和")"
$ ls
...
'LETITBE-(BEATLES).MP3'
... 

$ find . -name '*[()]*' -exec bash -c 'mv "$0" "${0//[()]/}"' {} \;
$ ls
...
LETITBE-BEATLES.MP3
...

 

----------------------------------------------------------------------------
ubuntu刪除大量檔案的檔案名稱中的"'"
$ ls
...
"月光女神-HEDOESN'TSEEME.MP3"

$ for f in *"'"*; do mv -- "$f" "${f//\'/}"; done
$ ls
...
月光女神-HEDOESNTSEEME.MP3


----------------------------------------------------------------------------
先備份

$ mkdir -p kkMusic-bk

 $ ls -l
總用量 72
drwxrwxr-x 2 kk kk  4096  7月 23 11:52 kkMusic-bk
drwxr-xr-x 2 kk kk 69632  7月 22 17:13 kkMusic-02

$ rsync -avh --progress kkMusic-02/ kkMusic-bk/

 

----------------------------------------------------------------------------
ubuntu 大量檔案,檔名中有"&"更改為"+"

$ cd kkMusic-02
$ ls
...

'李宗盛&林憶蓮_當愛已成往事.mp3'

$ find . -type f -name "*&*" | while read -r file; do
  newname=$(echo "$file" | sed 's/&/+/g')
  mv "$file" "$newname"

$ ls
李宗盛+林憶蓮_當愛已成往事.mp3
 

 

----------------------------------------------------------------------------
ubuntu大量檔案,檔案名稱副檔名"*.MP3"或"*.Mp3"或"*.mP3"都改成"*.mp3" 

$ ls
...
月光女神-AWHITERSHADEOFPALE.MP3 

 $ for file in *.[mM][pP]3; do     mv "$file" "${file%.*}.mp3"; done

$ ls
...
月光女神-AWHITERSHADEOFPALE.mp3
 

 

 ----------------------------------------------------------------------------
在 Ubuntu 中批量將檔案名稱中第一個出現的 "_" 改為 "-"

$ for file in *; do
  newname=$(echo "$file" | sed 's/_/-/')
  if [ "$newname" != "$file" ]; then
    mv -- "$file" "$newname"
  fi
done
  

$ ls
...
張信哲-弦歌有你_1_01愛如潮水.mp3


 ----------------------------------------------------------------------------
ubuntu 檔案名稱中前2個是英文字母,在原檔名前加上 "Eng-"

 $ ls
...
Adele-Hello.mp3

$ for file in [a-zA-Z][a-zA-Z]*; do
    if [[ -f "$file" ]]; then
        mv "$file" "Eng-$file"
    fi
done


$ ls
Eng-Adele-Hello.mp3
 

 

  ----------------------------------------------------------------------------
ubuntu系統如何將大量mp3檔中,權限設置不是"-rw-rw-r--"改成"-rw-rw-r--"

 

$ find -type f -name "*.mp3" ! -perm 664 -exec chmod 664 {} \;


$ ls -al
總用量 4488232
...
-rw-rw-r-- 1 kk kk   9663264  7月 17 12:29 Eng-Adele-Hello.mp3
 

 

 

 

  ----------------------------------------------------------------------------
ubntu 大量MP3檔案中,以檔名前2個字為篩選條件,相同者是同一個群集,如何在相同群集前加上3碼的編號。譬如:001_伍佰-你是我的花朵.mp3,001_伍佰-夢醒時分.mp3.......002_信樂團-OneNightIn北京.mp3.....003_光頭華夏-无期.mp3...。也就是不同群集有不同編號。

$ vi rename_clusters.sh
 #!/bin/bash

declare -A cluster_numbers
counter=1

# 處理所有大小寫變化的MP3文件
for file in *.[mM][pP]3; do
    # 統一轉換為小寫比較,並取前2個字元
    lower_file=$(echo "$file" | tr '[:upper:]' '[:lower:]')
    cluster_key=${lower_file:0:2}
    
    if [[ -z "${cluster_numbers[$cluster_key]}" ]]; then
        cluster_numbers[$cluster_key]=$(printf "%03d" $counter)
        ((counter++))
    fi
    
    cluster_number=${cluster_numbers[$cluster_key]}
    
    # 檢查是否已經有編號前綴
    if [[ "$file" != "${cluster_number}_"* ]]; then
        echo "重命名: $file → ${cluster_number}_$file"
        mv -- "$file" "${cluster_number}_$file"
    fi
done

echo "處理完成。"

 

 $ ls -l rename_clusters.sh 
-rw-rw-r-- 1 kk kk 894  7月 23 15:19 rename_clusters.sh


$ chmod +x rename_clusters.sh
$ ls -l rename_clusters.sh 
-rwxrwxr-x 1 kk kk 894  7月 23 15:19 rename_clusters.sh

 $ ls
Eng-Adele-Hello.mp3
...

$ ./rename_clusters.sh

$ ls
001_Eng-Adele-Hello.mp3
...



----------------------------------------------------------------------------
大量更改MP3檔的屬性的Title跟檔名一樣

$ for file in *.mp3; do
  title="${file%.mp3}"  # 移除.mp3扩展名
  id3v2 -t "$title" "$file"
done
 



 

----------------------------------------------------------------------------
 如何將磁碟內所有目錄及檔案複製到另一磁碟內的資料夾bk內?

~$ mkdir -p /mnt/disk01/spusb

~$ rsync -avh --progress /mnt/disk02/ /mnt/disk01/spusb/

~$ find /mnt/disk02 | wc -l
476

~$ find /mnt/disk01/spusb | wc -l
476


----------------------------------------------------------------------------
解壓縮到另一資料夾

$ unzip ./LocalKingMap_20250720_3D.zip -d kkLocal

 

 

----------------------------------------------------------------------------
USB 隨身碟分割區改成GPT

 ~$ sudo parted -l
...
型號:Ut165  (scsi)
磁碟 /dev/sdd:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:loop
磁碟旗標:

編號  起點   終點    大小    檔案系統  旗標
 1    0.00B  2022MB  2022MB  fat16


~$ sudo blkid /dev/sdd
/dev/sdd: SEC_TYPE="msdos" UUID="D6E0-DACF" BLOCK_SIZE="512" TYPE="vfat"

 ~$ sudo gdisk /dev/sdd
[sudo] kk 的密碼: 
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present
....

Command (? for help): ?
b    back up GPT data to a file
c    change a partition's name
d    delete a partition
i    show detailed information on a partition
l    list known partition types
n    add a new partition
o    create a new empty GUID partition table (GPT)
p    print the partition table
q    quit without saving changes
r    recovery and transformation options (experts only)
s    sort partitions
t    change a partition's type code
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu


Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y

Command (? for help): p
Disk /dev/sdd: 3948544 sectors, 1.9 GiB
Model:                 
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): D87E3941-C7FB-40DC-987F-379373674D17
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 3948510
Partitions will be aligned on 2048-sector boundaries
Total free space is 3948477 sectors (1.9 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdd.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.


~$ sudo parted -l
...
型號:Ut165  (scsi)
磁碟 /dev/sdd:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點  終點  大小  檔案系統  名稱  旗標

~$ sudo blkid /dev/sdd
/dev/sdd: SEC_TYPE="msdos" UUID="D6E0-DACF" BLOCK_SIZE="512" TYPE="vfat" PTUUID="d87e3941-c7fb-40dc-987f-379373674d17" PTTYPE="gpt"


----------------------------------------------------------------------------
建立分割區大小

~$ sudo gdisk /dev/sdd
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-3948510, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-3948510, default = 3946495) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdd: 3948544 sectors, 1.9 GiB
Model:                 
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): D87E3941-C7FB-40DC-987F-379373674D17
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 3948510
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         3946495   1.9 GiB     8300  Linux filesystem

 

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdd.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.


~$ sudo parted -l
...
型號:Ut165  (scsi)
磁碟 /dev/sdd:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點    終點    大小    檔案系統  名稱              旗標
 1    1049kB  2021MB  2020MB            Linux filesystem

 ~$ sudo blkid /dev/sdd
/dev/sdd: SEC_TYPE="msdos" UUID="D6E0-DACF" BLOCK_SIZE="512" TYPE="vfat" PTUUID="d87e3941-c7fb-40dc-987f-379373674d17" PTTYPE="gpt"

 

 

 

----------------------------------------------------------------------------
格式化分割區

~$ lsblk -l
...
sdd      8:48   1   1.9G  0 disk 
sdd1     8:49   1   1.9G  0 part 

 

~$ sudo mkfs.vfat -F 32 -n "USB_DATA" /dev/sdd1
mkfs.fat 4.2 (2021-01-31)

 

~$ lsblk -l
...
sdd      8:48   1   1.9G  0 disk 
sdd1     8:49   1   1.9G  0 part /media/kk/USB_DATA

~$ sudo parted  -l 
型號:Ut165  (scsi)
磁碟 /dev/sdd:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點    終點    大小    檔案系統  名稱              旗標
 1    1049kB  2021MB  2020MB  fat32     Linux filesystem

~$ sudo blkid /dev/sdd1
/dev/sdd1: LABEL_FATBOOT="USB_DATA" LABEL="USB_DATA" UUID="2532-F879" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="Linux filesystem" PARTUUID="5558af6f-be5f-4f58-baa0-a2fd6005a686" 

 

 

 

 

 

 




----------------------------------------------------------------------------
製作出linux與windows都能讀取資料的USB隨身碟

~$ sudo parted -l
...
型號: USB FLASH DRIVE (scsi)
磁碟 /dev/sdd:4010MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點    終點    大小    檔案系統  名稱              旗標
 1    1049kB  4009MB  4008MB            Linux filesystem

 

~$ lsblk -l
...
sdd      8:48   1   3.7G  0 disk 
sdd1     8:49   1   3.7G  0 part /media/kk/C852-54B5

 

~$ sudo umount /dev/sdd1
 

~$ sudo mkfs.vfat -F 32 -n "USB_DATA" /dev/sdd1
mkfs.fat 4.2 (2021-01-31)

 

~$ sudo parted -l
...
型號: USB FLASH DRIVE (scsi)
磁碟 /dev/sdd:4010MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點    終點    大小    檔案系統  名稱              旗標
 1    1049kB  4009MB  4008MB  fat32     Linux filesystem


~$ lsblk -l
...
sdd      8:48   1   3.7G  0 disk 
sdd1     8:49   1   3.7G  0 part 


~$ sudo blkid /dev/sdd1
/dev/sdd1: LABEL_FATBOOT="USB_DATA" LABEL="USB_DATA" UUID="DC64-50B6" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="Linux filesystem" PARTUUID="82ae7ab9-c4ca-4d2b-997d-ce7cca2e27d7"


 


----------------------------------------------------------------------------
USB隨身碟,GPD轉MBR

 ~$ sudo parted -l
...
型號:Ut165  (scsi)
磁碟 /dev/sdc:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點  終點  大小  檔案系統  名稱  旗標


#完全清除磁碟的分區表
~$ sudo sgdisk -Z /dev/sdc

~$ sudo parted -l
...
型號:Ut165  (scsi)
磁碟 /dev/sdc:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:unknown
磁碟旗標:
 

 

~$ sudo fdisk /dev/sdc

Welcome to fdisk (util-linux 2.39.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0xfc3f7af9.

命令 (m 以獲得說明): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty MBR (DOS) partition table
   s   create a new empty Sun partition table

命令 (m 以獲得說明): o
Created a new DOS (MBR) disklabel with disk identifier 0xba64ff3e.

命令 (m 以獲得說明): w
The partition table has been altered.
Syncing disks.

~$ sudo parted -l
...
型號:Ut165  (scsi)
磁碟 /dev/sdc:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點  終點  大小  類型  檔案系統  旗標
 

 

----------------------------------------------------------------------------
建立新分割區

 ~$ sudo fdisk /dev/sdc
...
命令 (m 以獲得說明): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
分割區編號 (1-4, default 1): 
First sector (2048-3948543, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-3948543, default 3948543): 

Created a new partition 1 of type 'Linux' and of size 1.9 GiB.

 命令 (m 以獲得說明): t
Selected partition 1
Hex code or alias (type L to list all): L

00 空               27 Hidden NTFS Win  82 Linux 交換區/ S  c1 DRDOS/sec (FAT-
01 FAT12            39 Plan 9           83 Linux            c4 DRDOS/sec (FAT-
02 XENIX root       3c PartitionMagic   84 OS/2 hidden or   c6 DRDOS/sec (FAT-
03 XENIX usr        40 Venix 80286      85 Linux 擴展       c7 Syrinx         
04 FAT16 <32M       41 PPC PReP 開機    86 NTFS 卷冊集      da Non-FS 資料    
05 延伸             42 SFS              87 NTFS 卷冊集      db CP/M / CTOS /… 
06 FAT16            4d QNX4.x           88 Linux 純文字     de Dell 公用程式  
07 HPFS/NTFS/exFAT  4e QNX4.x 第二部分  8e Linux LVM        df BootIt         
08 AIX              4f QNX4.x 第三部分  93 Amoeba           e1 DOS 存取       
09 AIX 可開機       50 OnTrack DM       94 Amoeba BBT       e3 DOS R/O        
0a OS/2 開機管理員  51 OnTrack DM6 Aux  9f BSD/OS           e4 SpeedStor      
0b W95 FAT32        52 CP/M             a0 IBM Thinkpad hi  ea Linux extended 
0c W95 FAT32 (LBA)  53 OnTrack DM6 Aux  a5 FreeBSD          eb BeOS fs        
0e W95 FAT16 (LBA)  54 OnTrackDM6       a6 OpenBSD          ee GPT            
0f W95 Ext'd (LBA)  55 EZ-Drive         a7 NeXTSTEP         ef EFI (FAT-12/16/
10 OPUS             56 Golden Bow       a8 Darwin UFS       f0 Linux/PA-RISC  
11 隱藏 FAT12       5c Priam Edisk      a9 NetBSD           f1 SpeedStor      
12 Compaq 診斷      61 SpeedStor        ab Darwin 開機      f4 SpeedStor      
14 隱藏 FAT16 <32M  63 GNU HURD 或 Sys  af HFS / HFS+       f2 DOS 次級       
16 隱藏 FAT16       64 Novell Netware   b7 BSDI fs          f8 EBBR protective
17 隱藏 HPFS/NTFS   65 Novell Netware   b8 BSDI 交換區      fb VMware VMFS    
18 AST 智慧休眠     70 DiskSecure 多種  bb 開機精靈隱藏     fc VMware VMKCORE 
1b 隱藏 W95 FAT32   75 PC/IX            bc Acronis FAT32 L  fd Linux raid 自動
1c 隱藏 W95 FAT32   80 舊版 Minix       be Solaris 開機     fe LANstep        
1e 隱藏 W95 FAT16   81 Minix / 舊版 Li  bf Solaris          ff BBT            
24 NEC DOS        

Aliases:
   linux          - 83
   swap           - 82
   extended       - 05
   uefi           - EF
   raid           - FD
   lvm            - 8E
   linuxex        - 85


Hex code or alias (type L to list all): 0c
Changed type of partition 'Empty' to 'W95 FAT32 (LBA)'.

命令 (m 以獲得說明): w
The partition table has been altered.
Syncing disks.


~$ sudo parted -l
...
 型號:Ut165  (scsi)
磁碟 /dev/sdc:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點    終點    大小    類型     檔案系統  旗標
 1    1049kB  2022MB  2021MB  primary            lba


~$ lsblk -l
...
sdc      8:32   1   1.9G  0 disk /media/kk/D6E0-DACF
sdc1     8:33   1   1.9G  0 part 

 

----------------------------------------------------------------------------
格式化成FAT32

 ~$ lsblk -l
...
 sdc      8:32   1   1.9G  0 disk /media/kk/D6E0-DACF
sdc1     8:33   1   1.9G  0 part 


~$ sudo umount /dev/sdc
~$ lsblk -l
...
sdc      8:32   1   1.9G  0 disk 
sdc1     8:33   1   1.9G  0 part 
 

~$ sudo mkfs.vfat -F 32 -n "USB_DATA" /dev/sdc1
mkfs.fat 4.2 (2021-01-31)

 

~$ sudo parted -l
...
型號:Ut165  (scsi)
磁碟 /dev/sdc:2022MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點    終點    大小    類型     檔案系統  旗標
 1    1049kB  2022MB  2021MB  primary  fat32     lba


~$ sudo blkid /dev/sdc1
/dev/sdc1: LABEL_FATBOOT="USB_DATA" LABEL="USB_DATA" UUID="112B-813A" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="ba64ff3e-01"
 

 

----------------------------------------------------------------------------
使用者家目錄以英文顯示

 ~$ LC_ALL=C xdg-user-dirs-gtk-update
Moving DESKTOP directory from 桌面 to Desktop
Moving DOWNLOAD directory from 下載 to Downloads
Moving TEMPLATES directory from 模本 to Templates
Moving PUBLICSHARE directory from 公共 to Public
Moving DOCUMENTS directory from 文件 to Documents
Moving MUSIC directory from 音樂 to Music
Moving PICTURES directory from 圖片 to Pictures
Moving VIDEOS directory from 影片 to Videos

~$ ls
 Desktop     Public      Documents   Templates   Downloads   Videos    Music     Pictures   

 

---------------------------------------------------------------------------- 
將掛載的磁碟內資料夾,再掛載綁定到使用者家目錄

~$ sudo mount /dev/sdb1 /mnt/disk01

~$ sudo mount --bind /mnt/disk01/MyDownLoad/ ./下載 

~$ ls 下載
張宇-走樣.mp4

~$ ls /mnt/disk01/MyDownLoad/
張宇-走樣.mp4
 

 

---------------------------------------------------------------------------- 
永久掛載

 ~$ sudo cp  /etc/fstab /etc/fstab.bk
~$ ls -l /etc/fstab*
-rw-r--r-- 1 root root 585  7月 23 10:53 /etc/fstab
-rw-r--r-- 1 root root 585  8月  4 09:40 /etc/fstab.bk
 

~$ sudo blkid
...
/dev/sdb1: UUID="9e7b9707-1373-4e9f-947e-b45fe49fdfa0" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="434d78b1-d584-43fc-be07-07ea365e8a87"


~$ sudo nano /etc/fstab
...

UUID=9e7b9707-1373-4e9f-947e-b45fe49fdfa0  /mnt/disk01  ext4  defaults  0  0

/mnt/disk01/MyDownLoad/ /home/kk/下載 none bind 0 0

#最底下加上這2行

 

 

 

 

 


 

 

 

---------------------------------------------------------------------------- 
USB隨身碟GPD轉MBR ,使用gdisk 

~$ sudo parted -l
...
型號: USB FLASH DRIVE (scsi)
磁碟 /dev/sdc:4010MB
磁區大小 (邏輯/物理):512B/512B
分割區:gpt
磁碟旗標:

編號  起點    終點    大小    檔案系統  名稱              旗標
 1    1049kB  4009MB  4008MB  fat32     Linux filesystem
 

$ sudo gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): r
 

Recovery/transformation command (? for help): g

MBR command (? for help): w

Converted 1 partitions. Finalize and exit? (Y/N): y
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.


~$ sudo parted -l 
...
型號: USB FLASH DRIVE (scsi)
磁碟 /dev/sdc:4010MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點    終點    大小    類型     檔案系統  旗標
 1    1049kB  4009MB  4008MB  primary  fat32

 

---------------------------------------------------------------------------- 
USB隨身碟GPD轉MBR ,使用fdisk

~$ sudo fdisk /dev/sdc
...
命令 (m 以獲得說明): o
...
命令 (m 以獲得說明): w
...

 

~$ sudo parted -l
...
型號: USB FLASH DRIVE (scsi)
磁碟 /dev/sdc:4010MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點  終點  大小  類型  檔案系統  旗標

 

 

----------------------------------------------------------------------------  
USB隨身碟GPD轉MBR後,重新建立分割區

~$ sudo fdisk /dev/sdc
...
命令 (m 以獲得說明): n
...
Created a new partition 1 of type 'Linux' and of size 3.7 GiB.
Partition #1 contains a vfat signature.

Do you want to remove the signature? [Y]es/[N]o: y
...

 
命令 (m 以獲得說明): t
Selected partition 1
Hex code or alias (type L to list all): 0c
...
命令 (m 以獲得說明): w
...

 

~$ sudo parted -l
...
型號: USB FLASH DRIVE (scsi)
磁碟 /dev/sdc:4010MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點    終點    大小    類型     檔案系統  旗標
 1    1049kB  4010MB  4009MB  primary            lba
 

 

 

 

----------------------------------------------------------------------------  
USB隨身碟轉MBR、建立分割區後,進行格式化

~$ lsblk -l
...
sdc      8:32   1   3.7G  0 disk 
sdc1     8:33   1   3.7G  0 part /media/kk/USB_DATA

 ~$ sudo umount /dev/sdc1
~$ lsblk -l
...
sdc      8:32   1   3.7G  0 disk 
sdc1     8:33   1   3.7G  0 part 


~$ sudo mkfs.vfat -F 32 -n "USB_DATA" /dev/sdc1

~$ sudo parted -l
...
型號: USB FLASH DRIVE (scsi)
磁碟 /dev/sdc:4010MB
磁區大小 (邏輯/物理):512B/512B
分割區:msdos
磁碟旗標:

編號  起點    終點    大小    類型     檔案系統  旗標
 1    1049kB  4010MB  4009MB  primary  fat32     lba


 

 

---------------------------------------------------------------------------- 
查看CPU與記憶體

~$ lscpu
架構:                    x86_64
  CPU 作業模式:          32-bit, 64-bit
  Address sizes:          36 bits physical, 48 bits virtual
  Byte Order:             Little Endian
CPU(s):                   4
  On-line CPU(s) list:    0-3
...

 

#核心數
~$ nproc
4
 

#記憶體
~$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.5Gi       2.8Gi       2.2Gi       456Mi       3.0Gi       4.7Gi
置換:         2.0Gi          0B       2.0Gi
  

 

---------------------------------------------------------------------------- 


 

檔案與資料夾操作

 Summary Table
----------------------------------------------------------------
Delete a file: rm file                # remove files or directories
Delete an empty directory: rmdir dir            #remove empty directories
Delete a directory & contents: rm -r dir
Force delete (no prompt): rm -rf dir
Rename/move file/dir: mv old new





 

 

2024年12月27日 星期五

Python

---------------------------------dir() 

 dir()

dir(__builtins__)

dir(list)
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] 

 

---------------------------------文字轉Lst

Lst=list("ABCDEF")
Lst
['A', 'B', 'C', 'D', 'E', 'F'] 

Lst=list(dir(list))
Lst
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Lst.index("append")
37
Lst[37:]
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] 

 

---------------------------------help() 

help(list.append)
Help on method_descriptor:

append(self, object, /)
    Append object to the end of the list.

Lst=[i for i in "ABCD"]
Lst
['A', 'B', 'C', 'D']

Lst.append("K")
Lst
['A', 'B', 'C', 'D', 'K']

 help(list.pop)
Help on method_descriptor:

pop(self, index=-1, /)
    Remove and return item at index (default last).

    Raises IndexError if list is empty or index is out of range.

Lst.pop()
'K'
Lst
['A', 'B', 'C', 'D']
Lst.pop(3)
'D'
Lst
['A', 'B', 'C']

 

 ---------------------------------print()format   

for i in range(1,9+1):
    for j in range(1,9+1):
        print("{0}*{1}={2:2d}".format(i,j,i*j),end=" ")
    print()

 
1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 
2*1= 2 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 
3*1= 3 3*2= 6 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 
4*1= 4 4*2= 8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 
5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 
6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 
7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 
8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 
9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81  

2024年12月25日 星期三

Ubuntu24.04.1

  Debian packages  and dependencies

 APT (Advanced Packaging Tool)


~$ dpkg -l | grep apt

~$ sudo apt update    #更新套件資料庫內,軟體版本資訊

~$ sudo apt upgrade    #更新系統上所有已經安裝套件



終端機

ctrl+alt+T 叫出終端機
ctrl+shift+Q    關閉終端機
F11    終端機全螢幕 / 恢復
 
ctrl+shift+C   複製
ctrl+shift+V    貼上
 

$ clear    清除終端機畫面

 

 

 系統時間呈現秒數

~$ gsettings set org.gnome.desktop.interface clock-show-seconds true

 設定-->系統-->日期與時間-->秒數



安裝chrome

下載    google-chrome-stable_current_amd64.deb

~$ ls ./下載/google-chrome-stable_current_amd64.deb
./下載/google-chrome-stable_current_amd64.deb

 ~$ sudo apt install ./下載/google-chrome-stable_current_amd64.deb



標點符號    全形
 中文(新酷音)

中英文輸入切換:微軟建+空白建


ctrl+1     /    ctrl+0   叫出各種符號選單

shift+,    ,
shift+.    。
shift+?    ?  
shift+!    !
shift+:    :
shift+"    ; 
         "    、         


shift+(  (     
shift+)     )
shift+[    『
shift+]    』
shift+z    《
shift+x    》



----------------------------------------------------------------- 開始使用 --------------------------------

查詢 Ubuntu 版本資訊

~$ uname -a
Linux kk-VMware-Virtual-Platform 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Thu Dec  5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux



~$ lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 24.04.1 LTS
Release:    24.04
Codename:    noble


顯示程式-->設定-->系統-->關於



查詢IP

$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d1:a8:ef brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.142.128/24 brd 192.168.142.255 scope global dynamic noprefixroute ens33
       valid_lft 1053sec preferred_lft 1053sec
    inet6 fe80::20c:29ff:fed1:a8ef/64 scope link
       valid_lft forever preferred_lft forever
 


概覽
Windows 鍵


 
在應用程式間切換
Alt + Tab
 

 
最大 / 小 化目前視窗
Windows 鍵 + 方向鍵上 / 下


 
關閉目前視窗
Alt +F4


 
擴展目前視窗到視窗的 左 / 右 半部
Windows 鍵 + 方向鍵左 / 右



切換 工作區(系統桌面的虛擬延伸)
Ctrl + Alt + 方向鍵左 / 右



擷取螢幕畫面

PrintScr


2024年10月3日 星期四

Python 例題

 

def TowerOfHanoi(n,OriginalPillar,DestinationPillar,IntermediaryPillar):
        
    if n==1:
        print("碟 1 從柱",OriginalPillar," 到柱",DestinationPillar)        
        return
    
       
    TowerOfHanoi(n-1,OriginalPillar,IntermediaryPillar,DestinationPillar)
        
    print("碟",n,"從柱",OriginalPillar," 到柱",DestinationPillar)
    
    TowerOfHanoi(n-1,IntermediaryPillar,DestinationPillar,OriginalPillar)

        
#第n個之上(n-1),從A-->B
#第n個        ,從A-->C
#第n個之上(n-1),從B-->C

TowerOfHanoi(3,"A","C","B")
print("完成!")

------------------------------------------

 碟 1 從柱 A  到柱 C
碟 2 從柱 A  到柱 B
碟 1 從柱 C  到柱 B
碟 3 從柱 A  到柱 C
碟 1 從柱 B  到柱 A
碟 2 從柱 B  到柱 C
碟 1 從柱 A  到柱 C
完成!

 ==========================================================

參考解說

https://www.youtube.com/watch?v=oV2C9UINg_U

 

cnt=0
def HanoiTower(n,fro,mid,des):
    global cnt
    if n==1:
        #移動時才累計
        cnt+=1
        #只有一個碟A-->C        
        print("Move",cnt,"disk",n,"from",fro,"to",des)
    else:
        #第n個碟之前(n-1),所有碟A-->B
        HanoiTower(n-1,fro,des,mid)

        #移動時才累計
        cnt+=1
        #第n個碟(n),A-->C
        print("Move",cnt,"disk",n,"from",fro,"to",des)
        
        #第n個碟之前(n-1),所有碟B-->C
        HanoiTower(n-1,mid,fro,des)

HanoiTower(3,"A","B","C")

------------------------------------------

Move 1 disk 1 from A to C
Move 2 disk 2 from A to B
Move 3 disk 1 from C to B
Move 4 disk 3 from A to C
Move 5 disk 1 from B to A
Move 6 disk 2 from B to C
Move 7 disk 1 from A to C
 

========================================================== 

def prime_factorization(n):
    if n==1:
        return[]
    for f in range(2,n+1):
        if n%f==0:
            return [f]+prime_factorization(n//f)

print(prime_factorization(3080))        

------------------------------------------

[2, 2, 2, 5, 7, 11]

 

==========================================================

 

------------------------------------------

==========================================================

 

------------------------------------------

==========================================================

 

------------------------------------------

==========================================================

 

 

 

2024年5月3日 星期五

RockyLinux 9.3 練習(四)

 

檔案系統權限
-----------------------------------------------------------------------------------------

Owner
Group
Other  

r    100    4
w   010    2
x    001    1

每一個檔案均有一個 Owner,僅有 root 與 owner 可以更改檔案存取保護模式 --> chmod
                                                只有 root 可以更改檔案的 Owner 與 Group        -->chown

[kk@Rocky ~]$ ls -l /etc/shadow
----------. 1 root root 730  4月 22 15:44 /etc/shadow

// ---------- 全關,只有 root 可以改

 

[kk@Rocky ~]$ ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 32656  5月 15  2022 /usr/bin/passwd

 密碼在系統檔中,本應只有 root 可以改
為了方便使用者可以自行設定密碼,所以改由使用者執行
即暫時將使用者身分提升至 root,完成工作後再自動降回使用者身分
但若故意將密碼設的太長造成溢位,無法完成設定密碼工作,就會一直在 root 身分

 

 

檔案系統權限
-----------------------------------------------------------------------------------------

 

檔案系統權限
-----------------------------------------------------------------------------------------

 

 

檔案系統權限
-----------------------------------------------------------------------------------------

 

 

檔案系統權限
-----------------------------------------------------------------------------------------

 

 

檔案系統權限
-----------------------------------------------------------------------------------------

2024年4月28日 星期日

RockyLinux 9.3 練習(三)

vi 使用
-------------------------------------------------------------------------------------------------
一般模式:標記、刪除、搬移、複製
G:移到檔案最後。5G:移到檔案第5行。gg:移到檔案開頭
dd:刪除游標所在之行。5dd 表刪除自游標算起 5 行
yy:拷貝游標所在之行至緩衝區。5yy:拷貝游標所在之處以下 5 行至緩衝區。
p:把緩衝區之資料貼上來
u:復原至上一動作。
‧ : 重覆執行上個指令


編輯模式:
i (insert) 進入編輯模式、ESC離開編輯模式


命令模式:
:wq ( 寫入並離開 vi )。:q! ( 強迫離開並放棄編輯的檔案 )
:set nu (列出行號)  :set nonu (不列出行號)
/pattern (往下尋找pattern字串),按"n"找下一個符合字串 ,按"N"找上一個符合字串
?pattern (往上尋找pattern字串),按"n"找上一個符合字串 ,按"N"找下一個符合字串



Firewalld
-------------------------------------------------------------------------------------------------

[kk@BigRocky ~]$ sudo firewall-cmd --state
[sudo] kk 的密碼:
running

[kk@BigRocky ~]$ sudo firewall-cmd --get-zones
block dmz drop external home internal nm-shared public trusted work

[kk@BigRocky ~]$ ls /usr/lib/firewalld/zones
block.xml  dmz.xml  drop.xml  external.xml  home.xml  internal.xml  nm-shared.xml  public.xml  trusted.xml  work.xml

[kk@BigRocky ~]$ sudo firewall-cmd --get-active-zone
public
  interfaces: ens160

[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources:
  services: cockpit dhcpv6-client ssh
  ports:
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:


[kk@BigRocky ~]$ sudo firewall-cmd --get-services | grep ftp
RH-Satellite-6 RH-Satellite-6-capsule afp amanda-client amanda-k5-client amqp amqps apcupsd audit ausweisapp2 bacula bacula-client bb bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc bittorrent-lsd ceph ceph-mon cfengine checkmk-agent cockpit collectd condor-collector cratedb ctdb dhcp dhcpv6 dhcpv6-client distcc dns dns-over-tls docker-registry docker-swarm dropbox-lansync elasticsearch etcd-client etcd-server finger foreman foreman-proxy freeipa-4 freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp galera ganglia-client ganglia-master git gpsd grafana gre high-availability http http3 https ident imap imaps ipfs ipp ipp-client ipsec irc ircs iscsi-target isns jellyfin jenkins kadmin kdeconnect kerberos kibana klogin kpasswd kprop kshell kube-api kube-apiserver kube-control-plane kube-control-plane-secure kube-controller-manager kube-controller-manager-secure kube-nodeport-services kube-scheduler kube-scheduler-secure kube-worker kubelet kubelet-readonly kubelet-worker ldap ldaps libvirt libvirt-tls lightning-network llmnr llmnr-tcp llmnr-udp managesieve matrix mdns memcache minidlna mongodb mosh mountd mqtt mqtt-tls ms-wbt mssql murmur mysql nbd netbios-ns netdata-dashboard nfs nfs3 nmea-0183 nrpe ntp nut openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole plex pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy prometheus prometheus-node-exporter proxy-dhcp ps3netsrv ptp pulseaudio puppetmaster quassel radius rdp redis redis-sentinel rpc-bind rquotad rsh rsyncd rtsp salt-master samba samba-client samba-dc sane sip sips slp smtp smtp-submission smtps snmp snmptls snmptls-trap snmptrap spideroak-lansync spotify-sync squid ssdp ssh steam-streaming svdrp svn syncthing syncthing-gui synergy syslog syslog-tls telnet tentacle tftp tile38 tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-http wbem-https wireguard ws-discovery ws-discovery-client ws-discovery-tcp ws-discovery-udp wsman wsmans xdmcp xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server zerotier

[kk@BigRocky ~]$ ls /usr/lib/firewalld/services | grep ftp
ftp.xml
tftp.xml


>>>>>>>>>>>>>>>>>>>> 將服務加入規則

[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --add-service=ftp
success
[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources:
  services: cockpit dhcpv6-client ftp ssh
  ports:
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:


>>>>>>>>>>>>>>>>>>>>將埠口加入規則
[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --add-port=8080/tcp
success
[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources:
  services: cockpit dhcpv6-client ftp ssh
  ports: 8080/tcp
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:


>>>>>>>>>>>>>>>>>>>>將服務與埠口移除
[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --remove-service=ftp
success

[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --remove-port=8080/tcp
 

success[kk@BigRocky ~]$ sudo firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources:
  services: cockpit dhcpv6-client ssh
  ports:
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:


>>>>>>>>>>>>>>>>>>>>firewalld.service 狀態
[kk@BigRocky ~]$ systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-04-29 13:25:14 CST; 1h 59min ago
       Docs: man:firewalld(1)
   Main PID: 710 (firewalld)
      Tasks: 2 (limit: 48646)
     Memory: 42.5M
        CPU: 2.348s
     CGroup: /system.slice/firewalld.service
             └─710 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid


>>>>>>>>>>>>>>>>>>>>firewalld.service 開機是否啟動
[kk@BigRocky ~]$ systemctl is-enabled firewalld.service
enabled

[kk@BigRocky ~]$ sudo systemctl disable firewalld.service
[sudo] kk 的密碼:
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".

[kk@BigRocky ~]$ systemctl is-enabled firewalld.service
disabled

[kk@BigRocky ~]$ sudo  systemctl enable firewalld.service
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.

[kk@BigRocky ~]$ systemctl is-enabled firewalld.service
enabled 

 

>>>>>>>>>>>>>>>>>>>> 停止FirewallD服務
[kk@BigRocky ~]$ sudo systemctl stop firewalld.service
[kk@BigRocky ~]$ systemctl is-active firewalld.service
inactive

[kk@BigRocky ~]$ sudo systemctl start firewalld.service
[kk@BigRocky ~]$ systemctl is-active firewalld.service
active

>>>>>>>>>>>>>>>>>>>> firewalld.service 目錄位置所在
[kk@BigRocky ~]$ ls /usr/lib/systemd/system | grep firewall
firewalld.service


 

詳細使用手冊
-------------------------------------------------------------------------------------------------

# man man
MAN(1) Manual pager utils MAN(1)
NAME
man - an interface to the on-line reference manuals
SYNOPSIS    //摘要
DESCRIPTION
...

Space】往前捲一個頁面
b】       往後捲一個頁面

PageDown】往前捲半個頁面
PageUp】     往後捲半個頁面

Enter】往下移行一行

【方向鍵】上下左右

/關鍵字】往後搜尋關鍵字,【n】同方向繼續搜尋,【N】反方向繼續搜尋
?關鍵字】往前搜尋關鍵字,【N】同方向繼續搜尋,【n】反方向繼續搜尋

q】離開


[kk@BigRocky ~]$ man --help

[kk@BigRocky ~]$ man -help

[kk@BigRocky ~]$ man -h


行程
-------------------------------------------------------------------------------------------------

[kk@BigRocky ~]$ ps aux | column -t
USER    PID   %CPU  %MEM  VSZ     RSS    TTY    STAT  START  TIME  COMMAND
kk      1423  0.0   0.0   10140   3508   pts/0  R+    14:39  0:00  ps                                 aux                                                                                                                                   
kk      1424  0.0   0.0   5960    2164   pts/0  R+    14:39  0:00  column                             -t      

 

[kk@BigRocky ~]$ ls /
afs  boot  etc   lib    media  opt   root  sbin  sys  usr
bin  dev   home  lib64  mnt    proc  run   srv   tmp  var

#proc 是一種虛擬文件系統。儲存的是當前核心運行狀態的一系列特殊文件,用戶可以通過這些文件查看有關系統硬體及當前正在運行進程的信息。

[kk@BigRocky ~]$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 94
model name      : Intel(R) Core(TM) i3-6100 CPU @ 3.70GHz
...

 

#run 程式或服務啟動後會將其 PID 放置於此,正在執行

[kk@BigRocky ~]$ cat /run/sshd.pid
830
 

[kk@BigRocky ~]$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         830  0.0  0.1  15852  9180 ?        Ss   14:21   0:00 sshd: /usr/sb



更新
-------------------------------------------------------------------------------------------------

[kk@BigRocky ~]$ sudo dnf -y update

 

>>>>>>>>>>>>>>>>>>>>讓設備自動定期更新套件

[kk@BigRocky ~]$ sudo vi /etc/dnf/automatic.conf
...

#apply_updates = no
apply_updates = yes

...

 

[kk@BigRocky ~]$ sudo systemctl start dnf-automatic.timer
 

[kk@BigRocky ~]$ sudo systemctl enable dnf-automatic.timer
Created symlink /etc/systemd/system/timers.target.wants/dnf-automatic.timer → /usr/lib/systemd/system/dnf-automatic.timer.
 

[kk@BigRocky ~]$ systemctl status dnf-automatic.timer
● dnf-automatic.timer - dnf-automatic timer
     Loaded: loaded (/usr/lib/systemd/system/dnf-automatic.timer; enabled; pres>
     Active: active (waiting) since Wed 2024-05-01 15:23:30 CST; 54s ago
      Until: Wed 2024-05-01 15:23:30 CST; 54s ago
    Trigger: Thu 2024-05-02 06:57:32 CST; 15h left
   Triggers: ● dnf-automatic.service


>>>>>>>>>>>>>>>>>>>>看更新紀錄,知道那些套件更新

[kk@BigRocky ~]$ cat /var/log/dnf.rpm.log | more
...

2024-05-01T15:17:08+0800 INFO --- logging initialized ---
2024-05-01T15:17:09+0800 SUBDEBUG Installed: dnf-automatic-4.14.0-8.el9.noarch

[kk@BigRocky ~]$ tail -2 /var/log/dnf.rpm.log
2024-05-01T15:17:08+0800 INFO --- logging initialized ---
2024-05-01T15:17:09+0800 SUBDEBUG Installed: dnf-automatic-4.14.0-8.el9.noarch
 

[kk@BigRocky ~]$ cat /var/log/dnf.rpm.log | grep Installed
2024-04-29T13:56:32+0800 SUBDEBUG Installed: protobuf-c-1.3.3-13.el9.x86_64
...
2024-05-01T15:17:09+0800 SUBDEBUG Installed: dnf-automatic-4.14.0-8.el9.noarch



-------------------------------------------------------------------------------------------------




 

2024年4月25日 星期四

RockyLinux 9.3 練習(二)

 

 迴路介面 loopback interface
  ----------------------------------------------------------------------------------------------------------

 //ifconfig 替換為 ip addr
[kk@rocky ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
...
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:a8:fc:5a brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 163.32.145.69/24 brd 163.32.145.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
...



[kk@rocky ~]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 

[kk@rocky ~]$ ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.052 ms
...



 IP 修改網路設定
  ----------------------------------------------------------------------------------------------------------

>>>>>>>>>>>>>>Tool based:

 //nmcli - command-line tool for controlling NetworkManager
[kk@rocky ~]$ nmcli device status
DEVICE  TYPE      STATE                   CONNECTION
ens160  ethernet  已連線                  ens160
lo      loopback  connected (externally)  lo

[kk@rocky ~]$ nmcli device show ens160
GENERAL.DEVICE:                         ens160
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:A8:FC:5A
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (已連線)
GENERAL.CONNECTION:                     ens160
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveC>
WIRED-PROPERTIES.CARRIER:               開
IP4.ADDRESS[1]:                         163.32.145.69/24
IP4.GATEWAY:                            163.32.145.254
IP4.ROUTE[1]:                           dst = 163.32.145.0/24, nh = 0.0.0.0, mt>
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 163.32.145.254, m>
IP4.DNS[1]:                             168.95.1.1
...

[kk@rocky ~]$ sudo nmcli connect modify ens160 ipv4.address 163.32.145.70/24
[sudo] kk 的密碼:
[kk@rocky ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
...
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:a8:fc:5a brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 163.32.145.69/24 brd 163.32.145.255 scope global noprefixroute ens160
...

[kk@rocky ~]$ sudo nmcli device connect ens160
裝置「ens160」已成功以「f8db373b-ebf8-3475-ac32-88976ada7f40」啟用。 

-----------------------------
[kk@rocky ~]$ sudo nmcli connect down ens160

[kk@rocky ~]$ sudo nmcli connect up ens160
-----------------------------

[kk@rocky ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
...
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:a8:fc:5a brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 163.32.145.70/24 brd 163.32.145.255 scope global noprefixroute ens160
...

 

 

 

>>>>>>>>>>>>>>File based:

[kk@rocky ~]$ sudo vi /etc/NetworkManager/system-connections/ens160.nmconnection
[sudo] kk 的密碼:
 

[connection]
id=ens160
uuid=f8db373b-ebf8-3475-ac32-88976ada7f40
type=ethernet
autoconnect-priority=-999
interface-name=ens160
timestamp=1714117009

[ethernet]

[ipv4]
address1=163.32.145.70/24,163.32.145.254
dns=168.95.1.1;
method=manual

[ipv6]
addr-gen-mode=eui64
method=auto

[proxy]
~
~
~
 

[kk@rocky ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
...
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
...
    inet 163.32.145.69/24 brd 163.32.145.255 scope global noprefixroute ens160
 

[kk@rocky ~]$ sudo nmcli connect load /etc/NetworkManager/system-connections/ens160.nmconnection

[kk@rocky ~]$ sudo nmcli connect up /etc/NetworkManager/system-connections/ens160.nmconnection
 



>>>>>>>>>>>>>>CLI 下的圖形設計工具:

[kk@rocky ~]$ systemctl status NetworkManager
● NetworkManager.service - Network Manager
     Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; p>
     Active: active (running) since Fri 2024-04-26 14:28:37 CST; 7min ago
       Docs: man:NetworkManager(8)
   Main PID: 755 (NetworkManager)
      Tasks: 3 (limit: 48646)
     Memory: 11.3M
        CPU: 348ms
     CGroup: /system.slice/NetworkManager.service
             └─755 /usr/sbin/NetworkManager --no-daemon
 

[kk@rocky ~]$ ls /usr/lib/systemd/system
 ...
 NetworkManager.service
...

 

#Nmtui is a part of the NetworkManager package in Linux and provides a text-based user interface to manage your network settings. 

 [kk@rocky ~]$ nmtui edit ens160


 [kk@rocky ~]$ sudo systemctl restart NetworkManager.service

 

 


 

 

網路測試工具  ----------------------------------------------------------------------------------------------------------

>>>>>>>>>>>>>>hostnamectl 取替 hostname

[kk@rocky ~]$ hostnamectl
 Static hostname: rocky.kk
...

[kk@rocky ~]$ sudo hostnamectl set-hostname BigRocky.kk
[sudo] kk 的密碼:
[kk@rocky ~]$ hostnamectl
 Static hostname: BigRocky.kk
...


 

>>>>>>>>>>>>>>ss 取替 netstat

//ss is used to dump socket statistics. It allows showing information similar to netstat.  It can display more TCP  and  state  information  than other tools.

//t:tcp協定   u:udp協定   n:不用名稱以port顯示   p:連線由哪個行程處理   l:在傾聽的連線

[kk@BigRocky ~]$ ss -tunpl
Netid    State     Recv-Q    Send-Q                           Local Address:Port       Peer Address:Port   Process
udp      UNCONN    0         0                                    127.0.0.1:323             0.0.0.0:*
udp      UNCONN    0         0                                        [::1]:323                [::]:*
udp      UNCONN    0         0            [fe80::20c:29ff:fea8:fc5a]%ens160:546                [::]:*
tcp      LISTEN    0         128                                    0.0.0.0:22              0.0.0.0:*
tcp      LISTEN    0         128                                       [::]:22                 [::]:*


//檢視tcp連線
[kk@BigRocky ~]$ ss -t
State       Recv-Q       Send-Q              Local Address:Port                Peer Address:Port        Process
ESTAB       0            64                  163.32.145.69:ssh                163.32.145.99:61321



//  -u, --udp   Display UDP sockets.
[kk@BigRocky ~]$ ss -u
Recv-Q         Send-Q                  Local Address:Port                   Peer Address:Port         Process



>>>>>>>>>>>>>>tracepath 取替  traceroute/traceroute6

 

//tracepath, tracepath6 - traces path to a network host discovering MTU along this path
[kk@BigRocky ~]$ tracepath 168.95.1.1
 1?: [LOCALHOST]                      pmtu 1500
 1:  _gateway                                              0.903ms
 1:  _gateway                                              0.975ms
 2:  10.241.3.153                                          1.531ms
 3:
 ^C

 >>>>>>>>>>>>>>ip route 取替  route
[kk@BigRocky ~]$ ip route | column -t
default          via  163.32.145.254  dev    ens160  proto  static  metric  100
163.32.145.0/24  dev  ens160          proto  kernel  scope  link    src     163.32.145.69  metric  100

 
[kk@BigRocky ~]$ ip -6 route | column -t > RouteTable.text
[kk@BigRocky ~]$ cat RouteTable.text
::1                   dev  lo                        proto  kernel  metric  256   pref    medium
2001:288:8241:1::23c  dev  ens160                    proto  kernel  metric  100   pref    medium
2001:288:8241:1::/64  dev  ens160                    proto  ra      metric  100   pref    medium
fe80::/64             dev  ens160                    proto  kernel  metric  1024  pref    medium
default               via  fe80::ea1c:baff:fec9:968  dev    ens160  proto   ra    metric  100     pref  medium


>>>>>>>>>>>>>>ip neighbor 取替  arp
//ARP
//Address Resolution Protocol 位址解析協定,由已知的 IP 位址查問其相對應的網路實體位址MAC
 //RARP
//Reverse Address Resolution Protocol 反向位址解析協定,由已知的網路實體位址(MAC 位址)查詢其相對應的 IP 位址

[kk@BigRocky ~]$ ip neighbor show
163.32.145.254 dev ens160 lladdr e8:1c:ba:c9:09:68 STALE
163.32.145.253 dev ens160 lladdr 4c:ae:a3:29:75:10 STALE
163.32.145.99 dev ens160 lladdr 00:e0:4c:23:07:3a REACHABLE
fe80::ea1c:baff:fec9:968 dev ens160 lladdr e8:1c:ba:c9:09:68 router STALE


 >>>>>>>>>>>>>>yum、dnf

////
被淘汰的工具只是不在預設安裝所包含的,所以是可以利用 yum 把他安裝回來
Note:在 yum 中不知道套件的名稱,可以用 provides 和 list 直接搜尋 package 裡面的工具
////


//DNF  is the next upcoming major version of YUM

[kk@BigRocky ~]$ dnf provides */ifconfig
Rocky Linux 9 - BaseOS                          1.1 MB/s | 2.2 MB     00:02
Rocky Linux 9 - AppStream                       1.6 MB/s | 7.4 MB     00:04
Rocky Linux 9 - Extras                           15 kB/s |  14 kB     00:00
net-tools-2.0-0.62.20160912git.el9.x86_64 : Basic networking tools
軟體庫  :baseos
符合來源:
檔案名稱:/usr/sbin/ifconfig

[kk@BigRocky ~]$ dnf list */ifconfig
上次中介資料過期檢查:0:01:08 前,時間點為西元2024年04月29日 (週一) 13時45分38秒。
可用的軟體包
net-tools.x86_64  


[kk@BigRocky ~]$ sudo dnf -y install net-tools
上次中介資料過期檢查:0:05:22 前,時間點為西元2024年04月29日 (週一) 13時54分50秒。

[kk@BigRocky ~]$ ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 163.32.145.69  netmask 255.255.255.0  broadcast 163.32.145.255
        inet6 2001:288:8241:1:20c:29ff:fea8:fc5a  prefixlen 64  scopeid 0x0<global>
        inet6 2001:288:8241:1::23c  prefixlen 128  scopeid 0x0<global>
        inet6 fe80::20c:29ff:fea8:fc5a  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a8:fc:5a  txqueuelen 1000  (Ethernet)
        RX packets 22125  bytes 14068960 (13.4 MiB)
        RX errors 0  dropped 1089  overruns 0  frame 0
        TX packets 6588  bytes 640794 (625.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 156 (156.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 156 (156.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0




[kk@BigRocky ~]$ dnf provides */dig
上次中介資料過期檢查:0:07:08 前,時間點為西元2024年04月29日 (週一) 13時45分38秒。
bind-utils-32:9.16.23-14.el9_3.4.x86_64 : Utilities for querying DNS name servers
軟體庫  :appstream
符合來源:
檔案名稱:/usr/bin/dig

[kk@BigRocky ~]$ dnf list */dig
上次中介資料過期檢查:0:09:19 前,時間點為西元2024年04月29日 (週一) 13時45分38秒。
可用的軟體包
bind-utils.x86_64


[kk@BigRocky ~]$ sudo dnf -y install bind-utils.x86_64
[sudo] kk 的密碼:
上次中介資料過期檢查:0:01:39 前,時間點為西元2024年04月29日 (週一) 13時54分50秒。
依賴關係解析完畢。

[kk@BigRocky ~]$  dig 168.95.1.1

; <<>> DiG 9.16.23-RH <<>> 168.95.1.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 20856
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;168.95.1.1.                    IN      A

;; AUTHORITY SECTION:
.                       2562    IN      SOA     a.root-servers.net. nstld.verisign-grs.com. 2024042900 1800 900 604800 86400

;; Query time: 7 msec
;; SERVER: 168.95.1.1#53(168.95.1.1)
;; WHEN: Mon Apr 29 13:58:46 CST 2024
;; MSG SIZE  rcvd: 114



 

  ----------------------------------------------------------------------------------------------------------