Thumbnail
Category: Server

Kinh nghiệm Linux

Date: March 6, 2022
18 views

Contents

[
Hide
]

1. Cài đặt linux in windows

  1. Install virtualbox: https://www.virtualbox.org/wiki/Downloads
  2. Download centos: https://www.linuxtrainingacademy.com/vdi/
  3. Install linux
  4. Mở virtual box
  5. New
  6. Name: cestosdesktop
  7. Type: linux => continue
  8. Chọn RAM => continue
  9. select Use an existing virtual hard disk file
  10. Select file đã giải nén centos download phía trên
  11. Create
  12. Click phải chuột vào item bên trái => start => Normal start
  13. Login:

username/password: adminuser/adminuser

2. Cấu trúc linux

Cấu trúc thư mục cơ bản:

  • / => root
  • /bin => excute program
  • /etc => config
  • /home => home directory
  • /opt => optional or third party
  • /tmp => temp
  • /usr => user
  • /var => variable data

3. Các lệnh cơ bản

  • man: Xem hướng dẫn lênh

  • ls -l  => xem list detail


which directoryName   // => cho biết full path of directory
                        // giống như search folder vậy
//Ex:
which cat

3.1 help

Sử dụng –help or -h


//Ex: 
ls --help
ls -h

3.2 Xem toàn bộ lệnh có trên linux


ls /bin

3.3 Directory

3.3.1 Tạo folder


mkdir directoryName     // => tạo folder
  
mkdir -p path           // => tạo folder với path
// Ex:  
mkdir -p dir1/dir2/dir2

3.3.2 Xóa folder


rm -rf directoryNameOrPath
//Ex: 
rm -rf laravel

3.4 List file – ls

Đọc nội dung khi sử dụng ls -l command.


ls -l -a or ls -la      // => hiện luôn file ẩn
ls -a 
​
ls -F                   // => cho biết file or directory


ls -t               // => sắp xếp theo thời gian
ls -r               // => revert  DESC
ls -latr            // viết tắt của ls -l -a -t -r
                    // hiển thị cả file ẩn theo thời gian DESC
ls -d               // chỉ hiện directory
ls --color          // foder sẽ có màu, file không có màu

3.4.1 Xem dạng cây thư mục


ls -R       // => cây thư mục dạng list


tree
tree -d         // => chỉ hiển thị thư mục

3.5 Remove file


rm file             // remove file
rm -r dir           // remove dir and conent in dir
rm -f file          // force delete

3.6 Copy file


cp source distination  // source nhiều file thì cách nhau bởi dấu cách
cp -i source distination    // có confirm có override không 
                            // => sử dụng sẽ an toàn hơn
cp -r source distination

3.7 Remove file


mv source distination
mv -i source distination    // có confirm có override không

3.8 Sort file


sort file 
sort -K F file  // sort by key, F is field number
sort -r file    // sort reverse
sort -u file    // unique

3.9 Nén và giải nén dữ liệu

3.9.1 tar command


tar [-] c|x|t f tarfile [pattern]
  
// Ex:
tar cf tps.tar tpsreports

3.9.2 gzip command


gzip fileOrFolder       // Ex: gzip data
gunzip fileCompress     // Ex: gunzip data.gz

3.9.3 gzcat command


gzcat
zcat

3.10 Xem dung lượng


du
du -k       // theo kilobyte
du -h       // format cho người có thể đọc

3.11 Wildcards – ký tự viết tắt


1) 
  ? => thay thế cho 1 ký tự nào đó
ls ? // file or dir có 1 ký tự
ls ?? // file or dir có 2 ký tự
ls a?.txt
  
2)
  * => // thay thế cho nhiều
ls a*  // có thể là aa, abc, action.txt ...
ls *.txt // tất cả file txt
ls a*.txt
  
3)
  [các ký tự] // một trong các ký tự này
ls c[abc]t      // có thể là cat, cct
ls [ad]*        // bất kỳ cái nào có a hoặc d ở đầu
  
 4)
 [a-g]      // gồm các ký tự a, b, c, d, e, f, g
 [0-3]      // gồm các ký tự 0, 1, 2, 3

3.12 IO direction

3.13 Comparing file – So sánh file


diff file1 file2
  
