Cài Webmin nhìn từ bash script setup-repos.sh

Các nguyên liệu nấu phở – Hình minh họa

Webmin là trang web để quản lí Linux server nên người ta phân loại webmin là web control panel.

Webmin được viết chủ yếu bằng ngôn ngữ lập trình Perl từ năm 1997.

Ngày xưa để cài webmin bạn phải gõ từng lệnh Linux bằng tay như khai báo kho rpm trên dòng Red Hat và các biến thể của Red Hat như CentOS, Fedora, Oracle Linux… sau đó mới có thành quả cuối cùng là lệnh cài đặt webmin.

Cài webmin trên Debian, Ubuntu cũng phải khai báo kho deb…

Bi giờ cài webmin khỏe hơn nhiều nhờ có bash script tự động mọi thứ tên là setup-repos.sh.

setup-repos.sh sẽ tự động nhận biết hệ điều hành Linux để cài webmin là Red Hat hay Debian, Ubuntu nếu không phải sẽ báo lỗi.

# cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.19.0
PRETTY_NAME="Alpine Linux v3.19"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

# curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5268  100  5268    0     0  44439      0 --:--:-- --:--:-- --:--:-- 44644

# sh setup-repos.sh
tty: ignoring all arguments
/dev/pts/0
Error: Unknown OS : alpine

# curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5268  100  5268    0     0  11256      0 --:--:-- --:--:-- --:--:-- 11256

# sh setup-repos.sh
Setup repository? (y/N) y
  Downloading Webmin key ..
  .. done
  Installing Webmin key ..
  .. done
  Setting up Webmin repository ..
  .. done
Webmin package can now be installed using dnf install webmin command.

# dnf install webmin
Webmin - noarch                                                                                        565 kB/s | 1.6 MB     00:02
Last metadata expiration check: 0:00:01 ago on Mon Jan 22 16:24:28 2024.
Dependencies resolved.
=======================================================================================================================================
 Package                              Architecture            Version                             Repository                      Size
=======================================================================================================================================
Installing:
 webmin                               noarch                  2.105-1                             webmin-noarch                   39 M
Installing dependencies:
 almalinux-indexhtml                  noarch                  9-2.el9                             appstream                      7.8 k
 perl-Digest-SHA                      x86_64                  1:6.02-461.el9                      appstream                       61 k
 perl-Filter                          x86_64                  2:1.60-4.el9                        appstream                       81 k
 perl-encoding                        x86_64                  4:3.00-462.el9                      appstream                       62 k
 perl-open                            noarch                  1.12-480.el9                        appstream                       17 k
Installing weak dependencies:
 lynx                                 x86_64                  2.8.9-19.el9                        appstream                      1.5 M
 perl-Encode-Detect                   x86_64                  1.01-37.el9                         appstream                       89 k
 perl-Time-HiRes                      x86_64                  4:1.9764-462.el9                    appstream                       57 k
 perl-Time-Piece                      x86_64                  1.3401-480.el9                      appstream                       41 k
 shared-mime-info                     x86_64                  2.1-5.el9                           baseos                         372 k

Transaction Summary
=======================================================================================================================================
Install  11 Packages

Total download size: 41 M
Installed size: 127 M
Is this ok [y/N]: y
Downloading Packages:
(1/11): perl-Digest-SHA-6.02-461.el9.x86_64.rpm                                                        265 kB/s |  61 kB     00:00
(2/11): almalinux-indexhtml-9-2.el9.noarch.rpm                                                          32 kB/s | 7.8 kB     00:00
(3/11): perl-Filter-1.60-4.el9.x86_64.rpm                                                              1.5 MB/s |  81 kB     00:00
(4/11): lynx-2.8.9-19.el9.x86_64.rpm                                                                   5.0 MB/s | 1.5 MB     00:00
(5/11): perl-Encode-Detect-1.01-37.el9.x86_64.rpm                                                      1.2 MB/s |  89 kB     00:00
(6/11): perl-Time-HiRes-1.9764-462.el9.x86_64.rpm                                                      3.1 MB/s |  57 kB     00:00
(7/11): perl-Time-Piece-1.3401-480.el9.x86_64.rpm                                                      2.4 MB/s |  41 kB     00:00
(8/11): perl-encoding-3.00-462.el9.x86_64.rpm                                                          3.5 MB/s |  62 kB     00:00
(9/11): perl-open-1.12-480.el9.noarch.rpm                                                              864 kB/s |  17 kB     00:00
(10/11): shared-mime-info-2.1-5.el9.x86_64.rpm                                                         6.3 MB/s | 372 kB     00:00
(11/11): webmin-2.105-1.noarch.rpm                                                                     181 kB/s |  39 MB     03:40
---------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                  189 kB/s |  41 MB     03:43
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                               1/1
  Installing       : shared-mime-info-2.1-5.el9.x86_64                                                                            1/11
  Running scriptlet: shared-mime-info-2.1-5.el9.x86_64                                                                            1/11
  Installing       : perl-Time-Piece-1.3401-480.el9.x86_64                                                                        2/11
  Installing       : perl-Time-HiRes-4:1.9764-462.el9.x86_64                                                                      3/11
  Installing       : perl-Filter-2:1.60-4.el9.x86_64                                                                              4/11
  Installing       : perl-encoding-4:3.00-462.el9.x86_64                                                                          5/11
  Installing       : perl-open-1.12-480.el9.noarch                                                                                6/11
  Installing       : perl-Encode-Detect-1.01-37.el9.x86_64                                                                        7/11
  Installing       : perl-Digest-SHA-1:6.02-461.el9.x86_64                                                                        8/11
  Installing       : almalinux-indexhtml-9-2.el9.noarch                                                                           9/11
  Installing       : lynx-2.8.9-19.el9.x86_64                                                                                    10/11
  Running scriptlet: webmin-2.105-1.noarch                                                                                       11/11
  Installing       : webmin-2.105-1.noarch                                                                                       11/11
  Running scriptlet: webmin-2.105-1.noarch                                                                                       11/11
  Verifying        : almalinux-indexhtml-9-2.el9.noarch                                                                           1/11
  Verifying        : lynx-2.8.9-19.el9.x86_64                                                                                     2/11
  Verifying        : perl-Digest-SHA-1:6.02-461.el9.x86_64                                                                        3/11
  Verifying        : perl-Encode-Detect-1.01-37.el9.x86_64                                                                        4/11
  Verifying        : perl-Filter-2:1.60-4.el9.x86_64                                                                              5/11
  Verifying        : perl-Time-HiRes-4:1.9764-462.el9.x86_64                                                                      6/11
  Verifying        : perl-Time-Piece-1.3401-480.el9.x86_64                                                                        7/11
  Verifying        : perl-encoding-4:3.00-462.el9.x86_64                                                                          8/11
  Verifying        : perl-open-1.12-480.el9.noarch                                                                                9/11
  Verifying        : shared-mime-info-2.1-5.el9.x86_64                                                                           10/11
  Verifying        : webmin-2.105-1.noarch                                                                                       11/11

