- How to run Linux Commands on Windows 10?
- Windows Subsystem for Linux(WSL)
- Linux Run Command As Another User
- 1. Using Linux runuser command as another user
- The runuser command options
- 2. Run a Linux command with substitute user and group ID using ‘su command’
- How To Run a Script In Linux
- Examples that shows how to run a script in Linux
- How to Run or Repeat a Linux Command Every X Seconds Forever
- 1. Use watch Command
- Monitor Memory Usage
- Monitor Logged-In Users, Uptime and Load Average
- Monitor Progress of Copy Command
- 2. Use sleep Command
- for loop Example
- while loop Example
- Conclusion
- If You Appreciate What We Do Here On TecMint, You Should Consider:
How to run Linux Commands on Windows 10?
It is a dilemma when you want to switch from one operating system to another. This is a common case when you switch from Windows to Linux. Either you store your data and then uninstall Windows to install Linux and then transfer your data or you can have a dual boot system where you encounter an option as to which OS to choose in the beginning. Don’t you think that both methods require a lot of work? No worries! What if you practice Linux commands on Windows itself? Yes, it will save a lot of work! Here are some ways to run Linux commands on Windows.
Windows Subsystem for Linux(WSL)
Step 1: Go to Update and Security in Settings.
Step 2: Go to the Developer’s Mode and Select the Developer’s Mode option.
Step 3: Open the Control Panel.
Step 4: Click Programs and Features.
Step 5: Click Turn Windows Features On or Off.
Step 6: Enable the Windows Subsystem for Linux Option in the List. Click OK. Click Restart Now to Reboot your Computer.
Step 7: Click the Start button (or press the Windows key), type bash, and press Enter.
Note: There are two problems that you might face after this step:
1. If you are seeing a message like:
“Windows Subsystem for Linux has no installed distributions. Distributions can be installed by visiting the Microsoft Store: https://aka.ms/wslstore
Press any key to continue…”
Then open command prompt and type “lxrun /install“. This will install Ubuntu on Windows.
Accept the terms after which Ubuntu bash will be downloaded from Windows store
2. If the bash is not opening or above solution not working then you can open Microsoft Store. Download and Install Ubuntu. Or click here to download and install the Ubuntu for Developer Tools Utilities.
Step 8: Create user Account and Password to use bash. Again click the Start button (or press the Windows key), type bash, and press Enter.
Источник
Linux Run Command As Another User
Dear nixCraft,
I just want to know how to run Linux commands as another user or as the root user?
–Sincerely,
Confused About Linux commands.
Dear Confused,
You can use the following commands to run as another user or as root user on Linux.
1. Using Linux runuser command as another user
Only session PAM hooks are run, and there is no password prompt. If run as a non-root user without privilege to set user ID, the command will fail as the binary is not setuid. As runuser doesn’t run auth and account PAM hooks, it runs with lower overhead than su.
For example, as a root user you may want to check shell resource limits for oracle user, enter:
# runuser -l oracle -c ‘ulimit -SHa’
Check nginx or lighttpd web server limitations:
# runuser -l nginx -c ‘ulimit -SHa’
# runuser -l lighttpd -c ‘ulimit -SHa’
Sometime, a root user can not browse NFS mounted share due to permission (security) issue:
# ls -l /nfs/wwwroot/cyberciti.biz/http
OR
# cd /nfs/wwwroot/cyberciti.biz/http
Sample outputs:
However, apache user is allowed to browse or access nfs based system mouted at /nfs/wwwroot/cyberciti.biz/http/:
# runuser -l apache -c ‘ls -l /nfs/wwwroot/cyberciti.biz/http/’
# runuser -l apache -c ‘cd /nfs/wwwroot/cyberciti.biz/http/; vi index.php’
No password is required to use runuser command and it must be run by root user only. Sometimes sudo or su will give following error:
To get around this try the following syntax:
# runuser -u www-data — command
## Run commands as www-data user ##
# runuser -u www-data — composer update —no-dev
# runuser -u www-data — php7 /app/maintenance/update.php
The runuser command options
- -l user_login_name : Make the shell a login shell, uses runuser-l PAM file instead of default one.
- -g group : Specify the primary group.
- -G group : Specify a supplemental group.
- -c COMMAND : Pass a single COMMAND to the shell with -c.
- —session-command=COMMAND : Pass a single COMMAND to the shell with -c and do not create a new session.
- -m : Do not reset environment variables.
- -u username : Run command as given username to get around ‘su -‘ or ‘sudo’ limit.
2. Run a Linux command with substitute user and group ID using ‘su command’
The su command allows you to become a super user or substitute user, spoof user, set user or switch user. It allows a Linux user to change the current user account associated with the running console or shell provided that you know the target user’s password. The syntax is as follows:
Источник
How To Run a Script In Linux
H ow do I run a Linux shell script? How can I run a script in Linux operating system using command line options?
By default, the shell script will not run. You need to set execute permission for your shell script. To execute or run script type the following command:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | N/A |
chmod +x script-name-here
OR
chmod 0755 script.sh
Next, use the ls command to view permission on the script:
$ ls -l script-name-here
To execute the script, type:
$ ./script-name-here
You can also run a script using any one of the following syntax:
$ /path/to/shell/script/backup.sh
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Run a script called backup.ksh using ksh shell:
$ ksh backup.ksh
To run a script called backup.bash using BASH shell:
$ bash backup.bash
Examples that shows how to run a script in Linux
Create a shell script called hello.sh using a text editor such as vi or gedit/nano:
nano hello.sh
OR
vim hello.sh
Append the following code:
Conclusion
You learned how to write a simple shell script and run a script in Linux operating system with help of chmod and other commands. Please see the following tutorials for more information on bash shell scripting under Linux or Unix-like operating systems:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
this is very good. It’s just missing one thing. the export command should be added in in the
/.bashrc file to survive across sessions.
$ tail -1
or the equivalent for your chosen shell (bs, ksh, tcsh etc …)
Just the solution I needed to figure this out, thanks man! 😀
Nice.
For compiling C prgms,
cc hello.c
./a.out
btw, Why don’t you have categories in WP?
Click on category icon (hint: Bash shell).
Hello Vivek, I was not aware that that image is a category link, too 🙂 It really looks like a post image. I’m referencing this site from time to time, and I’ve learned that you have categories just now! I’d really suggest using a different method for showing categories, unless you want to hide them 🙂 Thanks for the great site.
we can run a script even with out permissions using source command
sourch scriptname.sh
no dear if u want to run script the u hv to give execute permission on that file
like….scriptname.sh
chmod 755 or 777 scriptname.sh
Don’t be a patronizing shit…
hi. i m facing problem to write for setup ip address ,subnet and gateway in linux
kindly suggest me how i do…
if u have the give me idea…
#include
int main()
<
system(“c:\\windows\\your system name\\ip config”);
return 0;
>
Good information. I am trying to run a sql using a shell command.
Please provide the info..
How to run the Unix command only during the even and odd hours only.
Kindly provide the command to incorporate in the script.
Hi,
I am having a problem related to this. I have downloaded an program and it has a GUI written in Java. I need to run the script to launch this program, with “sudo” privileges in order to having it running properly. I don’t want to navigate to the folder where this script is, every time I need it, therefore I first made it executable and added the folder to the PATH. Now, when I write “sudo script-name”, I get “script-name not command found”, if I write only “script-name”, it finds it but it doesn’t run properly. Is there a way to launch a script, that is in the PATH, with sudo privileges? Thank you in advance.
Run it as follows:
Or cd to /home/foo and run:
Thank you. I decide to add it as an alias: “alias script-name=’sudo bash /path/to/script/script-name”
What’s the difference with §. /PATH/TO/TARGET§ and §./PATH/TO/TARGET§ ?
thanx, im learning and your are a good teacher.
Источник
How to Run or Repeat a Linux Command Every X Seconds Forever
A system administrator often needs to run a command repeatedly in a certain periods of time. Often such tasks can be easily completed with simple cron commands. In most of the cases this should work, but the shortest period which you can run cron command is every 1 minute. Believe it or not, in many cases this is too slow.
Run Linux Command Every Second
In this tutorial, you will learn a simple scripting techniques to monitor or keep a eye on a particular command in continuously running state similar to top command (continuously monitor the process and memory utilization) for every 3 seconds by default.
We will not stop to discuss the reasons, why you would need to run commands this often. I believe everyone has different reasons for that in their daily jobs or even at home PCs and laptops.
1. Use watch Command
Watch is a Linux command that allows you to execute a command or program periodically and also shows you output on the screen. This means that you will be able to see the program output in time. By default watch re-runs the command/program every 2 seconds. The interval can be easily changed to meet your requirements.
Monitor Memory Usage
“Watch” is extremely easy to use, to test it, you can fire up a Linux terminal right away and type the following command:
The above command will check your system free memory and update the results of the free command every two seconds.
Monitor Memory Usage in Linux
As seen per the above output, you have a header, displaying information about (from left to right) update interval, command that is being executed and current time. If you wish to hide this header, you can use the -t option.
The next logical question is – how to change the execution interval. For that purpose, you can use the -n option, that specifies the interval with which the command will be executed. This interval is specified in seconds. So let’s say you want to run your script.sh file every 10 seconds, you can do it like this:
Note that if you run the command like shown above, you will need to cd to the directory (learn Learn 15 cd Command Examples) where the script is located or otherwise specify the full path to that script.
Other useful options of watch command are:
- -b – creates a beep sound if the exit of the command is non-zero.
- -c – Interprets ANSI color sequences.
- -d – highlights the changes in the command output.
Monitor Logged-In Users, Uptime and Load Average
Let’s say you want to monitor logged-in users, server uptime and load average output in continuously phase every few seconds, then use following command as shown:
Watch Linux Load Average
To exit the command, press CTRL+C .
Here, the ‘uptime’ command will run and display the updated results every 2 seconds by default.
Monitor Progress of Copy Command
In Linux, while copying files from one location to other using cp command, the progress of data is not shown, to see the progress of data being copied, you can use the watch command along with du -s command to check the disk usage in real time.
Monitor Progress of Copy Command
If you think that the above process is too complicated to achieve, then I suggest you to go for Advance copy command, which shows progress of data while copying.
2. Use sleep Command
Sleep is often used to debug shell scripts, but it has many other useful purposes as well. For example, when combined with for or while loops, you can get pretty awesome results.
If you are new to bash scripting, you can check our guide about bash loops here.
In case this is the first time you hear about the «sleep» command, it is used to delay something for a specified amount of time. In scripts, you can use it to tell your script to run command 1, wait for 10 seconds and then run command 2.
With the above loops, you can tell bash to run a command, sleep for N amount of seconds and then run the command again.
Below you can see examples of both loops:
for loop Example
The above one liner, will run the echo command and display the current date, total of 10 times, with 5 seconds sleep between executions.
Here is a sample output:
You can change the echo and date commands with your own commands or script and change the sleep interval per your needs.
while loop Example
Here is sample output:
The above command will run until it is either killed or interrupted by the user. It can come in handy if you need to run a command running in the background and you don’t want to count on cron.
Important: When using the above methods, it is highly recommend that you set interval long enough to give enough time of your command to finish running, before the next execution.
Conclusion
The samples in this tutorial are useful, but are not meant to completely replace the cron utility. It is up to you to find which one works better for you, but if we have to separate the usage of both techniques, I would say this:
- Use cron when you need to run commands periodically even after system reboots.
- Use the methods explained in this tutorial for programs/scripts that are meant to run within the current user session.
As always if you have any questions or comments, do not hesitate to submit them in the comment section below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник