Linux Blog

Bash Scripting Dry Run

Filed under: Quick Linux Tutorials,Shell Script Sundays — TheLinuxBlog.com at 1:37 am on Sunday, September 12, 2010

http://www.flickr.com/photos/redteam/Occasionally when scripting it is desirable to not perform an action when modifying or creating a new script. In this case it is nice to be able to do a dry run similar to –dry-run for make.


When I’m making a script I’ve learned the hard way to:

  1. Make backups before hand
  2. Make backups while performing operations
  3. Perform mock dry runs by using echo liberally.

So next time you’re trying to do something complex and don’t want to do something goofy, stick an echo in there before running it and save yourself some time.

Image Source: http://www.flickr.com/photos/redteam/

Mounting Remote shares with SSHFS

Filed under: General Linux,Quick Linux Tutorials — TheLinuxBlog.com at 4:47 pm on Wednesday, August 11, 2010

If you have ssh access to a remote machine and wish to browse the file system SSHFS is a great utility.
It can be easily installed by your favorite package manager, or compiled from scratch. It has been a dear friend of mine for at least a couple of years.

To mount a file system with SSH, it is almost simpler than making an SSH connection:

sshfs [user]@[host]: [mountpoint]

to unmount is just as simple. Either:

fusermount -u [mountpoint]

OR

sudo umount [mountpoint]

Once your remote file system is mounted, you may bask in its glory and take advantage of everything a local file system has to offer although you may notice a slight delay.

If you’re an mplayer user and wish to stream media, you can use the -cache option to give you a little bit of a buffer.

Sequences with seq

Filed under: Quick Linux Tutorials,Shell Script Sundays — TheLinuxBlog.com at 9:28 pm on Sunday, July 19, 2009

I’m going to keep this one short since time I’ve had a serious lack of time recently. If you need to free up some time by putting repetitive commands into loops, you can use the command seq.

Basically, you can use any generic loop such as the while or for loop. Here are two examples:

for i in `seq 1 10`; do echo $i; done;
 
seq 1 10 | while read i; do echo $i; done;

These both count and echo from 1 to ten. Replace the echo $i; with whatever it is you want to do. You can use sleep, to sleep for a number of seconds, and any other logic you wish here.

Daily vim, Easier Command Line Editing Hack

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 2:06 pm on Tuesday, July 7, 2009

I’m a fan of the Daily Vim. While reading, I came across the old long command line edit: http://dailyvim.blogspot.com/2009/06/easier-command-line-editing.html

Since I don’t have emacs installed (fire away) after Pressing CTRL-x CTRL-e to edit the command, this is what I got:

[owen@LinuxBlog ~]$
bash: /usr/bin/emacs: No such file or directory

There is a simple fix for this:

sudo ln -s vim /usr/bin/emacs

I’m sure there is a correct way to do this, but this is what I did and it has worked well for me so far.

What groups am I in?

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 10:45 am on Tuesday, April 21, 2009

@nongeekboy on Twitter tweeted? about a blog post: Simple Script To List Groups in passwd File. I read it and have done something similar before so I figured I’d blog it here so I won’t have to write it again. Anyway, along with the point of this post, since this is suppose to be quick.

A question that is often asked is “What groups am I in?”

The easiest way to find out is to type the command: groups
This will give you a list of the groups you are in separated by a space. There are some other fancy ways of getting the groups but they rely on the `id` command. Running groups with no user name, its the same as running id -Gn.

Here are some other variations that you can try if you need to script the output:

1
2
3
4
5
6
7
8
9
groups
id
id -G
id -g
id -n
id -nG [user]
and the obvious:
id --help
info id

Run Levels in a Nutshell

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 9:03 am on Wednesday, March 11, 2009

Run levels in Linux are a great thing. Basically, a run level is by definition a configuration for a group of processes. The run levels and default run level is specified in /etc/inittab. Most Linux systems these days, with exception of a few boot into run level 5 which is generally a graphical user interface such as KDM or GDM. The others boot into run level 3 most servers will boot into this run level which is multi-user with networking but no X, and is many users preference.