Installed:
  almalinux-indexhtml-9-2.el9.noarch          lynx-2.8.9-19.el9.x86_64                  perl-Digest-SHA-1:6.02-461.el9.x86_64
  perl-Encode-Detect-1.01-37.el9.x86_64       perl-Filter-2:1.60-4.el9.x86_64           perl-Time-HiRes-4:1.9764-462.el9.x86_64
  perl-Time-Piece-1.3401-480.el9.x86_64       perl-encoding-4:3.00-462.el9.x86_64       perl-open-1.12-480.el9.noarch
  shared-mime-info-2.1-5.el9.x86_64           webmin-2.105-1.noarch

Complete!

# curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5268  100  5268    0     0  29228      0 --:--:-- --:--:-- --:--:-- 29266
 
# sh setup-repos.sh
Setup repository? (y/N) y
  Downloading Webmin key ..
  .. done
  Installing Webmin key ..
  .. done
  Setting up Webmin repository ..
  .. done
  Cleaning repository metadata ..
  .. done
  Downloading repository metadata ..
  .. done
Webmin package can now be installed using apt-get install --install-recommends webmin command.

# apt update ; apt-get install --install-recommends webmin
Hit:1 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:2 http://vn.archive.ubuntu.com/ubuntu jammy InRelease
Hit:3 http://vn.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://vn.archive.ubuntu.com/ubuntu jammy-backports InRelease
Ign:5 https://download.webmin.com/download/newkey/repository stable InRelease
Hit:6 https://download.webmin.com/download/newkey/repository stable Release
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
74 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libalgorithm-c3-perl libauthen-pam-perl libb-hooks-endofscope-perl libb-hooks-op-check-perl libclass-c3-perl libclass-c3-xs-perl libclass-data-inheritable-perl
  libclass-inspector-perl libclass-method-modifiers-perl libclass-singleton-perl libclass-xsaccessor-perl libdata-optlist-perl libdatetime-locale-perl
  libdatetime-perl libdatetime-timezone-perl libdevel-callchecker-perl libdevel-caller-perl libdevel-lexalias-perl libdevel-stacktrace-perl
  libdynaloader-functions-perl libencode-detect-perl libeval-closure-perl libexception-class-perl libfile-sharedir-perl libio-pty-perl libmodule-implementation-perl
  libmodule-runtime-perl libmro-compat-perl libnamespace-autoclean-perl libnamespace-clean-perl libpackage-stash-perl libpackage-stash-xs-perl libpadwalker-perl
  libparams-classify-perl libparams-util-perl libparams-validationcompiler-perl libreadonly-perl libref-util-perl libref-util-xs-perl librole-tiny-perl
  libspecio-perl libsub-exporter-perl libsub-exporter-progressive-perl libsub-identify-perl libsub-install-perl libsub-name-perl libsub-quote-perl
  libvariable-magic-perl libxstring-perl lynx lynx-common
Suggested packages:
  libscalar-number-perl libtest-fatal-perl
