Tag Archives: Linux

who command in Linux with Examples

Introduction

who command Displays a list of users who are currently logged into the computer? The who command in Linux is used to display information about users who are currently logged in to the system. It provides details such as username, terminal, login time, and remote host. Here are some examples of using the who command:

Linux offers a robust set of tools for managing and monitoring user sessions and system activity. Among these, the who command stands out as a simple yet powerful utility. Whether you’re a system administrator checking logged-in users or a developer troubleshooting access issues, understanding the who command is essential. This guide dives deep into the who command, explaining its functionality, use cases, and examples to empower Linux users of all levels.

What Is the who Command in Linux?

The who command provides details about users currently logged into the system. By running this command, you can obtain valuable insights such as:

  • Usernames of logged-in users
  • Terminal session details
  • Login times
  • Hostnames or IP addresses

Its simplicity and efficiency make it a staple tool for Linux professionals.

How to Use the who Command in Linux

Basic Syntax

The syntax for the who command is straightforward:

On the man page, the describes it

  • who – show who is logged on.
  • who – More details information.

who command in Linux with Examples?

$ who

Show only the username and login time:

who -u

Include the idle time of logged-in users:

who -u -i

Show the IP addresses of remote hosts:

who -a

Show the process ID associated with each login session:

who -p

Frequently Asked Questions (FAQ)

What is the difference between who and w?

  • The who command shows currently logged-in users, while the w command provides additional details about what each user is doing.

Can I use the who command on macOS?

Yes, the who command works on macOS as it is a Unix-based system, though some options may vary.

How does who differ from users?

The users command lists usernames of logged-in users in a single line without additional details.

Conclusion

who command is the simple command in It is the most popular in-use terminal Linux show who is logged on.

These are some common examples of using the who command. There are additional options available, which can be explored by referring to the command’s manual page using man who or by checking the command’s help using who --help. Thank you for reading the DevopsRoles page!

Mastering the users command in Linux with Examples

Introduction

In Linux, user management is a fundamental part of system administration, especially when it comes to monitoring active user sessions. The users command is one of the simplest yet effective tools for this purpose. With just a single command, you can instantly view who is logged into your system, making it a valuable utility for Linux administrators.

In this article, we will delve deep into the users command -its basic syntax, practical use cases, and advanced examples that show how you can automate user session monitoring. Whether you’re a beginner or an experienced administrator, you’ll find useful insights into how this command fits into everyday Linux administration.

What is the users Command in Linux?

The users command is a Linux utility that displays the usernames of all users currently logged into the system. It fetches this data from the /var/run/utmp file, which keeps track of all active sessions. This simple tool is invaluable for monitoring who is using the system at any given moment, especially when maintaining or troubleshooting multi-user environments.

Why Use the users Command?

The users command is useful for:

  • Quickly checking which users are currently logged in.
  • Monitoring user sessions during system maintenance.
  • Tracking user activities for security purposes.

While it seems straightforward, the users command can be enhanced and combined with other Linux commands to create powerful user management tools.

Basic Syntax of the users Command

The basic syntax for the users command is extremely simple:

users

When you run this command, it will display a space-separated list of usernames currently logged into the system. No options or additional parameters are required.

Example

$ users
root admin user1

This example shows that three users – root, admin, and user1 – are currently logged in.

Practical Use Cases of the users Command

Now, let’s explore some practical use cases where the users command comes in handy.

Example 1: Basic Usage

To see all currently logged-in users, simply run:

$ users
john mary admin

This provides a quick overview of who is using the system.

Example 2: Combine with the who Command for Detailed Info

If you need more information, like the terminal or login time, you can use the who command. The who command provides detailed user session data such as the terminal (pts/), login time, and remote host IP:

$ who
john     pts/0        2023-10-05 09:15 (192.168.1.100)
mary     pts/1        2023-10-05 09:20 (192.168.1.101)
admin    pts/2        2023-10-05 09:25 (192.168.1.102)

This shows the login times and other session details, which can be useful for system monitoring.

Count the number of logged-in users:

users | wc -w

By piping the output of the users command to wc -w, we can count the number of words in the output, which corresponds to the number of logged-in users.

Check if a specific user is logged in:

users | grep <username>

Check if a user is logged in and display a custom message:

if users | grep -q <username>; then echo "<username> is logged in"; else echo "<username> is not logged in"; fi

Advanced Examples with the users Command

The true power of Linux commands comes from combining simple commands with more advanced tools or integrating them into scripts. Let’s look at some advanced examples of using the users command.

Example 3: Count the Number of Logged-In Users

To count how many users are currently logged into your system, combine the users command with wc -w (word count):

$ users | wc -w
3

This counts the total number of words (usernames) output by the users command, showing that three users are logged in.

Example 4: Search for a Specific User

If you want to check if a specific user, like john, is logged in, you can use grep with the users command:

$ users | grep john
john

If john is logged in, his name will appear in the output. If he is not logged in, you’ll get no output.

Example 5: Monitor Users Continuously with watch

The watch command allows you to continuously execute the users command, refreshing every two seconds by default:

watch users

This command will update the list of logged-in users in real time, which is useful for monitoring systems under heavy use or tracking session changes during maintenance.

Automation and Scripting with the users Command

The users command can also be integrated into shell scripts for more complex automation tasks, such as user activity tracking or system monitoring.

Example 6: Create a Shell Script to Alert When Multiple Users Are Logged In

Below is a simple shell script that checks the number of logged-in users and sends a warning if there are more than three users:

#!/bin/bash

logged_in_users=$(users | wc -w)

if [ $logged_in_users -gt 3 ]; then
    echo "Warning: More than 3 users are logged in!"
fi

This script is a basic example of automating system monitoring by counting users and triggering an alert based on specific conditions. You can customize this further to send alerts via email or log events.

Additional Commands for User Monitoring

While the users command is excellent for a quick snapshot of logged-in users, you may need more detailed insights in some scenarios. Here are a few other commands that complement the users command:

1. The who Command

The who command offers more comprehensive information about logged-in users, including the terminal, login time, and remote IP:

$ who

2. The w Command

The w command displays not only the list of logged-in users but also what they are currently doing. It shows CPU usage, terminal details, and more:

w

Example Output:

09:23:31 up 1:12,  3 users,  load average: 0.08, 0.05, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
john     pts/1    192.168.1.100    09:21    1:12   0.01s  0.00s bash
mary     pts/2    192.168.1.101    09:23    0.02s  0.03s  0.01s vim
admin    pts/3    192.168.1.102    09:24    0.03s  0.02s  0.01s sshd

3. The last Command

The last command displays a log of all user login and logout events. It is useful for auditing and tracking user activity over time:

$ last
john     pts/0    192.168.1.100    Thu Oct  5 09:15   still logged in

Frequently Asked Questions (FAQs)

What does the users command do?

The users command in Linux shows a list of all users currently logged into the system by fetching data from the /var/run/utmp file.

Can the users command show login times or session details?

No, the users command only displays usernames. For more details like login times, use the who or w command.

How do I count the number of logged-in users?

You can count the number of logged-in users by using the users command combined with wc -w:

users | wc -w

Can I monitor users in real-time?

Yes, you can use the watch command with the users command to monitor logged-in users in real-time:

watch users

Conclusion

The users command in Linux is a versatile tool for monitoring logged-in users and tracking session activity. Though it provides only basic information, it can be combined with other commands like who, w, and last for more detailed insights. Advanced users can even automate tasks by integrating the users command into shell scripts to create custom monitoring solutions.

Whether you are just starting with Linux or managing a complex server environment, understanding how to use it efficiently will make you a better system administrator. Explore the commands and examples shared here, and enhance your ability to manage user sessions effectively.. Thank you for reading the DevopsRoles page!

Angular build production Deployment on Linux Servers

Introduction

How to use Angular build production on server Linux VPS. Deploying Angular applications in a production environment requires a strategic approach to optimization and server configuration. This guide will delve into best practices for building Angular apps for production, emphasizing effective command-line techniques and server setup to enhance performance and stability.

Angular build production

In development, you have run the ng serve command for your application. What about Angular production? If you look at package.json the file below

Now, To build the script use the Angular CLI ng build with the –prod flag as below

$ ng build --prod

The during run “build –prod” also creates a new folder called dist folder. You need to have server Nginx or Apache for all requests to this index.html

How to configure Nginx in production to serve an Angular app

server {
listen 80;
listen [::]:80 ipv6only=on;
if ($host = www.devopsroles.com) {
return 301 https://$host$request_uri;
}
if ($host = devopsroles.com) {
return 301 https://$host$request_uri;
}
server_name www.devopsroles.com devopsroles.com;
return 444;
}

