Saturday, January 11, 2020

How to use the screen command on Linux to keep your remote task running when the connection drops

https://net2.com/how-to-use-the-screen-command-on-linux-to-keep-your-remote-task-running-when-the-connection-drops

A lot of Linux users carry out a long running task on a remote destination without taking into account the possibility that the connection might break. In such situations, notwithstanding financial losses for large and small corporations, they will lose their work as well as their time since when they try to reconnect again, the remote task is no longer running.
Fortunately, Linux provides a tool called ‘screen’ which enables these remote sessions to keep running even when the connection drops. Indeed, when a given process is started with this utility, i.e. ‘screen’, it can be detached from the corresponding session and then when the connection is resumed for instance, the process that was originally started from the ‘screen’ is still running and can rejoin the session at a later time, .i.e users can pick up where they left off with their last command or task.
In simple terms, when a user starts a screen session and opens up multiple virtual terminals within that session, the running processes will not be affected by a disconnection event.

What is screen ?

The ‘screen’ utility is a window manager that is used to multiplex a physical terminal between multiple processes or interactive shells. When the screen command is called, a single window will show up where the user can work normally.
Users have the ability to open as many screens as they need, switch between them, list them, kill them, detach them, copy and paste text between them and even reconnect to them.
All windows execute or manage their own programs independently of each other. When a window is not visible, its corresponding programs continue to run even when the full screen session is detached or decoupled from the user’s terminal.

Installing Linux screen

On most Linux distros, the screen utility is already pre-installed. To check if screen is installed on your Linux system , type in the following command for Ubuntu and similar distros:
screen –version

As you can see from the snapshot above, screen is not installed on our Ubuntu 18.04. You can install it by running the apt command :
sudo apt install screen

Start using screen

In order to start your first screen session, type in screen in your terminal:
screen
You will be greeted with the following welcome message :

This will create a screen session, open a new window and finally start a shell in that window.
You may want to read: How to use scp command to transfer files securely using ssh on Linux

List of all currently opened screens

To find the list of all currently opened screens including those that are running in the background, issue the command below in your terminal :
screen -ls

The highlighted number above, .i.e 8585, identifies the PID of the screen session.
If you type in the command below :
ps -e | grep 8585

You will see that it is indeed a screen session process.

Creating a named screen

From the snapshot below, the title of the session is not very descriptive.

Since it is good practice to choose a memorable session name to remember what screen is executing what command, run the command below:
screen -S command_name
Where command_name is the command that would be run by the session. Otherwise you can just choose a name for the session per se as shown below :

When you hit enter, a second screen session will be created. To see the number of currently active screen sessions , use the command you used earlier :

Detaching from a screen

In order to detach yourself from the current screen, Press Ctlr+a d;

Notice that if you quit the terminal altogether and then open it up, you will see that the screen session is still running.
If case you need to quit the terminal, but you want your tasks to continue running in the screen session, you can detach the screen manually in order to reconnect with it later using Ctrl+a d.

Reattaching to a screen

In order to reattach to a screen session, issue the command below in which the PID of the detached session would need to be provided :
screen -r Screen_PID

Once you hit enter, you will automatically get reattached to the screen session you had indicated.
You can also, instead of providing the PID, use the name (or the first few characters of the name) of the screen session, for instance:
screen -r mysec
Where mysec is the beginning of the name of the second screen session called: mysecondcreensession.

How to quit an active session ?

In case you want to kill a screen session, you would need first to display the list of active screen sessions in order to identify the session to terminate :
screen -ls

Now we want to quit session with PID 8585 as shown above. To do this we use the command below :
screen -X -S 8585 quit

where you can see that the screen session with PID 858 was terminated.
Let us now attach to the only screen session left, i.e. with PID 10313 :

In order to kill the current screen session from within it, press Ctrl+a k. You will be prompted with the question :

Go ahead and choose y. This will terminate the screen (actually this will kill the window, but since the screen has one window, it will get terminated).


In order to exit a one-window screen, you could simply type in the exit command which will take you back to the standard bash prompt.

Creating multiple windows in a screen

To create several windows within your existing screen, simply press Ctrl-a followed by c. In order to see the list of windows in your current screen session, press Ctrl-a followed by w.

You can also display the screen windows using ‘Ctrl-a :’

Here you can scroll up or down in order to move between your desired windows and then hit enter once you decide to land on the selected window.

Renaming a window

If you want to rename a window, make sure you are already on the correct window that you want to rename and press Ctrl-a A. This will display the prompt below which enables you to rename the window :

Switching between screen windows

When more than one screen window is opened, you can switch between them by pressing Ctrl-a followed by space or the shell number, .i.e. just press Ctrl-a 0, Ctrl-a 1, Ctrl-a 3 if you have three windows and so forth.
You can also use Ctrl-a N to move to the next screen or Ctrl-a P to return to the previous screen.