The following NEW packages will be installed:
  libalgorithm-c3-perl libauthen-pam-perl libb-hooks-endofscope-perl libb-hooks-op-check-perl libclass-c3-perl libclass-c3-xs-perl libclass-data-inheritable-perl
  libclass-inspector-perl libclass-method-modifiers-perl libclass-singleton-perl libclass-xsaccessor-perl libdata-optlist-perl libdatetime-locale-perl
  libdatetime-perl libdatetime-timezone-perl libdevel-callchecker-perl libdevel-caller-perl libdevel-lexalias-perl libdevel-stacktrace-perl
  libdynaloader-functions-perl libencode-detect-perl libeval-closure-perl libexception-class-perl libfile-sharedir-perl libio-pty-perl libmodule-implementation-perl
  libmodule-runtime-perl libmro-compat-perl libnamespace-autoclean-perl libnamespace-clean-perl libpackage-stash-perl libpackage-stash-xs-perl libpadwalker-perl
  libparams-classify-perl libparams-util-perl libparams-validationcompiler-perl libreadonly-perl libref-util-perl libref-util-xs-perl librole-tiny-perl
  libspecio-perl libsub-exporter-perl libsub-exporter-progressive-perl libsub-identify-perl libsub-install-perl libsub-name-perl libsub-quote-perl
  libvariable-magic-perl libxstring-perl lynx lynx-common webmin
0 upgraded, 52 newly installed, 0 to remove and 74 not upgraded.
Need to get 39,5 MB of archives.
After this operation, 225 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Cài cắm trên Linux chạy bằng root nên nếu bạn chạy setup-repos.sh không phải root cũng sẽ bị lỗi…

[hieu@DevSecOps-Edu-VN ~]$ curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5268  100  5268    0     0  41480      0 --:--:-- --:--:-- --:--:-- 41480
[hieu@DevSecOps-Edu-VN ~]$
[hieu@DevSecOps-Edu-VN ~]$ sh setup-repos.sh
Error: `setup-repos.sh` script must be run as root!
[hieu@DevSecOps-Edu-VN ~]$
[hieu@DevSecOps-Edu-VN ~]$ echo $USER
hieu

webmin được stop start ở dạng 1 dịch vụ trong systemd.

systemctl status webmin
● webmin.service - Webmin server daemon
     Loaded: loaded (/usr/lib/systemd/system/webmin.service; enabled; preset: disabled)
     Active: active (running) since Mon 2024-01-22 16:28:42 UTC; 10min ago
    Process: 2349 ExecStart=/usr/libexec/webmin/miniserv.pl /etc/webmin/miniserv.conf (code=exited, status=0/SUCCESS)
   Main PID: 2350 (miniserv.pl)
      Tasks: 1 (limit: 4352)
     Memory: 67.6M
        CPU: 2.150s
     CGroup: /system.slice/webmin.service
             └─2350 /usr/bin/perl /usr/libexec/webmin/miniserv.pl /etc/webmin/miniserv.conf

Jan 22 16:28:41 DevSecOps-Edu-VN systemd[1]: Starting Webmin server daemon...
Jan 22 16:28:42 DevSecOps-Edu-VN systemd[1]: Started Webmin server daemon.

systemctl status webmin
● webmin.service - Webmin server daemon
     Loaded: loaded (/lib/systemd/system/webmin.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2024-01-23 00:25:21 +07; 24s ago
    Process: 4664 ExecStart=/usr/share/webmin/miniserv.pl /etc/webmin/miniserv.conf (code=exited, status=0/SUCCESS)
   Main PID: 4665 (miniserv.pl)
      Tasks: 1 (limit: 4556)
     Memory: 189.8M
        CPU: 3.482s
     CGroup: /system.slice/webmin.service
             └─4665 /usr/bin/perl /usr/share/webmin/miniserv.pl /etc/webmin/miniserv.conf

Thg 1 23 00:25:19 DevSecOps.Edu.VN systemd[1]: Starting Webmin server daemon...
Thg 1 23 00:25:20 DevSecOps.Edu.VN perl[4664]: pam_unix(webmin:auth): authentication failure; logname= uid=0 euid=0 tty= ruser= rhost=  user=root
Thg 1 23 00:25:21 DevSecOps.Edu.VN webmin[4664]: Webmin starting
Thg 1 23 00:25:21 DevSecOps.Edu.VN systemd[1]: Started Webmin server daemon.

Nếu chỉ xài setup-repos.sh thì bạn chỉ là script kiddie hay script dummy và bạn sẽ không có ưu thế gì so với người khác vì họ cũng xài bash script này dễ dàng.

Vậy nên bạn cần phải học lập trình bash shell để tự động mọi thứ theo ý muốn.

Linux automation là xu hướng không thể thay đổi để hạn chế lỗi từ thao tác của người dùng, đảm bảo hệ thống được cài đặt, cấu hình đúng như mong muốn và giảm thời gian thực hiện xuống càng nhanh càng tốt vì thời gian là tiền bạc (time is money).

Bài tập lập trình Bash script dành cho các bạn không mua khóa học Lập trình Bash Shell thực chiến cho Dev – DevOps – DevSecOps…

Chỉnh sửa code của setup-repos.sh để gọn gàng hơn 1 chút.

https://github.com/webmin/webmin/blob/master/setup-repos.sh