// Khi hiển thị
A   => Add
C   => Change
D   => Delete


sdiff file1 file2
  
// Khi hiển thị
hiện thay đổi bằng dâu |
mới bên trái, cũ hiện bên phải


vimdiff file1 file2
//=> Xem cách trực quan

3.14 Custom the Shell Prompt – Thay đổi hiển thị trước dòng lệnh

Lưu cấu hình lại trong file ~./bash_profile

3.15 Tạo các lệnh tắt


alias [name[=value]]
  
// Ex:
alias cls='clear'
alias ll='ls -l'
  
Chỉ gõ alias sẽ ra các alias đã định nghĩa


unalias [name]
  
//Ex:
unalias cls

3.16 Chuyển file thông qua network


SCP
SFTP - SSH
  
  
scpurce distination
sftp host       // lệnh put để đẩy file lên
ftp host
  
scp 2.txt adminuser@linuxsvr:/home/adminuser

Xem thêm tại bài 34 khóa học linux in 5 days. 

https://drive.google.com/file/d/1W9qEqAj4nsrpXLs_YhP5oN1CjBBkvncg/view?usp=sharing

3.17 Search in file


grep pattern file
    -i  // không phân biệt hoa thường
    -c  // cho biết số lượng tìm thấy
    -n  // cho biết tìm thấy ở dòng nào
    -v  // invert, ngược lại với pattern
    -e  // search nhiều pattern
    -E  // user pattern is regex
grep -e text 1 -e text2 -e text3 file text
grep -E "text1|text2|text3" file text

3.18 Dấu Pipes ‘|‘

Dấu pipe lấy output từ câu lệnh này và dùng nó làm input cho câu lệnh kế tiếp.


ifconfig | grep 192
  // result
  inet 192.168.1.96 netmask 255.255.255.0 broadcast 192.168.1.255

3.19 Environment variables

3.19.1 Xem


printenv        // view all environment variables 
printenv HOME
echo $HOME
  
// lệnh 2, 3 giống nhau để xem 1 biến cụ thể

3.19.2 Tạo


export VAR="value"
 
 // Ex:
  export TZ="US|Pacific"

3.19.3 Xóa


unset VAR
  
  // Ex:
  unser TZ

3.20 Process and Jobs

Xem CPU, Memory, các chương trình đang chạy các kiểu.

Bài này không hiểu lắm. Xem thêm: https://drive.google.com/file/d/1Mzjypbqg6i9DfN9zQowP1_qddjxEmv3I/view?usp=sharing


ps -e
  -f        // xem dạng list
  -u username
  -p pid
  -H        // xem dạng tree
  --forest  // xem dạng tree
  
pstree
top
htop
  
bg
jobs    // xem program đang chay
fg      // program đang chạy foreground
        // %%, %+, %-, %2
​
kill [-sig] pid
kill -L

3.21 Crontab


crontab // show all crontab running

Ví dụ:


0 7 * * 1  // run every Monday 7:00
0 2 * * *  // run 2:00 mỗi ngày  

3.21.1 Shortcut


0,3 => 30 phút
*/2 => 30 phút
0-4 => 5 phút

3.22 Switch user and run command

Không rõ lắm xem thêm: https://drive.google.com/file/d/1_tawMBZJAfnP8yERjrJ9G5Z23985ZgY-/view?usp=sharing

3.22.1 su command


su [username]   // chuyển user
   -
   - c

3.22.2 sudo Common


sudo -L command
     -u root command
     -u user command
  
sudo su
sudo su -
sudo su - username
        -S
        -u root -s
        -u user -s

3.22.3 whoami command


whoami   // cho biết user hiện tại

3.23 Shell history

Xem thêm: https://drive.google.com/file/d/1j31GEFShC3DYowtNbZaUAtN_zlqhKwxt/view?usp=sharing


history
HISTSIZE
​
!N
!!
!string
!:N <Event> <Separator> <word>
!
:N
!^
!$

TAB: autocomplete

3.23 Install software

3.23.1 Linux

3.23.1.1 Các software có sẵn


yum search [string]
yum info [packageName]
yum install -y [packageName]
yum remove package
3.23.1.2 Từ file download về dạng rpm