Splitting windows

You have the possibility to split your windows instead of switching between them in order to have a bird view of your work. This can be achieved by pressing Ctrl-a followed by S or | for horizontal or vertical splitting. To move between panes, use Ctrl-a followed by tab. Here is how splitting a screen session will look like :

Resuming a disconnected session

In case you want to resume a detached session to continue your work, run the command :
screen -d -r your_detached_ression
This is helpful when you want to continue your work after a disconnection event for instance without losing the detached screen session context (running processes , window output).

How to run a command with screen

When you have a command to that you want to run using screen, issue the command below :
screen -d -m your_command
For instance to run a Python script:, issue the command below:
screen -d -m python your_python_script

The screen configuration file

When you first call screen, it reads its actual configuration parameters from the file /etc/screenrc and ~/.screenrc if it is available. You can alter the default screen settings using the .screenrc file.

Conclusion

In this tutorial, you were introduced to the Gnu screen utility. Now you should be able to use the screen tool, create screen windows, navigate between them, detach and resume screen sessions as well as customize your screen display using the .screenrc file.
To learn more about Gnu screen, visit the screen user’s manual page.

How to copy a file to multiple directories in Linux

https://net2.com/how-to-copy-a-file-to-multiple-directories-in-linux

In this short article, you will learn how to copy one file into many directories. The obvious answer would be to use the cp command but this not is exactly correct since the cp command is rather used to copy several files into one directory. You will see what additional commands and combinations are needed to pull this off. Let’s get started.

The cp command basic syntax

The most basic cp command syntax that is used to copy multiple files into one directory is the following :
cp file_1 /directory_1/
cp file_1 file_2 file_3 /directory_1/
Here is an example :
cp /Documents/FileExample.txt /TextFiles/
cp command

How to copy one file to several directories ?

As mentioned above, the cp command cannot be used to copy one file to multiple folders. The solution would be to use the xargs or GNU parallel commands.

Using xargs

To copy for instance file_1 into the folders directory_1, directory_2, we could proceed as follows:
echo directory_1 directory_2 | xargs -n 1 cp file_1
In the command above, target directories (directory_1,directory_2) are first echoed and then piped out (or fed) as input to the command xargs where:
-n 1 : Instructs xargs to use one argument per command line at a time and forward to the cp command
cp : classic cp command
-v : Allows verbose mode in order to display more details of the copy task
The xargs will execute the cp command two times (i.e. as many target directories as provided in the input) where at each run, it appends the next directory path fed to it from the previous echo command to the end of the standard cp command.
So instead of executing two separate cp commands, we can now use one single command to perform the same task. If the file to be copied exists already in one of the destination folders, the old file will be replaced without prompting the user. In other words, the -i option (interactive) of the cp command cannot be used in conjunction with xargs.
If the file to be copied has a large size and you do not want the destination file (if it exists) to be replaced, you might want to add the -n switch to the cp command in the single line above. This will prevent the destination file from being replaced.
Read: How to use grep command in Linux


Using find

Another alternative to carrying out a copy to multiple destinations is to use the find command as follows :
find directory1 directory2 -exec cp file.txt {} \;
If the target directories have sub-directories and you don’t want to copy the file into them, you would need to add -maxdepth 0 option as follows:
find directory1 directory2 -maxdepth 0 -exec cp file.txt {} \;
This will overwrite or replace every file in directory1 and directory2 with the content of file.txt before copying it. In order not to affect other files in these destination directories, make find aware that it should only act on directories as follows:
find directory1 directory2 -type d -exec cp file.txt {} \;
Read: Linux directories explained

Using loop in a shell

xfAnother solution would be to use a for loop within a one line shell as follows :
for dir in *; do [ -d “$dir” ] && cp /full_path/file.txt “$dir” ; done
This will copy the file /full_path/file.txt to all directories in your current path or location.
for dir in *; do [ -d “$dir” ] && cp -rf /full_path/folder “$dir” ; done
This however will copy the folder /full_path/folder to every sub-folder or sub-directory in your current location.
Read: How to find the largest files on Linux

Using GNU parallel

GNU parallel is a shell utility used for executing tasks or jobs in parallel over one or multiple machines. The basic syntax is as follows:
parallel cp file_name ::: /directory1/ /directory2/
Here is an example on how to use it :
Let’s copy the file /etc/resolv.conf to /directory1/, /directory2/ :
parallel cp -v /etc/resolv.conf ::: /directory1/, /directory2/

Using tee

The tee command allows you to copy one file to multiple destinations. Here is an example on how to perform this:
tee ~/directory1/file1 ~/directory2/file1 < ~/file1
Note that the input that is written by tee will be forwarded to the standard output (stdout). If you don’t like this behavior, you have the possibility to prevent it by rerouting standard output to /dev/null as shown below:
tee ~/directory1/file1 ~/directory2/file1 < ~/file1 >/dev/nul