server {
server_name www.devopsroles.com devopsroles.com;
root /var/www/devopsroles.com/dist/devopsroles;
index index.html index.htm;
access_log off;
error_log /var/www/devopsroles.com/logs/error.log;

location / {
try_files $uri $uri/ index.html;
}
location ~ ^/(scripts.*js|styles|images) {
gzip_static on;
expires 1y;
add_header Cache-Control public;
add_header ETag "";

break;
}
listen 443 ssl http2 ; # managed by Certbot
listen [::]:443 ssl http2;
ssl_certificate /etc/letsencrypt/live/devopsroles.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/devopsroles.com/privkey.pem; # managed by Certbot
}

Conclusion

With the right setup and commands, you can seamlessly transition your Angular application from development to a production-ready state on Linux servers. By adhering to the outlined strategies, developers can ensure their applications are optimized for efficiency and ready for real-world deployment.

Through the article, You can use “Angular build production” as above. I hope this will be helpful to you. Thank you for reading the DevopsRoles page!

Complete Guide to Install Development Tools on CentOS

Introduction

This guide provides a comprehensive overview of installing essential development tools on CentOS. By using the yum groupinstall command, users can efficiently manage and install packages required for development, such as GNU GCC C/C++ compilers. This process is crucial for developers working in CentOS environments, aiming to streamline their setup and increase productivity.

In this tutorial, I used Centos “install development tools”.  The Ubuntu distribution install method is equivalent to “apt-get build-essential, while the Centos called groupinstall with yum command. How do I install all developer tools such as GNU GCC C/C++ compilers and others? You need to “install Development Tools“.

  • Development Tools for CentOS: Overview of tools and installation via Yum.
  • Development Tools for RHEL 7: Recommended tools and installation tips for Red Hat Enterprise Linux.
  • Development Tools for Ubuntu: Effective tools for Ubuntu and installation using apt-get.
  • Comparing Toolsets Across OS: Differences and similarities in toolkits across CentOS, RHEL 7, and Ubuntu.

How to Install Development Tools on Centos

For Centos, RHEL, and Fedora

Installing groupinstall use the yum command on Centos, RHEL, and Fedora.

[huupv@huupv devopsroles]$ sudo yum groupinstall "Development Tools"

The related with the subcommand  Group list

A list of groups uses “grouplist” command on Centos, RHEL, and Fedora.

[huupv@huupv devopsroles]$ sudo yum grouplist

To install a unit of a group name.

[huupv@huupv devopsroles]$ sudo yum groupinstall "Group name"

To remove a unit of a group using “groupremove” command on Centos, RHEL, and Fedora.

[huupv@huupv devopsroles]$ sudo yum groupremove "Group name"

Updating a unit of a group by “groupupdate” command on Centos, RHEL, and Fedora.

[huupv@huupv devopsroles]$ sudo yum groupupdate "Group name"

Conclusion

By following the steps outlined in this tutorial, users can successfully install all necessary development tools on CentOS, ensuring a robust environment for programming and development tasks.

This guide simplifies the process, making it accessible even for those new to Linux systems, ultimately enhancing their capabilities in handling various software development requirements. I hope will this your helpful. Thank you for reading the DevopsRoles page!

How to kill specific processes in Linux

Introduction

This guide will walk you through how to kill specific processes in Linux, from basic commands to advanced techniques, with practical examples. Whether you’re new to Linux or looking to refine your skills, mastering these commands can save you time and enhance system stability.

Linux is a powerful and flexible operating system, used in a variety of environments, from personal desktops to complex server systems. One essential aspect of managing a Linux system effectively is understanding how to handle running processes, especially when they behave unexpectedly or consume too many resources.

Why Kill Processes in Linux?

Processes in Linux are essentially tasks or programs that are currently running on the system. Sometimes, these processes can become unresponsive, hog system resources, or even cause the system to crash. In such cases, killing the process becomes essential to free up resources and maintain the smooth operation of the system. This article provides a comprehensive look at various ways to kill processes based on different criteria, allowing you to manage your Linux system with greater efficiency.

How to View Running Processes

Before killing any process, it’s crucial to know how to view the processes running on your system. You can use several commands to list active processes.

Using ps

The ps command provides a static snapshot of all currently running processes:

