🚨Things to remember before typing commands🚨
When typing a command that puts a load on the server, add the following to the beginning of the command, below there is difference between of less more cat view
When you want to back up a file temporarily create a .bak file in the same directory as the file you are operating. Sometimes I keep in /tmp
and /var/tmp
separately.
Explanation:
/tmp
will be erased when you reboot (if it is mounted by tmpfs)./var/tmp
does not lose the file even if it is restarted. It is also retained for longer than/tmp
.- Both are erased regularly.
/tmp
-> 10 days ,/var/tmp
30 days. - For more details type
cat/etc/cron.daily/tmpwatch
Use the service command rather than /etc/init.d service
Is less affected by the environment variable of the executing user.
Memory, disk
- Check the current memory usage
vagrant@vagrant:~$ free -m
vagrant@vagrant:~$ cat /proc/meminfo
Let’s look at the number of memories and the number of buffers. The number of buffers is important.
- View swap size
vagrant@vagrant:~$ swapon -s
- Check the current total disk usage
vagrant@vagrant:~$ df -th
Let’s see how much remains in total when disk space goes down
- Check disk usage
vagrant@vagrant:~$ sudo ionice -c 2 -n 7 nice -n 19 du -scm /* | sort -rn
scm
: do not show subdirectories + show total disk usage + show in MB format (if not sorting, use -m -k is easy to see).
rn
: Sort by the amount used first + compare by number after hitting df, see what files are big
File transfer, search
- Transfer files with 1MB/s limit (let’s give -n before command execution and run dry)
vagrant@vagrant:~$ sudo rsync -av --progress --bwlimit=1000 /tmp/test.txt 192.168.123.123:/tmp
Transfer test.txt to 192.168.123.123/tmp. av: a={-r -l -p -t -g -o -D }
include all options. v
display the transfer status when progress is added, the remaining transfer time is displayed. Come on when transferring large files. bwlimit is KByte specified.
- Search for a .txt file in the current directory, and delete the ones that include spaces in the file name.
vagrant@vagrant:~ find . -maxdepth 1 -type f -name "*.txt" -print0 | xargs -0 rm -rf
1 hierarchy of the directory to go to looking for in -maxdepth
(current). It is passed to xargs
and rm -rf
is executed. This time, the point is that it is blank. -print0:
White space is separated into \0 -0: \0
is treated as a delimiter
- Look for other than
.txt
and.log
files in the current directory.
vagrant@vagrant:~$ find . -maxdepth 1 -type f \! \( -name "*.txt" -o -name "*.log" \)
CPU, OS, package
- CPU information, number of cores, etc.
vagrant@vagrant:~$ cat /proc/cpuinfo
- CPU usage
vagrant@vagrant:~$ ionice -c 2 -n 7 nice -n 19 top
By pressing 1
, you can see the usage amount for each core, which is convenient.
- Check the number of processes
vagrant@vagrant:~$ ps auxww
w
to increase the display width. With two, there is no limit and the commands are displayed in full.
- Check the process of the file used
vagrant@vagrant:~$ lsof -p PID_BUMBER
ps auxww
to check the PID_BUMBER
and use it to find out what process is using this command.
- Stop all processes running in /bin/sh
vagrant@vagrant:~$ pkill -f "/bin/sh"orvagrant@vagrant:~$ ps auxww | grep "/bin/sh" | awk '{print $2}' | xargs kill
awk
to get the process number. Some people often use exec rm
, but I think so xargs
is safer and faster so use it.
- Check OS version
vagrant@vagrant:~$ cat /etc/*release
- Check kernel version
vagrant@vagrant:~$ uname -a
- Package check
Ubuntu:
vagrant@vagrant:~$ dpkg -l | grepRHEL:
vagrant@vagrant:~$ rpm -qa | grep
Check which version of the package is included. Good to use with grep 😀.
Network
- View interface and IP information
vagrant@vagrant:~$ ifconfig
vagrant@vagrant:~$ ip addr show
- View routing
vagrant@vagrant:~$ route
If you can’t connect to the net, check it first.
- Check the network route
vagrant@vagrant:~$ traceroute -n 192.168.123.123
If you can’t connect to the net, check it first. -n
Do not reverse. I just look at the route. If the device does not allow UDP communication, use ICMP to trace.
vagrant@vagrant:~$ traceroute -In
If there is a network device that repels UDP communication occasionally, use this. I (capital letter). However, unlike UDP, the speed is slower, so in case of emergency.
- Check solr connection
vagrant@vagrant:~$ netstat -ano | grep 8393
-ano
: All connections + No reverse lookup + Time display. During maintenance, you can check if there are any connections from outside. 8393 is the solr port.
- Check IPMI information
vagrant@vagrant:~$ sudo ipmitool lan print
- Confirmation of iptables
vagrant@vagrant:~$ iptables -L
If you want to read more about IPTABLES rules and commands visit on this article:
- Important files around the interface
vagrant@vagrant:~$ cat /etc/udev/rules.d/70-persistent-net.rules
Inside there are configuration of multiple NICs.
- View network interface status
vagrant@vagrant:~$ ethtool -i eth0
Redundancy can be checked, but NIC is a vendor code for each manufacturer, and the first value of the MAC address is fixed. In case of redundant NIC, eth is assigned from the smallest number of MAC address, so assign it in the order started (inserted) with pci using bus-info value and 70-persistent-net.rules
file.
Account & login
- Check the logged-in user
vagrant@vagrant:~$ w
Is it due to someone’s work at the time of failure? You can check if anyone is touching when doing server maintenance.
- Send a message to all logged in users
vagrant@vagrant:~$ wall
Very useful when there is a logged-in user when dropping the server.
- Display a list of users and groups
vagrant@vagrant:~$ getent passwd
Useful when we use it to get user information from LDAP.
- User creation
vagrant@vagrant:~$ sudo useradd -g examplegroup -u 600 -d /home/user/exampleuser exampleuser
examplegroup
group + user ID: 600 + home directory is /home/user/exampleuser and exampleuser
user creation.
- Delete user
vagrant@vagrant:~$ sudo userdel exampleuser
- User password setting
vagrant@vagrant:~$ passwd exampleuser
- If you make a mistake and enter the ssh password, delete it from history. simply check line numbers
vagrant@vagrant:~$ history | less
When you find the line simply delete them.
vagrant@vagrant:~$ history -d 280
MySQL
- MySQL show master information
mysql> show master status \G
Required when making a slave. When you stop the slave, be sure to hit it and save it in a text.
- MySQL show slave information
mysql> show slave status \G
Hit very well. When you stop the slave, be sure to hit it and save it in a text. When there is an error around replication, check if the output result below is Yes.
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
- MySQL see the running process
mysql> show processlist
You can see what command is being typed now.
- MySQL user information
mysql> select host,user from mysql.user ;
- MySQL confirmation of user authority
mysql> show grants for user_name ;
- MySQL add authority
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER
ON *.* TO 'example_user'@'%' IDENTIFIED BY 'P@ssw0rd';
*.*
Means all DBs, all Tables, %
does not include localhost so if you want to connect from local, specify localhost explicitly.
- MySQL examine the columns of the table
mysql> describe table_name ;
- MySQL lock when you do not want to access the table
mysql> flush tables with read lock ;
mysql> show processlist ;
mysql> quit
$ mysql -uroot -e "show status" | grep "Key_blocks_not"
$ mysql -uroot -e "show engine innodb status \G" | grep "Log sequence"
$ mysql -uroot -e "show engine innodb status \G" | grep "Log flush"
Very often used for maintenance, but It will be very dangerous as it will affect the service.
⚠️Be sure to run show processlist multiple times to see the process.⚠️
- MySQL check the number of temp tables currently opened by slave threads
mysql> show status like "Slave_open_temp_tables" ;
- MySQL change global variable to flush logs in innodb buffer pool at shutdown
mysql> set global innodb_fast_shutdown = 0 ;
In the case of innodb, the data consistency after shutdown can be improved by writing the part that is not reflected in the tablespace at shutdown.
- MySQL reset of slave information
mysql> reset slave all ;
When we restore Slave from backup data, the previous slave information may remain so in that case, clear it once with this command and then paste master and replication.
- MySQL check if we use innodb or myisam
mysql> use information_schema ;
mysql> select table_name,engine,table_schema from tables where table_schema = "Database_name" ;
More info about differences between INNODB and MYISAM
- MySQL Logs
tail -f /var/log/mysql/mysqld.log
Define the log defined in /etc/my.cnf
. and check log when something happens is important!
- MySQL start replication
mysql> \e
CHANGE MASTER TO
MASTER_HOST = '192.168.123.123' ,
MASTER_USER = 'repilication_user' ,
MASTER_PASSWORD = 'password' ,
MASTER_PORT = 3306,
MASTER_LOG_FILE = 'Master_Log_File' ,
MASTER_LOG_POS = Exec_Master_Log_Pos ;
# Press ESC when done: wq
> ;
\e
is convenient because you can edit the input command like vi.
Enter the replication destination IP for host, let’s enter the user for replication in user. I first execute mysql> reset slave all;
to clear the slave information once and then I have to do mysql> start slave;
- MySQL database import
mysql> create database testdb
mysql> \q
vagrant@vagrant:~$ mysql -uroot testdb <test_database.sql
- MySQL backup of all databases
vagrant@vagrant:~$ mysqldump -uroot --all-databases --single-transaction > all_databases.sql
--single-transaction
backs up the range of transactions without locking the table.
- MySQL backup one database
vagrant@vagrant:~$ mysqldump -uroot --quote-names --opt --single-transaction --master-data = 2 --hex-blob -R --order-by-primary example_database> db_backup.sql
Explanation:
--master-data=2
: After being restored with the data of mysqldump, from what line of which file in binlog, it writes to the dump file which should be restored. By setting 2
(starting acquisition from the second line), the “CHANGE MASTER TO” syntax at the beginning is eliminated.
It’s required if you just take a backup.
--order-by-primary
: Explicitly sort by primary key
-hex-blob
: Converts binary data into hexadecimal notation string and outputs
-r
: Dump remembered routines from the dumped database.
--quote-names
Eat database, table and column names with characters ‘ ‘
--opt
: Enabled by default. Provides fast dump operations and produces dump files that can be quickly reloaded into a MySQL server.
Disks & partitions
- Show second disk in system
vagrant@vagrant:~$ parted -l | grep sdb
- Create new partition
vagrant@vagrant:~$ sudo parted /dev/sdb
( parted ) mklabel gpt
( parted ) mkpart primary 0% 100%
( parted ) set 1 lvm on # partition #1 with lvm flag Let's
( parted ) print
Since sdb1
has been created, create a file system, let’s make a logical volume with high flexibility, not a physical volume. For volume groups, use “Volum” at the beginning but for logical volumes, “Logic” at the beginning I thinks so is easy to understand later.
vagrant@vagrant:~$ sudo pvcreate /dev/sdb1
vagrant@vagrant:~$ sudo vgcreate VolumGroupName /dev/sdb1
vagrant@vagrant:~$ sudo lvcreate -l 100%free -n LogicName VolumGroupName
- Allocate a file system.
vagrant@vagrant:~$ sudo mkfs -t ext4 /dev/VolumGroupName/LogicName
Almost done so now just mount it.
vagrant@vagrant:~$ sudo mkdir /NewDisk
vagrant@vagrant:~$ sudo vim /etc/fstab
/dev/mapper/VolumGroupName/LogicName /NewDisk ext4 defaults 1 1
vagrant@vagrant:~$ sudo mount /NewDisk
⚠️ Because it is a logical volume, never register with UUID ⚠️
Delete Disk
- Delete logical volume
vagrant@vagrant:~$ sudo lvremove /dev/VolumGroupName/LogicName
- Delete Virtual Volume
vagrant@vagrant:~$ sudo vgreomove VolumGroupName
- Delete physical volume. Pay attention to sdb and sdc.
vagrant@vagrant:~$ pvdisplay
vagrant@vagrant:~$ sudo pvremove /dev/sdb1
After this we can use freely fdisk -cu
.
Other useful quick notes
Conclusion
It seems to me that this set of commands will be very useful for every engineer who works with Linux systems infrastructure on a daily basis