rpm -qa // list all install package
rpm -qf  /path/to/file // list the file's package
rpm -ql package // list package files
rpm -ivh package.rpm  // install
rmp -e package  // uninstall

3.23.2 Ubuntu

3.23.2.1 Các software có sẵn


apt-cache search [string]
apt-get install -y [packageName]
apt-get remove [packageName]   // delete package, leaving config
apt-get purge [packageName]  // delete package, delete config
apt-cache show [packageName]
3.23.2.2 Từ file download về dạng deb


dpkg -l  // list installed package
     -S pat/to/file // list file's package
     -L [packageName] // list all file in package
     -i [package.deb] // install package
     -r [packageName]
     --force-all [packageName]

Other command

  • strings :  hiển thị dạng table
  • cut : cắt bỏ
  • tr : chuyển vị
  • column : Hiện thị dạng column cho dễ nhìn

4. Permission

Có 3 loại:

  • r: read
  • w: Website
  • x: excute

4.1 Đọc permission

Tương ứng với ký tự viết tắt khi sử dụng lệnh

4.2 change permission


// syntax
chmod [ugoa][+-=][rwx]
​
// Ex:
chmod g+w sales.data        // => thêm quyền write cho group của ile sales.data
chmod a=r sales.data        // => set cả 3 user, group, other chỉ có quyền read
                            // => permission có dạng -r--r--r--
chmod u=rwx,g=rx,o= sales.data  // => -rwxr-x---

permission sang bát phân:

Ví dụ 1:

rwx — —-

111 000 000

7   0   0  => permission 700

  • 1 * 2^2 + 1 * 2^1 + 1 * 2^0 = 7

Ví dụ 2:

rw- r– r-x

110 100 101

6   4   5

4.3 Change group


chgrp tenGroup tenFileOrFolder
  
// Ex:
chgrp sale sales.data

4.4 Quyền khi tạo mới file or folder trong folder cha

Mặc định:

  • directory => 755
  • file => 644

4.4.1 Xem quyền


umask       // xem dạng số  0022, 0755,...
umask -S    // xem dạng    u=rwx,g=rx,o=rx

4.4.2 Change quyền mặc định khi tạo mới file or folder bên trong


umask [số quyền]
​
//ex:
umask 007
umask 700

5. Làm việc với file

5.1 Xem file


cat file        // => xem nội dung file
more file       // => xem nội dung file
less file       // => xem nội dung file với cửa sổ riêng
                // thoát nhấn q như lệnh man
head file       // xem 10 dòng đầu tiên của ile
tail file       // xem 10 dòng cuối cùng của file
tail -f file    // follow file luôn, nếu file có chỉnh sửa

5.2 Edit file

5.2.1 Sử dụng nono editor


nano [tenFile]
  
// các lệnh được hiển thị dưới phần edit file

5.2.2 Sử dụng vi editor


vi [tenFile]


// Đi đến dòng thứ 125
esc :125
​
:set nu  // hiển thị số dòng bên trái


~  // =>  reverses
  
// Ex1
the name  // vị trí con trỏ đang ở chữ t, gõ ~
// result => The name
  
// Ex 2
the nAme  // vị trí con trỏ đang ở chữ a gõ ~
// result => the name

5.2.2.1 Cheetshet

https://drive.google.com/file/d/1t-R70QNhHbAm6ugPZdNFr4BCgCEddi1q/view?usp=sharing

6. Tìm kiếm file và folder

6.1 find command


find [path....] [expression]

Expression:

  • -name : tìm kiếm phân biệt hoa thường
  • -iname : tìm kiếm không phân biệt hoa thường
  • -mtime [days]
  • -size [num]
  • -type
  • -newer [file[ : mới hơn file này
  • -exec command {}\;   Lệnh có thể thao tác với file or folder đó.


// Ex
find /sbin -name makedev
find /sbin -iname makedev  
find /sbin -name *V
  
find /sbin -mtime +10 -mtime -13
  
find /sbin -size +1M
find /sbin -size +1G
  
find /sbin -type d -newer file.txt

6.2 locate command


locate [từ cần search]
// Ex:
locate sales
locate sa

Các lưu ý

  • Tên file có khoảng trắng, sẽ chứa trong dấu ngoặc kép khi gọi đến


vi "text translate.txt"

Copyright © 2025 All Right Reserved