ps aux
  • a: Shows processes for all users.
  • u: Displays processes in a user-oriented format.
  • x: Lists processes not connected to a terminal.

Using top and htop

The top and htop commands give a real-time view of running processes.

top

For a more user-friendly interface, install and use htop:

htop

kill specific processes in Linux

For example, kill all PID of the browser Firefox. But not kill line “grep –color=auto firefox” as the picture below

Use ps command with “-ef” option the display PID of browser Firefox.

[huupv@huupv devopsroles]$ ps -ef | grep firefox

The only display PID of Firefox as command below

[huupv@huupv devopsroles]$ ps -ef | grep firefox | grep -v "grep" | awk '{print $2}'

The screen output terminal as below

Using kill command to kill all Processes for Firefox as command line below

[huupv@huupv devopsroles]$ sudo kill -9 $(ps -ef | grep firefox | grep -v "grep" | awk '{print $2}')

Frequently Asked Questions (FAQ)

How do I kill a process without knowing its PID?

You can use pkill or killall to kill a process by name.

What’s the difference between kill and kill -9?

The default kill (SIGTERM) requests a graceful shutdown, while kill -9 (SIGKILL) forcefully stops the process.

Is there any risk in using kill -9?

kill -9 terminates a process immediately without cleanup, so unsaved data may be lost. Use it only when necessary.

How can I kill processes that belong to another user?

To kill processes owned by another user, you need root privileges. Use sudo pkill -u [username].

Why is xkill not working on my Linux distribution?

Some distributions don’t have xkill installed by default. You can install it using your package manager.

External Resources

Conclusion

Managing processes in Linux is a fundamental skill that improves your efficiency and control over the system. This guide has covered essential commands for killing specific processes, from using kill and pkill to more advanced techniques. Practice these commands to confidently handle any unresponsive or resource-consuming processes on your system. Remember to exercise caution, especially with kill -9, and ensure you understand the implications of terminating critical processes. By mastering these techniques, you’ll be better equipped to maintain a smooth-running Linux environment. Thank you for reading the DevopsRoles page!

Bash string comparison

Introduction

In Bash scripting, comparing strings is an essential skill that allows you to check and manipulate input data. Unlike other programming languages, Bash has its own syntax and rules for handling string comparisons. In this article, we will explore how to use conditional expressions to compare strings in Bash. We will learn about common comparison operators, string handling techniques, and how to test strings under different conditions.

This guide aims to help you grasp the basics and provides practical examples that you can apply to your daily tasks. How do I use bash string comparison? In the bash shell use the if statement “==” to check equality and “!=” to check the inequality of the string. String comparison examples. The bash script is essential for DevOps Roles.

In bash, you can compare strings using various operators. Here are some common string comparison operators in bash:

  • = : Checks if two strings are equal
  • != : Checks if two strings are not equal
  • -z : Checks if a string is empty (has zero length)
  • -n : Checks if a string is not empty
  • < : Checks if one string is less than another string (in lexicographical order)
  • > : Checks if one string is greater than another string (in lexicographical order)

Bash string comparison use “==” operator

#!/bin/bash
STRA=huu
STRB="www.devopsroles.com"
if [[ "$STRA" == "$STRB" ]]; then
   echo "$STRA equal $STRB"
else
   echo "$STRA not equal $STRB"
fi

The screen output terminal:

Bash script string compare use “!=” operator

#!/bin/bash
STRA=huu
STRB="www.devopsroles.com"
if [[ "$STRA" != "$STRB" ]]; then
   echo "$STRA not equal $STRB"
else
   echo "$STRA equal $STRB"
fi

The screen output terminal:

Bash script string compare use wildcards

#!/bin/bash
STRA=huu
STRB="www.devopsroles.com"
if [[ "$STRA" == *$STRB* ]]; then
   echo "$STRA equal $STRB"
else
   echo "$STRA not equal $STRB"
fi

The screen output terminal:

Examples of string comparison in bash

Conclusion

In summary, comparing strings in Bash is a crucial skill that every Bash programmer needs to master. By using comparison operators and conditional expressions, you can effectively and accurately perform string checks and manipulations. Understanding how to compare strings not only helps you write more powerful scripts but also improves your ability to handle data and automate complex tasks.

Hopefully, this article has given you a comprehensive overview and the necessary knowledge to apply to your Bash projects. I hope will this your helpful. For more details refer to the Bash script.