To define what run level your system boots into by default you would edit the /etc/inittab file and edit the line similar to:

 id:5:initdefault:

This is run level 5, if you wanted to switch to command line you’d change the 5 to 3 and vice versa.

If your not ready to make the jump yet but would like to check it out, you can (as root) use the command telinit to tell init to change run level. If you are in run level 5, try (be prepared to lose everything in X, as it will kill everything for you)

 telinit 3

If you are doing maintenance, you may want to switch to level 1 which is single user mode. Level 2 on Fedora is the same as 3 except it doesn’t have NFS support.

Level 0 is halt and run level 6 is reboot which are the best ones to accidentally set as a default run level (trust me on this one.) For more information on the different run levels check out the man pages.

Adjust sudo timeout

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 1:05 pm on Thursday, March 5, 2009

SudoI think its pretty evident that I love sudo right? Well, what I do not like about sudo is the timeout. I understand while its there but five minutes is not exactly what I’d call an overly generous time, especially when I’m parked here at my desk for hours upon end. This tutorial shows you the line you’ll need adjust the sudo timeout:

First as root you’ll want to get into the sudo file and edit it. I’m sure you know how to do this since you’ve probably already visudo’ed your way into using sudo and are now trying to adjust the timeout. For those just reading for the sake of it, you’ll do the command: visudo

Right, now you’re there, you’ll either be in nano, pico or vi depending on your distribution. Search for the Defaults section, and put

Defaults:[your username] timestamp_timeout=[your timeout]

Replace your username with yours. Change your timeout to the number of minutes, or -1 for unlimited per session. Save and quit, then exit. Try it again, then try it again after the sudo timeout you set has changed. If it works, great news if not double check your sudoers file for another Default property that may be acting up.

Use VNC through SSH

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 11:33 am on Thursday, November 20, 2008

Here is another quick tutorial;

Some times its nice to tunnel through SSH. Perhaps you have SSH running but the firewall does not allow anything but SSH in. You can tunnel VNC (or any other service) through SSH by doing the following:

On the machine local to you establish an SSH connection to the remote machine with “Local (-L)”  port forwarding. This may seem confusing and often confuses me, where [-p PORT] is optional

 ssh -L 5901:localhost:5900 username@HOST [-p PORT];

Once I have the connection established I can now use vncviewer to connect to my local host with the port specified

vncviewer  localhost:5901

Thats all there is to it, have fun!

Using a custom Tomcat on Fedora

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 10:22 am on Wednesday, November 12, 2008

So, I hear you need to use Tomcat on Fedora eh? Not happy with the available Tomcat version from the repository? Well my friends you can add a custom Tomcat to Fedora and have it run as a service.

This post is somewhat related to: my Adding a service on Fedora post except this one is more specific to Tomcat. If you’d like more information on adding services to Fedora that is the place to look.

Here is the script that I have been using: (Read on …)

Forwarding Ports over an active SSH connection

Filed under: General Linux,Linux Software,Quick Linux Tutorials — TheLinuxBlog.com at 12:01 am on Tuesday, October 7, 2008

There is no doubt about it that SSH is a handy administration and remote access tool. Have you ever wanted to add port forwarding or cancel port forwarding from within an active SSH connection? Well, a feature that many do not know about is the ssh escape feature. With this pseudo terminal you can do some nifty things, such as forward ports over an active SSH connection.

To do this, follow these instructions:
(Read on …)

Charting your boot processes with bootchart

Filed under: Linux Software,Quick Linux Tutorials — TheLinuxBlog.com at 4:57 pm on Monday, September 22, 2008

Linux users often like to boast about their awesome bootup times. I thought that there was nothing cooler than getting a wicked fast bootup time, until now. A while back I found this nifty application called bootchart and shoved it in my bookmarks. I was randomly surfing my bookmarks, came across it again and gave it another shot. (Read on …)

« Previous PageNext Page »