Linux commands for Automation

1. System/Hardware Information

# Display Linux system information
admin@ubuntu:/$ uname -a
Linux nextzen1 4.15.0-88-generic #88-Ubuntu SMP Tue Feb 11 20:11:34 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

# Display the IP addresses of the host
hostname -I
ifconfig

# Display CPU/memory information
cat /proc/cpuinfo
cat /proc/meminfo

2. Performance monitoring and statistics

# Display and manage the top processes
top

# Interactive process viewer (top alternative)
htop

# Display free and used memory ( -h for human readable, -m for MB, -g for GB.)
free -h

# Capture and display all packets on interface eth0
tcpdump -i eth0

# Show free and used space on mounted filesystems
df -h

$ df -T -h ### Show type of each filesystems listed such as ext4, btrfs, ext2, nfs4, fuse, cgroup, cputset, and more
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda       btrfs  2.8T   67G  2.7T   3% /data

# Display disks partitions sizes and types
fdisk -l

# Display disk usage for all files and directories in human readable format
du -ah

# Display total disk usage off the current directory
du -sh

# Display the uptime of the server
$ uptime
 02:21:57 up 51 days, 14:36,  1 user,  load average: 0.00, 0.00, 0.02

3. File and Directory COMMANDS

# List all files(include hiden files) in a long listing (detailed) format
ls -al -t(modified time)

# Create a directory
mkdir directory

# Remove (delete) file
rm file

# Remove the directory and its contents recursively
rm -r directory

# Force removal of file without prompting for confirmation
rm -f file

# Forcefully remove directory recursively
rm -rf directory

# Copy file1 to file2
cp file1 file2

# Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2
mv file1 file2

# Create an empty file or update the access and modification times of file.
touch file

# View the contents of file
cat file

# Browse through a text file
less file

# Display the last 10 lines of file and "follow" the file as it grows.
tail -f file

4. Remove all files in subdirectories:

 thachhoangn$ find . -name \*.txt -type f -delete

The -delete option means find will directly delete the matching files.
Using -type f means find will only process files.

5. SEARCH

# Find files and directories by name
locate name

# Find files in /home/user that start with "prefix".
find /home/user -name 'prefix*'

# Find files larger than 100MB in /home
find /home -size +100M

6. SSH to a server without entering password.

  • Create Authentication SSH-Kegen Keys on machine 192.168.0.12 where you run ssh command.
  • Create .ssh Directory on ssh server 192.168.0.11:
    • mkdir -p .ssh
  • Upload Generated Public Keys
    • Use SSH from server 192.168.0.12 and upload new generated public key (id_rsa.pub) on server 192.168.0.11 under .ssh directory as a file name authorized_keys
    • cat .ssh/id_rsa.pub | ssh sheena@192.168.0.11 ‘cat >> .ssh/authorized_keys’
  • Set Permissions
    • ssh username@192.168.0.11 “chmod 700 .ssh; chmod 640 .ssh/authorized_keys”
  • Login to 192.168.0.11 Server without Password:
    • ssh username@192.168.0.11

7. How to kill a process running on particular port in Linux.

To list any process listening to the port 8080:

lsof -i:8080

To kill any process listening to the port 8080:

kill $(lsof -t -i:8080)

or more violently:

kill -9 $(lsof -t -i:8080)

8. Free up space

refer to this link

9. How to Keep Your SSH Sessions Alive

Login as root and change two settings below:

$vi /etc/ssh/sshd_config
ClientAliveInterval 0
#ClientAliveCountMax 3

ClientAliveInterval:
number of seconds that the server will wait before sending a null packet to the client (to keep the connection alive). This is the limit of how long a client are allowed to stay unresponsive before being disconnected. Setting a value of 0 (the default) will disable these features so your connection could not be dropped if it is idle too long.

ServerAliveCountMax
Sets the number of server alive messages (see below) which may be sent without ssh(1) receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session.
you can run $man sshd_config to read more details about these settings.

Another example:
/etc/ssh/sshd_config:
ClientAliveInterval 300
ClientAliveCountMax 2

These settings will make the SSH client or server send a null packet to the other side every 300 seconds (5 minutes), and give up if it doesn’t receive any response after 2 tries, at which point the connection is likely to have been discarded anyway.

10. Running  a command with sudo without entering password.

[admin@Thachsystem ]$ sudo /usr/share/hnthach/install_build.sh

for example, i want to run the script above in admin user. There are some commands in that script required sudo. So to run it , I need to do the following steps:

– Create the file installation in  /etc/sudoers.d/ directory.

–  Add one line to installation file:

[admin@Thachsystem ]$ sudo visudo -f /etc/sudoers.d/installation
%admingrp ALL=(ALL) NOPASSWD:/usr/share/hnthach/install_build.sh

the syntax is:  %groupname host = (user privileges ) NOPASSWD: /sbin/shutdown

admingrp is group name, this means that all member of the admingrp group will run this command using sudo without a password.

You can also use /etc/sudoers  instead of modifying /etc/sudoers.d. But i would prefer sudoers.d.  This is good way to separate customized changes to sudo rights and leaves the original sudoers file untouched.

If you would not consider security risk , there is a simple way to run  a sudo command in bash script:

echo [your_password_here] | sudo -S [your_command_here]

Leave a comment