scp command example: A Comprehensive Guide to Secure File Transfers

Introduction

How to use the SCP command in Linux to copy a file from one server to another. it is used to securely copy a file to or from a remote server.

Discover the simplicity and power of using the SCP command in Linux for secure file transfers. This tutorial will guide you through various examples of how to utilize SCP to copy files and directories between servers securely. Whether you are a beginner or an experienced administrator, mastering the SCP command is crucial for managing remote file transfers efficiently.

SCP command example

Copy file from one client location to another server location

$ scp /tmp/local.conf huupv@192.168.1.22:/home/huupv

Use the “-r” recursive option to copy the directory and sub-directories.

$ scp -r huupv@192.168.1.22:/home/huupv/* ./your-workspace

To copy the remote file to the local directory

$ scp huupv@192.168.1.22:/home/huupv/file-remote.txt /local/folder

Copying a local file to a remote directory

$ scp local-text.txt huupv@192.168.1.22:/home/huupv

Using key files like SSH command 🙂

$ scp -i your_key.pem local-text.txt huupv@192.168.1.22:/home/huupv

Conclusion

With the SCP command, transferring files between servers becomes a seamless task. This guide has shown you several practical examples to help you understand how to use SCP effectively. By integrating these techniques into your workflow, you’ll enhance your server management skills and ensure secure data handling. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Linux list services

In this tutorial, How do I  list all services running in Linux distribution? List running service on Ubuntu Or Systemd service management. Linux the essential for DevOps Roles.

Linux list services

Systemd service management

Listing services

Listing running services.

[huupv@huupv devopsroles]$ systemctl

The screen output terminal

Listing the “failed” services

[huupv@huupv devopsroles]$ systemctl --failed

The screen output terminal

Managing targets

To find the default target for your system.

[huupv@huupv devopsroles]$ systemctl get-default

Setting the default target for your system

$ sudo systemctl set-default <target-name>

Managing to autostart of services

Showing a service is enabled on system boot.

$ sudo systemctl is-enabled [service-name]

To show a service is currently active (running).

$ sudo systemctl is-active [service-name]

Enabling a service on system boot.

$ sudo systemctl enable [service-name]

Disabling a service on system boot.

$ sudo systemctl disable [service-name]

Managing services at runtime

Starting a service

$ systemctl start [service-name]

Stopping a service

$ systemctl stop [service-name]

Restarting a service

$ sudo systemctl restart [service-name]

To request the service to reload its configuration.

$ sudo systemctl reload [service-name]

Showing the current status of a service

$ sudo systemctl status [service-name]

To restart your Systemd.

$ sudo systemctl daemon-reload

List running services on the Ubuntu distribution

To get the list of the services on your system.

$ service --status-all

Conclusion

Through the article, you can use Linux list services as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

tar command examples in Linux

In this tutorial, How do I use the tar command the compress and extract files and folders in Linux? Linux the essential for DevOps Roles.

The tar command in Linux is used for creating and manipulating tar archives, which are commonly used for bundling multiple files and directories into a single file. Here are a few examples of how to use the tar command.

The syntax tar command

tar [Options] your_tar_name.tar source_tar

Options

-c –create Create a new archive.
-x –extract Extract files from an archive.
-t –list List the contents of an archive.
-f –file=ARCHIVE Use archive file or directories ARCHIVE.
-v –verbose Verbosely list files processed.
-a –auto-compress Use archive suffix to determine the compression program.
-j –bzip2 Filter the archive through bzip2.
-J –xz Filter the archive through xz.
-z –gzip Filter the archive through gzip.

For example, tar command examples, compress a directory.

Creating an archive of a directory as command below

[huupv@huupv devopsroles]$ tar -cvf folder.tar folder

The archiving a folder compressed “gzip” you can use -z option.

[huupv@huupv devopsroles]$ tar -czf folder.tar.gz folder

You can compress the archive with “bzip2” by using -j option

[huupv@huupv devopsroles]$ tar -cjf folder.tar.bz2 folder

Or compress “xz” by using the -J option.

[huupv@huupv devopsroles]$ tar -cJf folder.tar.xz folder

For example, Extract a directory from an archive

To extract a directory from an archive in the current location

[huupv@huupv devopsroles]$ tar -xvf folder.tar

Or to extract a directory from an archive to a specific “your_folder”.

[huupv@huupv devopsroles]$ tar -xvf  folder.tar -C ./directory/your_folder

For example list archive content

Listing content as command below

[huupv@huupv devopsroles]$ tar -tvf folder.tar

For listing the content of a tar.gz archive

[huupv@huupv devopsroles]$ tar -tzvf folder.tar.gz

Compress and exclude one or multiple directories

This is my folder tree

[huupv@huupv devopsroles]$ tree tar-folder
tar-folder
├── folder1
├── folder2
└── folder3

3 directories, 0 files

You can exclude one or several folders “–exclude” option as the command below

[huupv@huupv devopsroles]$ tar --exclude='./tar-folder/folder1' --exclude='./tar-folder/folder3' -cvf my-archive.tar ./tar-folder

The screen output terminal

./tar-folder/
./tar-folder/folder2/

Conclusion

Through the article, you can use tar command examples in Linux as above. These are just a few examples of how to use the tar command in Linux. The tar command offers many other options and functionalities, so you can refer to the command’s manual (man tar) for more detailed information and usage examples. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Linux System information and Hardware Information

How to get System Information about CPU, Memory, Network, and Disk I/O on the Linux operating system. Linux the essential for DevOps Roles.

To obtain system and hardware information on a Linux system, you can use various commands and utilities. Here are a few commonly used ones.

Linux System information

CPU

You can use the mpstat command to get processor-related statistics.

[huupv@huupv devopsroles]$ mpstat 2 10

The screen output terminal:

Using the lscpu command to get CPU information

[huupv@huupv devopsroles]$ lscpu

The screen output terminal:

Find CPU model/speed information

[huupv@huupv devopsroles]$ cat /proc/cpuinfo

Count processor

[huupv@huupv devopsroles]$ grep -c processor /proc/cpuinfo

DISK

To get information about your disk in real-time with the iostat command

[huupv@huupv devopsroles]$ iostat -kx 2

NETWORK

To see your network services with the netstat command

The command as below

[huupv@huupv devopsroles]$ netstat -ntlp # Open TCP sockets
[huupv@huupv devopsroles]$ netstat -nulp # Open UDP sockets
[huupv@huupv devopsroles]$ netstat -nxlp # Open Unix sockets

LIST HARDWARE

You can report the exact memory configuration, Mainboard configuration, Firmware version, CPU version, and speed cache.

Using lshw command

[huupv@huupv devopsroles]$ sudo lshw

To show PCI info

[huupv@huupv devopsroles]$ lspci -tv

Getting USB info

[huupv@huupv devopsroles]$ lsusb -tv

Display BIOS Infomation

[huupv@huupv devopsroles]$ sudo dmidecode -q | less

How to read speed test on disk sda

[huupv@huupv devopsroles]$ sudo hdparm -tT /dev/sda

The screen output terminal:

[sudo] password for huupv:

/dev/sda:
Timing cached reads: 5668 MB in 2.00 seconds = 2837.23 MB/sec
Timing buffered disk reads: 110 MB in 3.08 seconds = 35.73 MB/sec

Using badblocks command test for an unreadable block on disk sda

[huupv@huupv devopsroles]$ badblocks -s /dev/sda

Process monitoring

List processes in a hierarchy

[huupv@huupv devopsroles]$ ps -e -o pid,args --forest

How to list processes “% CPU” usage

[huupv@huupv devopsroles]$ ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'

List processes mem (KB) usage

[huupv@huupv devopsroles]$ ps -e -orss=,args= | sort -b -k1,1n

Interactive monitoring

You can use the tool for dynamic monitoring

[huupv@huupv devopsroles]$ top
[huupv@huupv devopsroles]$ htop
[huupv@huupv devopsroles]$ atop
[huupv@huupv devopsroles]$ sudo iotop

Conclusion

Through the article, you can use Linux system information and hardware Information as above. These are just a few examples of commands you can use to obtain system and hardware information on Linux. Depending on your distribution and installed packages, there may be additional tools available. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Linux check disk space

In this tutorial, How do I check disk space on Linux distribution? Sometimes you find out which directory consumes how much disk space. I used the du command and the df command. Linux the essential for DevOps Roles.

To check the disk space on a Linux system, you can use the df command. Here’s how you can do it:

  • Investigate the folder for Disk Usage
  • Checking Disk Space

Linux check disk space

Investigate the folder for Disk Usage

du command summarizes disk usage and recursively for files and directories

The syntax,

du [option]

For example, Summarizing disk usage in the current directory

[huupv@huupv devopsroles]$ du -sh *

The summarizing includes hidden files

[huupv@huupv devopsroles]$ du -sh .[!.]* *

you can add total the output by adding the -c option

[huupv@huupv devopsroles]$ du -csh .[!.]* *

The screen output terminal:

Investigate root director will only show folders with more than 20GB for disk usage

[huupv@huupv devopsroles]$ sudo du --threshold=20G -ch /.[!.]* /*

Checking Disk Space

[huupv@huupv devopsroles]$ df -h

Conclusion

Thought the article, you can use Linux check disk space as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Check Linux version

In this tutorial, How do I check Linux version for Linux distribution? Linux the essential for DevOps Roles.

Check RHEL / CentOS / Fedora distribution

Look at the content of “/etc/redhat-release”

[huupv@huupv devopsroles]$ cat /etc/redhat-release

The screen output terminal:

Check systemd-based distribution

This method will work on a modern version of Linux distribution.

[huupv@huupv devopsroles]$ cat /etc/os-release

Check Debian-based distribution

Run command lsb_release -a

$ lsb_release -a

From the bash shell,

[huupv@huupv devopsroles]$ ( source /etc/os-release && echo "$PRETTY_NAME" )

The screen output terminal:

To check the Linux version on your system, you can use the uname command with the -a option. Open your terminal and type the following command:

[huupv@huupv devopsroles]$ uname -a

This will display information about your Linux kernel, including the version. The output will typically look like this:

For example: The screen output terminal

Linux huupv 4.18.11-200.fc28.x86_64 #1 SMP Sun Sep 30 15:31:40 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Conclusion

Through the article, you can use the Check Linux version as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Jenkins run as non root user

In this tutorial, How do I Jenkins run as non root user in Linux? Jenkins the essential for DevOps Roles.

Jenkins run as non root user

To change the Jenkins user, Open “/etc/sysconfig/jenkins” file. Changing JENKINS_USER variable and Make sure user exists in your system.

The default, JENKINS_USER is Jenkins USER as below

[root@DevopsRoles ~]# cat /etc/sysconfig/jenkins | grep JENKINS_USER
JENKINS_USER="jenkins"

Changing JENKINS_USER to huupv USER.

[root@DevopsRoles ~]# sed -i -e 's/JENKINS_USER\="jenkins"/JENKINS_USER\="huupv"/g' /etc/sysconfig/jenkins

The screen output terminal:

[root@DevopsRoles ~]# cat /etc/sysconfig/jenkins | grep JENKINS_USER
JENKINS_USER="huupv"

The changing ownership Jenkins home

[root@DevopsRoles ~]# chown -R huupv:huupv /var/lib/jenkins 
[root@DevopsRoles ~]# chown -R huupv:huupv /var/cache/jenkins
[root@DevopsRoles ~]# chown -R huupv:huupv /var/log/jenkins

The restarted Jenkins and check user has changed with ps command.

[root@DevopsRoles ~]# /etc/init.d/jenkins restart
[root@DevopsRoles ~]# ps -ef | grep jenkins

The screen output terminal:

Conclusion

Thought the article, you can use Jenkins run as the non root user as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Bash script symbolic link

In this tutorial, How do I use the Bash script symbolic link? A symbolic link or a soft link. Much the same as the shortcut in Windows or a Macintosh alias. Bash script the essential for DevOps Roles.

Bash script Function get Symbolic link

#!/bin/bash
#Function get Symbolic link
getSymboliclink (){
  local FOLDER=$1
  echo "----------------- Symbolic link ------------------"
  find $FOLDER -type l -exec ls -l {} \; | awk '{print $1 " " $3 " " $4 " " $9 " -> " $11}'
}
getSymboliclink $1

The screen output terminal:

Conclusion

Thought the article, you can use the Bash script symbolic link as above. I hope will this your helpful. More details refer to Bash script .

Bash script create new file

In this tutorial, I use a bash script to create a new file. Trick and tips create folder and file with arguments in the bash script. Bash script the essential for DevOps Roles.

Bash script create new file

If “no such folder” then create folder and file.

My bash script

#!/bin/bash
function create-touch() {
  local FILE
  local FOLDER
  FILE=${@:$#}
  FOLDER="$(dirname "$FILE")"
  mkdir -p "$FOLDER" && \touch "$@"
}
create-touch $1 # Call Function in bash script

The screen output terminal:

Conclusion

Thought the article, you can use a Bash script to create a new file as above. I hope will this your helpful. More details refer to Bash script.

Ansible read remote file

In this tutorial, How to use Ansible to read a remote file? Ansible the essential for DevOps Roles.

Ansible read remote file using slurp module

I use the slurp module to read a remote file.

- hosts: server1
  tasks:
  - name: slurp file
    slurp:
     src: /home/vagrant/devopsroles
    register: slurp_remote_file

  - name: Read file
    debug:
     msg: "{{ slurp_remote_file['content'] | b64decode }}"

The terminal output as below

Ansible shell module

Using the shell module cat command to read a remote file

- hosts: server1
  tasks:
    - name: cat file
      shell: cat /home/vagrant/devopsroles
      register: cat_content_file

    - name: echo file
      debug:
        msg: "{{ cat_content_file.stdout }}"

The terminal output as below

Ansible fetch module

To read a remote file using Ansible, you can use the fetch module. The fetch module allows you to retrieve files from remote hosts and store them locally on the Ansible control machine.

Here’s an example task to read a remote file:

- name: Read remote file
  hosts: your_host
  gather_facts: false
  tasks:
    - name: Fetch remote file
      fetch:
        src: /path/to/remote/file.txt
        dest: /path/to/local/directory/

In this example, replace your_host with the target host or group of hosts where the remote file is located. Set the src parameter to the path of the remote file you want to read. Set the dest parameter to the local directory path where you want to store the fetched file.

Conclusion

Make sure you have proper SSH access and permissions to read the remote file on the target host(s) before running this playbook.

Through the article, you can use Ansible read remote file. I hope will this your helpful. For more details refer to Ansible tutorial.

Devops Tutorial

Exit mobile version