| title | description | tags |
|---|---|---|
Linux |
My Linux note about server and something... |
skills |
This is a linux documents using debian 10
View it on HACKMD: https://hackmd.io/@wilicw/linuxSkills
Linux Distribution : Debian 10
[TOC]
Change timezone
sudo timedatectl set-timezone Asia/TaipeiShow timezone
ls -l /etc/localtimeChange hostname
sudo vim /etc/hostnamesudo useradd -m -d "/home/username" -s /bin/bash username
import os
os.system("groupadd groupName")
for i in range(1, 51): # 1~50
username = ""
password = ""
name = "{}{:02d}".format(username, i)
os.system("useradd -s /bin/bash {}".format(name))
os.system("usermod -aG sudo {}".format(name)) # in fedora sduo calls wheel
os.system("usermod -aG gourpName {}".format(name))
os.system("echo \"{}:{}\" | chpasswd".format(name, password))
sudo userdel -r usernameecho "username:password" | chpasswdsudo passwd -d [username]sudo chage -m 999 [username]Edit /home/user/.bash_logout
#!/bin/bash
# shell script
Edit /etc/gdm3/daemon.conf
[security]
...
AllowRoot=true
...
Edit /etc/pam.d/gdm-password
# auth required pam_succeed_if.so user != root quiet_success
# comment this line
sudo groupadd group_kawaii_no_joshi_shougakusei_namesudo chgrp group_name folder_namesudo gpasswd group_namesudo gpasswd -A username group_name- Only group admin or root can do it
sudo gpasswd -d group_user group_name- Only group admin or root cna do it
sudo gpasswd -a username group_nameEdit /etc/profile
if [ `id -ng` = "the_cool_group_name" ] ; then
# do stuff for people in the_cool_group
fi
main file: /etc/network/interfaces
Change interface name to old syntax (ens33 -> eth0)
vim /etc/default/grub
Add net.ifnames=0 biosdevname=0 into GRUB_CMDLINE_LINUX
...
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
...
Update grub
sudo update-grub
force to clean ip of interface
ip addr flush dev eth0
renew a dhcp ip
dhclient
auto eth0
iface eth0 inet dhcp
iface eth0 inet static
address 192.168.0.7
netmask 255.255.255.0
gateway 192.168.0.254
auto eth0:0
iface eth0:0 inet static
address 192.168.0.7
netmask 255.255.255.0
gateway 192.168.0.254
auto eth0:1
iface eth0:1 inet static
address 192.168.0.8
netmask 255.255.255.0
gateway 192.168.0.254
auto dsl-provider
iface dsl-provider inet ppp
pre-up /sbin/ifconfig eth0 up # line maintained by pppoeconf
provider dsl-provider
auto eth0
iface eth0 inet manual
Edit /etc/resolv.conf
nameserver 8.8.8.8
sudo apt install ifenslave # Install ifenslave
sudo modprobe bonding
sudo echo 'bonding' >> /etc/modulesAdd bond0 in /etc/network/interfaces
auto bond0
iface bond0 inet dhcp
bond-mode 1
bond-primary eth0
bond-slaves eth0 eth1
bond-millmon 100
bond-downdelay 400
bond-updelat 800
sudo ifdown eth0 eth1
sudo systemctl restart networkingsudo apt install ntpdate # Install ntpdate tool
sudo ntpdate [ntp server ip]
sudo hwclock -w # Write time into BIOSAllow some users use crontab
Edit /etc/cron.allow
User1
User2
Deny some users use crontab
Edit /etc/cron.deny
User3
User4
Level: Allow > Deny
crontab -lcrontab -rEdit User's crontab
crontab -eor Edit System's crontb in /etc/crontab as root
Format
* * * * * command
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
*
n,m n and m
n-m n to m
/n every n
Example
* */12 * * * shutdown -r now
# every 12 hours restart computer
@reboot command # run command in reboot
@yearly command # run command every year same as 0 0 1 1 * command
@monthly command # run command every month same as 0 0 1 * * command
@weekly command # run command every week same as 0 0 * * 1 command
@daily command # run command every day same as 0 0 * * * command
@hourly command # run command every hour same as 0 * * * * command- Limiting the disk space
Edit /etc/fstab
# Original configuration
/dev/sda2 /home ext4 defaults 1 1
# Add usrquota
/dev/sda2 /home ext4 defaults,usrquota 1 1
Remount
sudo mount -o remount /homeCreate aquota.user and aquota.group file
sudo quotacheck -cum /home
cd /home
sudo quotacheck -avugfm
Load config
sudo quotaoff -a # shutdown all quota services
sudo quotaon -avug # start quota and load all config file
Setting up User disk limit
sudo edquota -u User
# Edit blocks it will automatic calculate inodes part
# Unit `KB` 0 is no limit
# Disk quotas for user User (uid 1001):
# Filesystem blocks soft hard inodes soft hard
# /dev/sda2 1024 1000 1024 3 0 0sudo edquota -t
# Grace period before enforcing soft limits for users:
# Time units may be: days, hours, minutes, or seconds
# Filesystem Block grace period Inode grace period
# /dev/sda2 7days 7dayssudo repquota -au
# *** Report for user quotas on device /dev/sda2
# Block grace time: 7days; Inode grace time: 7days
# Block limits File limits
# User used soft hard grace used soft hard grace
# ----------------------------------------------------------------------
# root -- 20 0 0 2 0 0
# wilicw -- 301532 0 0 3378 0 0
# User +- 1024 1000 1024 6days 3 0 0Create a shell script in any folder
Create and edit /etc/systemd/system/service-name.service
[Unit]
Description=Service desc
[Service]
ExecStart=/root/your-script.sh
Restart=always
[Install]
WantedBy=default.target
Enable the service
sudo systemctl enable service-nameEdit ~/.bashrc
HISTSIZE=100
HISTFILESIZE=100
# saving the last 100 commands in history file
Edit ~/.bashrc
TMOUT=60
# 60 sec
Install nginx
sudo apt install nginxStart nginx in startup
sudo systemctl enable nginxUsing htpasswd command
sudo apt install apache2-utils
Create password file in /var/www/.passwd via htpasswd
htpasswd -c /var/www/.passwd user0
Adding more user in passwd file
htpasswd /var/www/.passwd user1
Configuring nginx
# restric /admin but allow /admin/pubic
location /secret {
auth_basic "Administratorβs Area";
auth_basic_user_file /var/www/.passwd;
location /public/ {
auth_basic off;
}
}# allow 192.168.1.0/24 but deny 192.168.1.2
location /private {
deny 192.168.1.2;
allow 192.168.1.1/24;
allow 127.0.0.1;
deny all;
}upstream myweb {
server 192.168.10.1 weight=3;
server 192.168.10.2 weight=2;
}sudo apt install openssh-server # Install ssh
sudo systemctl enable ssh
sudo systemctl start sshconfig file in /etc/ssh/sshd_config
PermitRootLogin no # Disable root login
AllowUsers user1 user2 # Only allow user1 and user2 login
PasswordAuthentication yes # Use password login
Port 22 # Use 22 port
MaxAuthtries 10 # Max 10 times login try
Match User test3
Banner /etc/Bannertest3 # Only user test3 show banner
sudo apt install fail2banEdit /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
filter = sshd
maxretry = 3
findtime = 600
bantime = 600sudo systemctl restart fail2ban Generating RSA Key pair in client pc
ssh-keygenCopy rsa id to server
ssh-copy-id -i ~/.ssh/id_rsa [email protected]Edit Server side config
# commit this line
# PasswordAuthentication yes
PubKeyAuthenication yes
AuthenicationMethods publickey, password
Enable ip forward setting
sudo sysctl net.ipv4.ip_forward=1Forward enable at evey boot
Edit /etc/sysctl.conf
...
net.ipv4.ip_forward=1
If want to allow PPTP passthrough. Run modprobe ip_nat_pptp
See NFTables
sudo apt install isc-dhcp-server # Install DHCPAdd interface name in /etc/default/isc-dhcp-server
INTERFACES="eth0"
Edit /etc/dhcp/dhcpd.conf
default-lease-time 600;
max-lease-time 7200;
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.100 192.168.0.200;
option subnet-mask 255.255.255.0;
option domain-name-servers 8.8.8.8, 1.1.1.1;
option routers 192.168.1.1;
}
# IP range 192.168.0.100-200
# Netmask 255.255.255.0
# Nameserver 8.8.8.8 1.1.1.1
# Default gateway 192.168.1.1 host android {
hardware ethernet 08:00:27:11:EB:C2; # MAC Address
fixed-address 192.168.100.30; # Static IP
}Restart dhcp server
sudo /etc/init.d/isc-dhcp-server restartShow dhcp client
cat /var/lib/dhcp/dhcpd.leases
sudo apt install dhcp-helperEdit /etc/default/dhcp-helper
# Option flags used to start dhcp-helper.
#
# You will need at least "-s " or
# "-b so that dhcp-helper knows where
# to relay DHCP requests.
#
# See "man 8 dhcp-helper" for more details.
DHCPHELPER_OPT="-s 192.168.10.2"Forward all dhcp requests traffic to 192.168.10.2
sudo apt install bind9 dnsutils # Install dns server and test toolsZone: skills39.co
Edit /etc/bind/named.conf.loacl
zone "skills39.co" IN {
type master;
file "/etc/bind/skills39.co.db";
allow-update {
none;
};
};
And edit /etc/bind/skills39.co.db
$TTL 60
@ IN SOA ns.skills39.co. root.skills39.co. (
20
60
86400
86400
60
)
; Name Server
IN NS ns.skills39.co
ns IN A 10.0.13.212
; A Record
@ IN A 10.0.13.244
www IN A 10.0.13.244
Restart DNS server
sudo systemctl restart bind9Test DNS server
dig skills39.ccOutput
; <<>> DiG 9.10.3-P4-Debian <<>> skills39.co
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 114
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
skills39.co. IN A
;; ANSWER SECTION:
skills39.co. 60 IN A 10.0.13.212
;; AUTHORITY SECTION:
skills39.co. 60 IN NS ns.skills39.co.
;; ADDITIONAL SECTION:
ns.skills39.co. 60 IN A 10.0.13.212
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Apr 12 12:27:43 CST 2019
;; MSG SIZE rcvd: 88
iptables is replace by nftables
more at #nftables
sudo iptables -A INPUT -i lo -j ACCEPTsudo iptables -A OUTPUT -i lo -j ACCEPTsudo iptables -A INPUT -s [ip]/[CIDR] -j DROPClean up the old setting
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -XNAT configuration via iptables
echo "1" > /proc/sys/net/ipv4_forward
sudo iptables -A INPUT -i lo -j ACCEPT
# Allow loopback
iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -o eth0 -j MASQUERADEEdit /etc/sysctl.conf
...
net.ipv4.ip_forward=1
IN interface: ens33
forward 8080 port to 192.168.1.20:80
sudo iptables -t nat -A PREROUTING -p tcp -i ens33 --dport 8080 -j DNAT --to 192.168.1.20:80
sudo iptables -A FORWARD -p tcp --dport 80 -d 192.168.1.20 -j ACCEPT
# Forward tcp/8080 to 192.168.1.20:80Configration file at /etc/nftables.conf
sudo apt install nftablessudo systemctl enable nftablesEdit config file
table ip nat {
chain prerouting {
type nat hook prerouting priority -100;
}
chain postrouting {
type nat hook postrouting priority -100;
}
}
Edit /etc/nftables.conf
add rule nat postrouting masquerade
add rule nat postrouting ip saddr 192.168.0.0/24 oif eth1 snat 10.0.0.1
# all traffic from 192.168.0.0/24 will be forward to eth1 and ip masquerade as 10.0.0.1
Forward eth0 80 port to 192.168.1.2:80
add rule nat prerouting iif eth0 tcp dport { 80 } dnat 192.168.1.2
sudo apt install samba # Install samba serverEdit /etc/samba/smb.conf
[global]
log file = /var/log/samba/log.%m
[file]
comment = File share
path = /home/file
read only = yes
public = yes
[user]
comment = User home file
path = /home/user
read only = no
writable = yes
public = yesAdd samba user
sudo smdpasswd -a user # User should exist in systemsudo apt install vsftpd # Install vsftpd (ftp server)Add /etc/vsftpd.conf
# Enable write access
write_enable=YESAdd /etc/vsftpd.conf
userlist_file=/etc/vsftpd.userlist
userlist_enable=YESCreate /etc/vsftpd.userlist
Only User1 User2 User3 can access ftp server
User1
User2
User3Create /var/ftp and allow all user read
mkdir /var/ftp # Create ftp
chmod 555 /var/ftp # Read only
chown ftp.ftp /var/ftp/ # Change group to ftpEdit /etc/vsftpd.conf
anon_root=/var/ftp
anonymous_enable=YES
no_anon_password=YES
# Enable anonymous writing permission
anon_upload_enable=YES
anon_other_write_enable=YES
anon_mkdir_write_enable=YESEdit /etc/vsftpd.userlist
anonymousBanner
banner_file=/var/ftp/bannerFile.txt
Restart vsftpd service
sudo systemctl restart vsftpdsudo apt install ntp # Install ntpEdit /etc/ntp.conf
# NTP server in stdtime.gov.tw
pool tock.stdtime.gov.tw iburst
pool watch.stdtime.gov.tw iburst
pool time.stdtime.gov.tw iburst
pool clock.stdtime.gov.tw iburst
pool tick.stdtime.gov.tw iburst
# allow 10.0.0.0/8 use this ntp server
restrict 10.0.0.0 mask 255.0.0.0Restart ntp server
sudo systemctl restart ntp-name file name you want to find
-regex use Regex to find with name
-exec trigger a command when target meet the criteria
-empty find empty file
Common use
find . -name "a.txt" -exec rm {} \; #find file named 'a.txt' and delete it c add file in tar file (no compress)
x decompress or unpack a file from tar
t show file in tar
z use gzip (with compress)
f name.tgz output a file named name.tgz
Common use
tar -czvf file.tgz file/Edit /etc/logrotate.d/log
/path/to/your/rails/current/log/*.log {
daily # daily backup
dateext # record date time
missingok
rotate 65535 # keep 65535
compress # using gzip compress
delaycompress
notifempty
copytruncate
}
0 shutdown
1 single user (no network)
2 multiple user (no network)
3 multiple user with network
4 ?
5 multiple user with gui
6 Reboot
sudo init Xedit /etc/inittab to change default run level
sudo runlevelInstall openssl package
sudo apt install opensslGenerate a private key and request file
openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csrCommon Name is your domain name
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Use Windows Server AD CS to auth this request file and get a public key
Rename the public key
mv certnew.crt server.pemEdit nginx config file
sudo vim /etc/nginx/sies-avaliable/default
Add ssl configuration
ssl on;
ssl_certificate /[path]/server.pem;
ssl_certificate_key /[path]/server.key;
Restart nginx services
sudo systemctl restart nginx
sudo apt install openssl
# enable ssl mod
sudo a2enmod ssl
openssl genrsa -out private.key 2048
openssl req -new -key private.key -out public.csrCopy public.csr and rename to public.crt
mv public.csr public.crtMove public.crt to /etc/ssl/certs
cp public.scr /etc/ssl/certs/Move private.key /etc/ssl/private
cp private.key /etc/ssl/private/Modify /etc/apache2/site-available/ssl.conf
SSLCertificateFile /etc/ssl/certs/public.crt
SSLCertificateKeyFile /etc/ssl/private/private.key
SSLCACertificatePath /etc/ssl/certs/
SSLCACertificateFile /etc/ssl/certs/[Intermediate Certificate].crt #If using a self-signed certificate, omit this line- Use root to do following step
Download source code
cd ~
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.2.2.tar.xz # get kernel source code
mkdir /usr/src/kernel
tar xvf linux-5.2.2.tar.xz
mv linux-5.2.2 /usr/src/kernelInstall tools
apt install flex libffi-dev build-essential libncurses5-dev libssl-dev ccache gcc makecd /usr/src/kernel/linux-5.2.2
make mrproper
make clean
make menuconfig
make all
# Wait for 2 hoursmake modules_install
make install
grub-mkconfig -o /boot/grub/grub.cfg
reboot