Conclusion

As you have seen, there are many ways to copy a file to multiple directories. Most of these solutions make use of the cp command which cannot perform this feat on its own.

Thursday, February 14, 2019

3 Ways to List Users in Linux

https://linuxhandbook.com/linux-list-users

This tutorial shows you how to list users in Linux. You’ll also learn to list only the logged users.
Today different Operating Systems have the capability to use multiple users, each one with their settings and custom configurations to make things easier for administrators and operators to work in together on the same system.
Linux on the other hand is very strong on this matter as it allows multiple users to work at the same time on the system in an independent way. It can even allow a single user to open several sessions even from different locations in order to work on the system.
Here are some hints & tricks to handle users in Linux.

List all the users on Linux

How to List Users in Linux
Probably, the very first thing to know is how to know what users are in my system. There are several ways you can obtain the list of users in Linux.

1. Show users in Linux using less /etc/passwd

This command allows sysops to list the the users that are locally stored in the system. It will give the listing in structured way as:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
johndoe:x:1000:1000:John Doe,,,:/home/helder:/bin/bash
davmail:x:127:65534::/var/lib/davmail:/usr/sbin/nologin
statd:x:128:65534::/var/lib/nfs:/usr/sbin/nologin
/etc/passwd (END)
The structure in the above output goes as:
  • User name
  • Encrypted password (x represents password is stored)
  • User ID number (UID)
  • User’s group ID number (GID)
  • Full name
  • User’s home directory
  • User’s Login shell (default is bash shell)
Why so many users? Which ones are ‘real’?
The list shows a lot more users than you expected because it lists all the system users too.
Now if you want to distinguish the normal users from the system users, you can refer to the User ID (UID) number.
Generally, a normal user has UID greater or equal to 1000. This gives you a hint that the user with UID >=1000 is a normal user and users with UID <1000 are="" p="" system="" users.="">

2. View users using getent passwd

This command will give you a similar output as “less /etc/passwd” however, this one actually queries the GNU Name Service Switch functionality configuration file (located at /etc/nsswitch.conf).
This conf includes passwd, so that’s why it will display very similar but if you use LDAP for authentication it will include that as well.

3. List Linux users with compgen

If you just want to list all the usernames without any additional information, you can use the compgen command with -u option.
compgen -u
The output would be like this:
compgen -u
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-network
systemd-resolve
syslog
messagebus
_apt
uuidd
avahi-autoipd
usbmux
dnsmasq
rtkit
cups-pk-helper
speech-dispatcher
whoopsie
kernoops
saned
pulse
avahi
colord
hplip
geoclue
gnome-initial-setup
gdm
abhishek 
Tip
You can use compgen command with -c option to list all the commands available to you. This is helpful when you are not the admin on a Linux system and don’t have sudo access.

A few tips about listing users in Linux

You just saw three ways to view users in Linux. Here are a few tips that would help you while dealing with the users listing.

List only the usernames

You already have the compgen command for that but you don’t have to remember it all the time.
If we would like to only get a list of the usernames in the system, you can use the awk command or the cut command to filter the output of the other two commands we saw earlier.
cut -d: -f1 /etc/passwd
or
getent passwd | awk -F: '{ print $1}'
Any of these will give us a filtered list of users, showing only the very first column which is username:
root
daemon
bin
sys
sync
games
man
lp
mail
news
johndoe
davmail
statd

Check if a username already exists in the system

This might be useful if you want to know if a particular username already exists in the system:
getent passwd | grep johndoe
johndoe:x:1000:1000:John Doe,,,:/home/johndoe:/bin/bash

List all the connected users

If you want to know what users are currently logged into your system, then you need to perform a simple ‘who’ on your command line and this will immediately list current usernames with an active session to your system
user@system:~$ who
johndoe   :0           2019-01-28 21:35 (:0)
harrysmith   pts/0        2019-02-01 09:51 (192.168.1.1)
stevejones   pts/1        2019-02-02 09:51 (192.168.1.173)
In this case, the listing will give you not only the list of usernames connected but also how they are connected, since when they are connected and from where they are connected.
The very first column will tell you what username is it.
The second column will give you what type of connection it is: if it’s represented with a “:X” where X is a number, it means it is using a Graphical User Interface (GUI) or Desktop session such as Gnome, XDE, etc; if it says “pts/X” where X is a number, it means it’s a connection made through SSH protocol (command line).
The third column will tell you since when this session has been connected to the server (date and time). The fourth and last column will give you the location from where it’s connected, if remote it will display the IP from where the connection is made if local (like the GUI) it will display “(:X)” where X is the number of the session in this case and will match the number in the second column for that row.

Wrapping up

As you can see, listing users in Linux is not difficult at all. It consists of simple commands which will output all the information for you, whatever you want to do or obtain of that information is something you need to filter depending on what you want to check on the system.